我实际上有一个数组$ServerList
,其中包含要测试的数组列表,而$ExclusionList
是必须排除的服务器列表。
代码应使用Test-Connection
测试服务器列表,并将不可达的服务器保存在$nonactivearray
中,并将其从$ServerList
中删除。
之后,它将从$ExclusionList
中删除$ServerList
中的所有条目。
如果$ServerList
变成空数组,它将要求用户键入服务器列表。
在给出正确的服务器之前,代码似乎可以正常工作。
我发现,尽管$nonactivearray
为空,但$nonactivearray.Count
或$nonactivearray.Length
返回1
,因此由于$ServerList
和{{1 }}的元素数大于$nonactivearray
。
0
谢谢,最好。
[编辑]
对不起,我也忘记了在代码顶部添加的以下几行:
# Define function to center the text in the current line (taken from: https://stackoverflow.com/a/48622585/10516690)
function Write-HostCenter {
param($Message)
Write-Host ("{0}{1}" -f (' ' * (([Math]::Max(0, $Host.UI.RawUI.BufferSize.Width / 2) - [Math]::Floor($Message.Length / 2)))), $Message)
}
# Define Test-Connection as a string for Invoke-Expression later
[string]$TestConn = "Test-Connection -Quiet -Count 1 -ComputerName "
# Define excluded servers
$ExclusionList = @("Server1","Server2","Server3")
# Save old progress settings
$OldProgress = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
# Test Server availability
$nonactivearray = @() # Array of unreachable/excluded servers
$nonactive = "" # String to display the unreachable/excluded doscovered in our list of servers. Probably unnecessary, "$nonactivearray" can be used
$i = 0
foreach ($newserver in $ServerList) {
if (-Not (Invoke-Expression -Command "$TestConn $newserver")) {
$nonactive += $newserver
$nonactivearray += $newserver
if ($i -lt $ServerList.Length) {
$nonactive += " "
}
}
$i++
}
$ProgressPreference = $OldProgress
# List unreachable and excluded servers
$nonactivearray += (Compare-Object -ReferenceObject $ServerList `
-DifferenceObject $ExclusionList `
-ExcludeDifferent -IncludeEqual).InputObject
$nonactive = "$nonactivearray"
$CurPosPing = $host.UI.RawUI.CursorPosition
while ($nonactivearray.Length -ge 1) {
$host.UI.RawUI.CursorPosition = $CurPosPing
Write-Host " These HOSTS are not reachable, hence they will be ignored:"
$TempForegroundColor = $Host.Ui.RawUI.ForegroundColor # <== Temporary solution
$Host.Ui.RawUI.ForegroundColor = "Red" # <== Temporary solution
Write-HostCenter $nonactive # Write-HostCenter defined in a function. It centers the text in the shell
$Host.Ui.RawUI.ForegroundColor = $TempForegroundColor # <== Temporary solution
# Remove unreachable servers with filters
$ServerList = $ServerList | Where-Object {$nonactivearray -notcontains $_}
# Check if the new list is empty
while ($ServerList.Length -lt 1) {
Write-Host " "
$CurPos = $host.UI.RawUI.CursorPosition
Write-Host " The list of server is now empty, please type it now. " -ForegroundColor Yellow
Write-Host " Use the comma `"" -NoNewline; Write-Host "," -NoNewline -ForegroundColor Yellow
Write-Host "`" as a separator (spaces will be ignored)."
Write-Host " "
# Get list from user
# $CurPos = $host.UI.RawUI.CursorPosition
$ServerList = Read-Host -Prompt " Server list"
$ServerList = $ServerList -replace ' ',''
[string[]]$ServerList = $ServerList -split ','
$ServerList = $ServerList | Where-Object {$_.length -gt 0}
while ($ServerList.Length -lt 1) {
$Host.UI.RawUI.CursorPosition = $CurPos
Write-Host " >>> The list is STILL empty, please type it again. <<< " -ForegroundColor Yellow
Write-Host " "
Write-Host " "
# $CurPos = $host.UI.RawUI.CursorPosition
$ServerList = Read-Host -Prompt " Server list"
$ServerList = $ServerList -replace ' ', ''
[string[]]$ServerList = $ServerList -split ','
$ServerList = $ServerList | Where-Object {$_.length -gt 0}
}
}
# Re-check server connectivity
$OldProgress = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
Write-Host "serverlist"
$ServerList
$nonactivearray = @()
$nonactive = ""
$i = 0
foreach ($newserver in $ServerList) {
if (-Not (Invoke-Expression -Command "$TestConn $newserver")) {
$nonactive += $newserver
$nonactivearray += $newserver
if ($i -lt $ServerList.Length) {
$nonactive += " "
}
}
$i++
}
$ProgressPreference = $OldProgress
$nonactivearray += (Compare-Object -ReferenceObject $ServerList `
-DifferenceObject $ExclusionList `
-ExcludeDifferent -IncludeEqual).InputObject
$nonactive = "$nonactivearray"
}
我希望我不会忘记其他任何事情...
答案 0 :(得分:0)
感谢您的帮助。
我无法通过提出的解决方案解决问题,但是您给了我寻找解决方案的见识。
我搜索了“如何从数组中删除$null
值”,然后在另一篇文章中找到了this answer from mklement0。
我尝试了以下方法:
PS N:\> $list = @("1","","3","$null","7")
PS N:\> $list
1
3
7
PS N:\> $list -notlike ''
1
3
7
,然后在我的代码中添加以下行:
$nonactivearray = $nonactivearray -notlike ''
这解决了问题。 :)
再次感谢您。