如何解决在 PowerShell 中,为什么我的 if 语句返回整个数组而不是单个对象?
我的 PSCustomObject Location = $scope.name
正在获取整个范围数组而不是匹配项,如下所示。
Asset : U1087
Location : {bdg-213,Vo213,0V-Gen-DH,0-v734...}
Status : Online
Asset : U1281
Location : {bdg-213,0-v734...}
Status : Offline
Asset : U1061
Location : {bdg-13,0-v734...}
Status : Online
我导入了一个名为 $list
的主机名 .txt 文件。然后循环遍历 $list
和 $scopes
以查找匹配项。
$list = Get-Content C:\script\HostNameList.txt #Defines content it pulls as list
$DHServers = Get-DhcpserverInDC #gives variable name for loop
$Output =
foreach ($hostname in $list) { #Calls each item in list a hostname and sends to output
if (test-connection -count 1 -computername $hostname -quiet){ #With 1 ping,check if hostname is online
[array] $scope =
foreach ($Server in $DHServers){
Get-DHcpserverv4Scope -ComputerName $Server.dnsname #get all scopes
$i = 0;
foreach ($scope in $scopes){
if($scope | Get-DhcpserverV4Lease -ComputerName $DHServers[$i++] | Where-Object HostName -like "$hostName*" ) #compares the hostname to lease to find which scope it is in`
{ $scopename} #return name of the scope it found hostname in. Name property is location in DHCP
}
}
[PSCustomObject]@{ #Rename varibles in data pull for output file
Asset = $hostname
Location = $scope.name #only want the name of the scope
Status = "Online"
}
}
else #statement if hostname is not online
{
}
}
$Output #show output in powershell
$Output | Export-Csv -Path C:\script\Asset_Result.csv -NoTypeinformation #outputs .csv
解决方法
正如其他人评论的那样,循环的嵌套看起来不正确。让我试着分析一下有问题的代码。
[array] $scopes = foreach ($Server in $DHServers){
# Creates output that is collected in $scopes
Get-DHCPServerv4Scope -ComputerName $Server.dnsname
# Now you are looping through the output of the outer loop,which is not finished yet,# in the inner loop,redundantly looping over $scopes multiple times.
$i = 0; foreach ($scope in $scopes){
if($scope | Get-DhcpServerV4Lease -ComputerName $DHServers[$i++] | Where-Object HostName -like "$hostName*")
{$scope.name} # Here you are outputting the scope name to $scopes
# again,which doesn't make any sense at all.
}
}
# Here you assign the $scope.name from the *last* iteration of the inner loop,regardless
# of the if statement in the inner loop. Again,this doesn't look right.
[PSCustomObject]@{
Asset = $hostname
Location = $scope.name
Status = "Online"
}
以下是“一半的答案”,因为我认为我没有完全理解您的代码的意图。至少这样对我来说看起来更合乎逻辑。
$DHServers = Get-DhcpServerInDC #gives variable name for loop
$Output = foreach ($hostname in $list){ #Calls each item in list a hostname and sends to output
if (test-connection -count 1 -computername $hostname -quiet){ #With 1 ping,check if hostname is online
[array] $scopes = foreach ($Server in $DHServers){# Collects the output from all Get-DHCPServerv4Scope calls
Get-DHCPServerv4Scope -ComputerName $Server.dnsname
}
$i = 0
foreach ($scope in $scopes){#loop through array
if($scope | Get-DhcpServerV4Lease -ComputerName $DHServers[$i++] | Where-Object HostName -like "$hostName*"){ #compares the hostname to lease to find which scope it is in
[PSCustomObject]@{ #Rename varibles in data pull for output file
Asset = $hostname
Location = $scope.name #only want the name of the scope
Status = "Online"
}
}
}
}
}
$Output | Format-Table #show output in PowerShell
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。