2010/05/25

如何使用找來的 scripts

開始學 powershell 了,在還不會寫的現在,先拿別人寫好的來用,遇到了第一個問題:
要怎麼把文字存成檔案再執行?


$Result = @()

foreach($server in (gc .\servers.txt)){

$computer = [ADSI](”WinNT://” + $server + “,computer”)
$Group = $computer.psbase.children.find(”Administrators”)

function getAdmins
{$members = ($Group.psbase.invoke(”Members”) | %{$_.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $_, $null)}) -replace ('WinNT://DOMAIN/' + $server + '/'), '' -replace ('WinNT://DOMAIN/', 'DOMAIN\') -replace ('WinNT://', '')
$members}

$Result += Write-Output "SERVER: $server"
$Result += Write-Output ' '
$Result += ( getAdmins )
$Result += Write-Output '____________________________'
$Result += Write-Output ' '
}

$Result > d:\results.txt

Invoke-Item d:\results.txt

存檔副檔名 .ps1 (ps one) ,先啟動 powershell ,再去執行即可

參考文件:
http://technet.microsoft.com/en-us/library/ee176949.aspx

沒有留言: