我有一大堆主機名,我需要 ping 以查看它們是否在 ping。我是腳本新手,但我設法弄清楚了這一點,但是在運行此腳本時出現錯誤,我使用“PowerShell.exe script.ps > output.csv”運行腳本。
下面的錯誤:
"pinghost.ps : The term 'pinghost.ps' is not recognized as the name of a cmdlet function script file or operable
program. Check the spelling of the name or if a path was included verify that the path is correct and try again.
At line:1 char:1
#NAME?
~~~~~~~~~~~
CategoryInfo: ObjectNotFound: (pinghost.ps:String) [] CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException :"
下面的代碼:
$names = Get-content ".\hnames.txt"
foreach ($name in $names){
if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
Write-Host "$name,up"
}
else{
Write-Host "$name,down"
}
}
uj5u.com熱心網友回復:
PowerShell 將嘗試決議作為與其作業目錄相關的命令列引數傳遞的不合格檔案路徑- 如果您從運行提示符啟動 PowerShell 或作為以特定本地用戶帳戶運行的計劃任務,則作業目錄將默認為用戶組態檔檔案夾.
要么傳遞一個根檔案路徑:
powershell.exe -File C:\Users\Akshay\path\to\pinghost.ps1
或者相對于預期作業目錄的檔案路徑(使用/而不是\會阻止 PowerShell 將路徑解釋為模塊限定的命令名稱):
powershell.exe -File path/to/pinghost.ps1
為了使 PowerShell 的提供程式 cmdlet 正確決議hnames.txt相對于腳本本身位置的檔案,請在路徑前添加$PSScriptRoot自動變數:
$names = Get-content (Join-Path $PSScriptRoot hnames.txt)
foreach ($name in $names){
if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
Write-Host "$name,up"
}
else{
Write-Host "$name,down"
}
}
現在只要腳本和文本檔案在同一個目錄中,它就可以作業,不管你從哪里呼叫它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/437598.html
標籤:电源外壳
