我正在使用PowerShell Studio创建一个多形式脚本,并且遇到了一些麻烦。
我试图根据Test-Connection将$ HostList ArrayList拆分为$ OnlineHosts和$ OfflineHosts。
我知道这个命令应该可行,我只是通过立即执行在线和离线数组的写主机,在我的一个普通ps1脚本上测试它。以下是我的ps1的工作代码部分:
Clear-Host
$HostList = Get-Content "$Home\Documents\Scripts\ComputerList.txt"
Write-Host "Pinging selected computers to verify connection..."
$OnlineHosts, $OfflineHosts = $HostList.Where({Test-Connection $_ -Count 1 -Quiet},'Split')
$OnlineHosts
pause
$OfflineHosts
pause
Clear-Host
Write-Host "Could not contact the following computers:`n`n$OfflineHosts`n`nThe following computers are online:`n`n$OnlineHosts`n`nContinue anyways?" -ForegroundColor Red
$ReadHost= Read-Host " (y/n) "
Switch ($ReadHost)
{
Y {Write-Host "Yes, Continuing to main menu."}
N {Write-Host "No, preparing to exit" ; Pause ; Exit}
Default {Write-Host "Yes, Continuing to main menu." }
}
但是,在我的Powershell Studio项目中,它不起作用。一个小上下文:这是一个从父表单传递$ HostList变量的新表单。我通过执行Write-Host" $ HostList woohoo来测试子表单是否正在接收变量!"我测试了将Test-Connection代码放入 并且在$ formTest_Load命令之外。老实说,我不确定究竟要放入或不放入$ formTest_Load命令,所以有关它的信息也会有所帮助。
这是我的PowerShell Studio项目中的代码:
param ( [parameter(Mandatory = $true)]
[string]$global:HostList )
$global:OnlineHosts = [System.Collections.ArrayList]@()
Write-Host "$HostList woohoo!"
$formTest_Load =
{
$OnlineHosts.Add($HostList)
$OnlineHosts, $OfflineHosts = $HostList.Where({Test-Connection $_ -Count 1 -Quiet },'Split')
}
以下是运行PowerShell Studio程序时收到的错误代码:
错误:测试连接:通用失败formPing.psf(14,49): 错误:在线:14字符:49 错误:+ ... neHosts = $ HostList.Where({Test-Connection $ _Count 1 Quiet},' Spl ... 错误:+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 错误:+ CategoryInfo:InvalidOperation:(:) [Test-Connection],ManagementException 错误:+ FullyQualifiedErrorId:TestConnectionException,Microsoft.PowerShell.Commands.Test ConnectionCommand ERROR:
我的测试连接代码似乎与我旧的ps1文件中的字符相同。
任何人都可以看到可能导致错误的原因吗?
感谢。
此外,这里有关于我正在运行的信息: -Windows 10 Pro -PowerShell Studio 2018 -version 5.5.152.0 -64位
在ISE中运行以下代码,您将看到我正在尝试做的事情:
$HostList = [System.Collections.ArrayList]@()
$HostList += HostName
$HostList += "FakeComputer"
Clear-Host
Write-Host "HostList contains: $HostList"
pause
Clear-Host
Write-Host "Verifying connection to target hosts..."
$OnlineHosts, $OfflineHosts = $HostList.Where({Test-Connection $_ -Count 1 -Quiet }, 'Split')
Clear-Host
Write-Host "HostList contains: $HostList`n"
Write-Host "OnlineHosts contains: $OnlineHosts`n"
Write-Host "OfflineHosts contains: $OfflineHosts`n"
pause
答案 0 :(得分:1)
我解决了这个问题,我的脚本传递变量的方式出错了。 在父表单中输入计算机名称的手动输入方法未正确地将变量从其脚本框中传出。
感谢您的所有帮助!
答案 1 :(得分:1)
我不会像这样的委托使用Where
方法。它有效,但它通常不是PowerShell的编写方式。 PowerShell的LINQ支持充其量是尴尬的,因此样式模式并不符合正常风格。我希望这样的事情:
$OnlineHosts = foreach ($HostName in $HostList) {
if (Test-Connection -ComputerName $HostName -Count 1 -Quiet) {
$HostName
}
}
$OfflineHosts = $HostList | Where-Object { $_ -notin $OnlineHosts }
或者像这样:
[System.Collections.ArrayList]$OnlineHosts = @()
[System.Collections.ArrayList]$OfflineHosts = @()
foreach ($HostName in $HostList) {
if (Test-Connection -ComputerName $HostName -Count 1 -Quiet) {
$OnlineHosts.Add($HostName)
}
else {
$OfflineHosts.Add($HostName)
}
}