我正在嘗試編寫將檢查服務器主機名的腳本。 現在我有:爲powershell腳本升級輸出
Computers.txt
192.168.10.10
192.168.10.11
192.168.10.12
和腳本:
$servers = get-content "C:\Script\computers.txt"
Invoke-Command -Credential company\admin1 -ComputerName $computers -scriptblock {[Environment]::GetEnvironmentVariable("ComputerName")} | out-file C:\Script\report_hostnames.txt
而且我報告:
Computer1
Computer2
Computer3
你能幫我添加IP地址來報告和錯誤狀態,如下所示:
192.168.10.10 Computer1
192.168.10.11 Computer1
192.168.10.12 Computer Unavailable
我試過了:foreach;嘗試,趕上,如果,否則,但不明白如何以正確的方式使用它。
謝謝,這真的很有用。但現在它要求每個記錄的密碼。 試圖設置: $ username =「company \ admin1」 $ password = cat C:\ temp \ pass.txt | convertto-securestring $ cred = new-object -typename System.Management.Automation.PSCredential' -argumentlist $ username,$ password 但是有些問題。 – Alex
謝謝你的幫助。 – Alex
謝謝你的幫助。 這是它現在的樣子: ' $ mycredentials = Get-Credential Get-Content「c:\ Script \ computers.txt」| foreach {$ job = invoke-command -credential $ mycredentials -ComputerName $ _ -ScriptBlock {hostname.exe} write-output「$ _ $ job」| out-file「C:\ Script \ report_hostnames.txt」-Append } ' – Alex