Arrays 在PowerShell中,为什么if语句返回整个数组而不是单个对象?
我有一个脚本,可以找到主机名的作用域 我的PSCustomObjectArrays 在PowerShell中,为什么if语句返回整个数组而不是单个对象?,arrays,powershell,scope,dhcp,pscustomobject,Arrays,Powershell,Scope,Dhcp,Pscustomobject,我有一个脚本,可以找到主机名的作用域 我的PSCustomObjectLocation=$scope.name获取的是整个作用域数组,而不是匹配项,如下所示 Asset : U1087 Location : {bdg-213, Vo213, 0V-Gen-DH, 0-v734...} Status : Online Asset : U1281 Location : {bdg-213, Vo213, 0V-Gen-DH, 0-v734...} Status : Offline
Location=$scope.name
获取的是整个作用域数组,而不是匹配项,如下所示
Asset : U1087
Location : {bdg-213, Vo213, 0V-Gen-DH, 0-v734...}
Status : Online
Asset : U1281
Location : {bdg-213, Vo213, 0V-Gen-DH, 0-v734...}
Status : Offline
Asset : U1061
Location : {bdg-13, Vo213, 0V-Gen-DH, 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
正如其他人所评论的,循环的嵌套看起来并不正确。让我试着分析有问题的代码
[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
你想还什么?只是位置?例如:“bdg-213”?本质上是的。DHCP中的作用域属性“Name”是位置,因此
scope.Name
看起来您放错了一些花括号。例如。您使用的是$scopes
,它在内部循环中从外部循环获得输出。您的内部循环看起来不正确:$scopes
用于收集输出,但它也在最内部循环中使用,Get-DHCPServerv4Scope
结果将输出到管道,并将分配给最顶层的$scopes
,您将从最内层循环返回scriptblock{$scope.name}
。我不确定它应该如何工作,我感觉它会输出一些东西,只是因为您的会话或脚本中的其他地方有剩余变量。您可以执行以下操作:$Scope.Name.Location |选择-First 1
,因为$Scope.Name
返回第一个代码块中显示的输出什么你想回来吗?只是位置?例如:“bdg-213”?本质上是的。DHCP中的作用域属性“Name”是位置,因此scope.Name
看起来您放错了一些花括号。例如。您使用的是$scopes
,它在内部循环中从外部循环获得输出。您的内部循环看起来不正确:$scopes
用于收集输出,但它也在最内部循环中使用,Get-DHCPServerv4Scope
结果将输出到管道,并将分配给最顶层的$scopes
,您将从最内层循环返回scriptblock{$scope.name}
。我不确定它应该如何工作,我感觉它会输出一些东西,只是因为您在会话中或脚本中的其他地方有剩余变量。您可以执行以下操作:$Scope.Name.Location |选择-First 1
,因为$Scope.Name
返回第一个代码块中显示的输出总体意图是使用,Get-DhcpServerInDC
Get-DHCPServerv4Scope
和Get-DhcpServerV4Lease
来获取服务器信息。然后获取我的主机名列表,以查找它们所驻留的相应租约。这将告诉我主机名所在的建筑。如果你想看我上一篇文章的话,这确实是一个后续问题。我的总体意图是使用,Get-DhcpServerInDC
Get-DHCPServerv4Scope
和Get-DhcpServerV4Lease
来获取服务器信息。然后获取我的主机名列表,以查找它们所驻留的相应租约。这将告诉我主机名所在的建筑。如果你想看看我上一篇文章的话,这真的是一个后续问题。