我有一個名爲script1.ps1的PowerShell腳本,可以很好地工作。下面是該腳本:自動搜索本地「管理員」組的成員
Write-Host Script to display members of the local -ForegroundColor Green
Write-Host Administators group of a remote server. -ForegroundColor Green
Write-Host "`n"
$strComputer = Read-Host "Please enter the computer name"
$computer = [ADSI]("WinNT://" + $strComputer + ",computer")
$group = $computer.PSBase.Children.Find("administrators")
Write-Host ""
Write-Host "Computer Name : "$computer.Name
Write-Host "_____________________________________"
Write-Host ""
Write-Host "Group Name : "$Group.Name
Write-Host "_____________________________________"
$domain = $group.Path.Split("/")[2]
$string1 = "WinNT://" + $domain + "/" + $strComputer + "/"
$string2 = $strComputer + "/"
$string3 = "WinNT://"
$members = ($group.PSBase.Invoke("Members") | Foreach-Object {$_.GetType().InvokeMember("Adspath", 'GetProperty', $null, $_, $null)}) -replace ($string1,$string2) -replace ($string3,"")
Write-Host ""
Write-Host "Members : "
Write-Host ""
$members
但正如你所看到的,我不得不計算機的名稱我想每次都寫。 我問自己,是否有一個功能或其他東西自動從文本或CSV文件中的計算機名稱? 像這樣在腳本開始時,它會自動將所有計算機名稱&給我本地「管理員」組的成員,然後將其導出到一個文本文件或CSV文件中?
編輯:根據給出的答案更新腳本。
我有一個名爲script1.ps1的PowerShell腳本不能很好地工作。下面是該腳本:
Write-Host Script to display members of the local -ForegroundColor Green
Write-Host Administators group of a remote server. -ForegroundColor Green
Write-Host "`n"
Get-Content 'C:\Users\herbautr\Desktop\List1.txt' | ForEach-Object {
Write-Host "-$_-"
$computer = [ADSI]("WinNT://" + $_ + ",computer")
$group = $computer.PSBase.Children.Find("administrators")
Write-Host ""
Write-Host "Computer Name : "$computer.Name
Write-Host "_____________________________________"
Write-Host ""
Write-Host "Group Name : "$Group.Name
Write-Host "_____________________________________"
$domain = $group.Path.Split("/")[2]
$string1 = "WinNT://" + $domain + "/" + $_ + "/"
$string2 = $_ + "/"
$string3 = "WinNT://"
$members = ($group.PSBase.Invoke("Members") | Foreach-Object {$_.GetType().InvokeMember("Adspath", 'GetProperty', $null, $_, $null)}) -replace ($string1,$string2) -replace ($string3,"")
Write-Host ""
Write-Host "Members : "
Write-Host ""
$members
} | Set-Content 'C:\Users\herbautr\Desktop\administrators.txt'
我加1級的計算機名稱到List1.txt:
01SPEAI-TEST1 01SPEAI-TEST2 02SPHPV-TEST1 01SLCPTAAP-PROD
和它的作品(不)完美(不可讀佈局)
01SPEAI-PROD1/Administrator
VNF-PRINCIPAL/Admins du domaine
VNF-PRINCIPAL/svceri
01SPEAI-PROD2/Administrator
VNF-PRINCIPAL/Admins du domaine
VNF-PRINCIPAL/svceri
02SPHPV-PROD1/Administrator
VNF-PRINCIPAL/Admins du domaine
01SLCPTAAP-PROD/Administrator
VNF-PRINCIPAL/Admins du domaine
01SLCPTAAP-PROD/maint
VNF-PRINCIPAL/svcoraas
VNF-PRINCIPAL/svcvisionit
VNF-PRINCIPAL/GopOAS
VNF-PRINCIPAL/svcdigora
注(15:18 pm):我試着用5個名字,它繼續工作..
爲什麼只添加1個名字它「有效」? Oo'
檢查「$ _」是否是您認爲的。 「Write-Host」的輸出是什麼 - $ _-「'? –
@Ansgar好的我明白了,我已經做了這個檢查$ _ =「lol」 寫主機「$ _」(顯示我哈哈)我明白了。但是,我應該在Get-Content之前定義$ _還是可以按照我想要的方式重新設置$ _? – Skyxia
你應該在你的循環中加入'Write-Host' - $ _-「'並且報告輸出結果。正如我在這裏所說的那樣,使用陳述*。不要留下任何東西。不要添加任何東西。 –