我有一個服務器串列,我必須在其中找到特定用戶“adtuser”,如果它是每個服務器中管理員組的一部分,并輸出一個文本檔案。
目前我有這個腳本,它部分有效。
我有所需的輸出,但是缺少一些服務器(如果您單獨檢查它們就可以了)并且腳本需要很多時間。
提前致謝
Get-Content C:\servers.txt | ForEach-Object {
if (-not (Test-Connection -ComputerName $_ -Count 1 -Quiet)) {
Write-Warning "Server '$_' is Unreachable hence Could not fetch data"
return
}
$computer = $_
([adsi]"WinNT://$_").Children.ForEach{
if($_.SchemaClassName -ne 'user' -and $_.Name.Value -ne 'ADTuser') {
return
}
$groups = $_.Groups().ForEach([adsi]).Name
[pscustomobject]@{
Computername = $computer
UserName = $_.Name.Value
Memberof = $groups -join ';'
Status = $groups -contains 'Administrators'
}
}
} | Out-File -FilePath C:\users.txt
uj5u.com熱心網友回復:
請注意,這test-netconnection
需要 powerhshell 2.0 或更高版本。方法.ForEach
需要 Powershell 4.0 或更高版本。
正如@Santiago提到的——我正在使用Test-Connection
with-port 3389
來測驗 Windows RDP 埠。OP 最初只是測驗 ICMP 連接性,由于常見的防火墻規則,這是一個糟糕的測驗。
您可以測驗任何已知的 Windows 埠,但通常可以安全地假設 RDP 已打開。NMAP(或者你的網路管理員……grin)可能會給你最好的指導。
#!/usr/bin/env powershell
Get-Content -Path $env:HOMEDRIVE/servers.txt | ForEach-Object {
if (-not (Test-Connection -ComputerName $_ -Count 1 -Quiet)) {
Write-Warning -Message ("Server '{0}' is Unreachable hence Could not fetch data" -f $_)
return
}
$computer = $_
([adsi]('WinNT://{0}' -f $_)).Children.ForEach{
if($_.SchemaClassName -ne 'user' -and $_.Name.Value -ne 'ADTuser') {
return
}
$groups = $_.Groups().ForEach([adsi]).Name
[pscustomobject]@{
Computername = $computer
UserName = $_.Name.Value
Memberof = $groups -join ';'
Status = $groups -contains 'Administrators'
}
}
} | Out-File -FilePath $env:HOMEDRIVE/users.txt
uj5u.com熱心網友回復:
既然您問過,如果您的服務器都可以使用 PowerShell 5.1 或更高版本,您可以使用Get-Local*
cmdlet。
根據您的問題,您正在專門尋找某個用戶ADTuser
$userToCheck = 'ADTuser'
$servers = Get-Content -Path 'C:\servers.txt'
$cred = Get-Credential -Message 'Please enter your admin credentials'
# loop through the list of servers and collect the output objects
$result = foreach ($computer in $servers) {
if (-not (Test-Connection -ComputerName $computer -Count 1 -Quiet)) {
Write-Warning -Message "Server '$computer' is Unreachable hence Could not fetch data"
# output a failed object
'' | Select-Object @{Name = 'ComputerName'; Expression = {$computer}}, UserName,MemberOf,
@{Name = 'Status'; Expression = {'Unreachable'}}
# skip processing this server and proceed with the next one
continue
}
# here we probe the server for the existance of user $userToCheck
$out = Invoke-Command -ComputerName $computer -Credential $cred -ScriptBlock {
$user = Get-LocalUser -Name $using:userToCheck -ErrorAction SilentlyContinue
if ($user) {
# get the names of all local groups the user is member of
$userGroups = (Get-LocalGroup | Where-Object { ($_ | Get-LocalGroupMember).SID -contains $user.SID }).Name
[PsCustomObject]@{
Computername = $env:COMPUTERNAME
UserName = $user.Name
MemberOf = $userGroups -join '; '
Status = $userGroups -contains 'Administrators'
}
}
else {
[PsCustomObject]@{
Computername = $env:COMPUTERNAME
UserName = $using:userToCheck
MemberOf = $null
Status = "User does not exist"
}
}
}
# output the object but remove extra properties PowerShell added
$out | Select-Object * -ExcludeProperty PS*, RunspaceId
}
# show in the console window
$result | Format-Table -AutoSize
# you can now use Export-Csv to output the results in a structured file you can open with Excel
$result | Export-Csv -Path 'C:\users.csv' -UseCulture -NoTypeInformation
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/470013.html