无法将 Ping 响应添加到输出变量
Unable to add the Ping Response to Output variable
我已经编写了一个脚本来使用服务器 ping 响应导出特定的注册表项和其中的子项,但是我的脚本按预期工作但是当我将它添加到输出时 ping 响应值给出空值
请帮我获取每个服务器的 ping 响应值。
## Clear the host
Clear-Host
## Install Export-Excel Module if it is not installed
If (-Not (Get-InstalledModule -Name ImportExcel)){
Install-Module -Name ImportExcel -ErrorAction SilentlyContinue -Force
}
## Set Script Location
Set-Location $PSScriptRoot
## Out File Name
$FileName = "$PSScriptRoot\TCPIP_Interface_Details.xlsx"
## Get full list of servers
$Servers = GC -Path ".\Servers.txt"
## Delete if Old file exists
if (Test-Path $FileName) { Remove-Item $FileName
write-host "$FileName has been deleted" -BackgroundColor DarkMagenta }
else { Write-host "$FileName doesn't exist" -BackgroundColor Red }
## Loop through each server
$Result = foreach ($vm in $Servers) {
## Check the Ping reponse for each server
Write-Host "Pinging Server" $vm
$Ping = Test-Connection -Server $vm -Quiet -Verbose
if ($Ping){Write-host "Server" $vm "is Online" -BackgroundColor Green}
else{Write-host "Unable to ping Server" $vm -BackgroundColor Red}
## Check the Network Share path Accessibility
Write-Host "Checking Share Path on" $vm
$SharePath = Test-Path "\$vm\E$" -Verbose
if ($SharePath){Write-host "Server" $vm "Share Path is Accessible" -BackgroundColor Green}
else{Write-host "Server" $vm "Share path access failed" -BackgroundColor Red}
Invoke-Command -ComputerName $vm {
## Get ChildItems under HKLM TCPIP Parameter Interface
Get-ChildItem -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces' | ForEach-Object {
Get-ItemProperty -Path $_.PSPath | Where-Object { $_.PsObject.Properties.Name -like 'Dhcp*' }
} | Select-Object -Property @{Name = 'ComputerName'; Expression = {$env:COMPUTERNAME+"."+$env:USERDNSDOMAIN}},
@{Name = 'Ping_Response'; Expression = {if($using:Ping) {'Pinging'} else {'Unable to ping'}}},
@{Name = 'Share_Path_Access'; Expression = {if($using:SharePath) {'Accessible'} else {'Not Accessible'}}},
DhcpIPAddress, @{Name = 'DhcpNameServer'; Expression = {$_.DhcpNameServer -split ' ' -join '; '}},
DhcpServer, @{Name = 'DhcpDefaultGateway'; Expression = {$_.DhcpDefaultGateway -join '; '}}
}}
$Result | Select-Object * -Exclude PS*, RunspaceId
如评论所述,只需将计算的 属性 更改为
@{Name = 'Ping_Response'; Expression = {$using:Ping}}
通过用 using:
限定变量 $Ping
的范围,它将在 Invoke-Command 的脚本块中已知。如果没有它,scritblock 只会将其视为新的未定义变量(即 $null
)
如果正如您所评论的那样,您更愿意看到一些文本而不只是 True
或 False
,请在计算出的 属性 输出中包含任何您想要的表达式:
@{Name = 'Ping_Response'; Expression = {if($using:Ping) {'Pinging'} else {'Unable to ping'}}}
让脚本块知道 $Ping
是什么的另一种方法是将其作为参数添加到命令中:
Invoke-Command -ComputerName $vm {
param([bool]$Ping)
# ... the rest of the scriptblock
# now, you do not have to use the `using:` scope on $Ping
} -ArgumentList $Ping
为了回答您的最新评论,如果对机器执行 ping 操作无效,则确实没有理由尝试使用 Invoke-Command
从中获取属性。如果您还想在输出中包含 non-pingables,请执行:
$result = foreach ($vm in $Servers) {
$Ping = Test-Connection -Server $vm -Quiet -Verbose
if ($Ping) {
# we can reach this machine, so gather the properties we need and output an object
# the 'Ping_Response 'can now be hardcoded
Invoke-Command -ComputerName $vm -ScriptBlock {
Get-ChildItem -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces' | ForEach-Object {
Get-ItemProperty -Path $_.PSPath | Where-Object { $_.PsObject.Properties.Name -like 'Dhcp*' }
}
} |
Select-Object -Property @{Name = 'ComputerName'; Expression = {$_.PSComputerName}},
@{Name = 'Ping_Response'; Expression = { 'Pinging' }},
DhcpIPAddress,
@{Name = 'DhcpNameServer'; Expression = {$_.DhcpNameServer -split ' ' -join '; '}},
DhcpServer,
@{Name = 'DhcpDefaultGateway'; Expression = {$_.DhcpDefaultGateway -join '; '}}
}
else {
# return a similar object for machines that didn't respond to Ping
# since we cannot reach this machine, most properties will be empty
'' | Select-Object -Property @{Name = 'ComputerName'; Expression = {$vm}},
@{Name = 'Ping_Response'; Expression = { 'Unable to ping' }},
DhcpIPAddress,DhcpNameServer,DhcpServer,DhcpDefaultGateway
}
}
# output the results
$result | Select-Object * -ExcludeProperty PS*, RunspaceId | Export-Csv -Path "C:\temp\TCPIP_Interface_Details.csv" -NoTypeInformation
我已经编写了一个脚本来使用服务器 ping 响应导出特定的注册表项和其中的子项,但是我的脚本按预期工作但是当我将它添加到输出时 ping 响应值给出空值
请帮我获取每个服务器的 ping 响应值。
## Clear the host
Clear-Host
## Install Export-Excel Module if it is not installed
If (-Not (Get-InstalledModule -Name ImportExcel)){
Install-Module -Name ImportExcel -ErrorAction SilentlyContinue -Force
}
## Set Script Location
Set-Location $PSScriptRoot
## Out File Name
$FileName = "$PSScriptRoot\TCPIP_Interface_Details.xlsx"
## Get full list of servers
$Servers = GC -Path ".\Servers.txt"
## Delete if Old file exists
if (Test-Path $FileName) { Remove-Item $FileName
write-host "$FileName has been deleted" -BackgroundColor DarkMagenta }
else { Write-host "$FileName doesn't exist" -BackgroundColor Red }
## Loop through each server
$Result = foreach ($vm in $Servers) {
## Check the Ping reponse for each server
Write-Host "Pinging Server" $vm
$Ping = Test-Connection -Server $vm -Quiet -Verbose
if ($Ping){Write-host "Server" $vm "is Online" -BackgroundColor Green}
else{Write-host "Unable to ping Server" $vm -BackgroundColor Red}
## Check the Network Share path Accessibility
Write-Host "Checking Share Path on" $vm
$SharePath = Test-Path "\$vm\E$" -Verbose
if ($SharePath){Write-host "Server" $vm "Share Path is Accessible" -BackgroundColor Green}
else{Write-host "Server" $vm "Share path access failed" -BackgroundColor Red}
Invoke-Command -ComputerName $vm {
## Get ChildItems under HKLM TCPIP Parameter Interface
Get-ChildItem -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces' | ForEach-Object {
Get-ItemProperty -Path $_.PSPath | Where-Object { $_.PsObject.Properties.Name -like 'Dhcp*' }
} | Select-Object -Property @{Name = 'ComputerName'; Expression = {$env:COMPUTERNAME+"."+$env:USERDNSDOMAIN}},
@{Name = 'Ping_Response'; Expression = {if($using:Ping) {'Pinging'} else {'Unable to ping'}}},
@{Name = 'Share_Path_Access'; Expression = {if($using:SharePath) {'Accessible'} else {'Not Accessible'}}},
DhcpIPAddress, @{Name = 'DhcpNameServer'; Expression = {$_.DhcpNameServer -split ' ' -join '; '}},
DhcpServer, @{Name = 'DhcpDefaultGateway'; Expression = {$_.DhcpDefaultGateway -join '; '}}
}}
$Result | Select-Object * -Exclude PS*, RunspaceId
如评论所述,只需将计算的 属性 更改为
@{Name = 'Ping_Response'; Expression = {$using:Ping}}
通过用 using:
限定变量 $Ping
的范围,它将在 Invoke-Command 的脚本块中已知。如果没有它,scritblock 只会将其视为新的未定义变量(即 $null
)
如果正如您所评论的那样,您更愿意看到一些文本而不只是 True
或 False
,请在计算出的 属性 输出中包含任何您想要的表达式:
@{Name = 'Ping_Response'; Expression = {if($using:Ping) {'Pinging'} else {'Unable to ping'}}}
让脚本块知道 $Ping
是什么的另一种方法是将其作为参数添加到命令中:
Invoke-Command -ComputerName $vm {
param([bool]$Ping)
# ... the rest of the scriptblock
# now, you do not have to use the `using:` scope on $Ping
} -ArgumentList $Ping
为了回答您的最新评论,如果对机器执行 ping 操作无效,则确实没有理由尝试使用 Invoke-Command
从中获取属性。如果您还想在输出中包含 non-pingables,请执行:
$result = foreach ($vm in $Servers) {
$Ping = Test-Connection -Server $vm -Quiet -Verbose
if ($Ping) {
# we can reach this machine, so gather the properties we need and output an object
# the 'Ping_Response 'can now be hardcoded
Invoke-Command -ComputerName $vm -ScriptBlock {
Get-ChildItem -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces' | ForEach-Object {
Get-ItemProperty -Path $_.PSPath | Where-Object { $_.PsObject.Properties.Name -like 'Dhcp*' }
}
} |
Select-Object -Property @{Name = 'ComputerName'; Expression = {$_.PSComputerName}},
@{Name = 'Ping_Response'; Expression = { 'Pinging' }},
DhcpIPAddress,
@{Name = 'DhcpNameServer'; Expression = {$_.DhcpNameServer -split ' ' -join '; '}},
DhcpServer,
@{Name = 'DhcpDefaultGateway'; Expression = {$_.DhcpDefaultGateway -join '; '}}
}
else {
# return a similar object for machines that didn't respond to Ping
# since we cannot reach this machine, most properties will be empty
'' | Select-Object -Property @{Name = 'ComputerName'; Expression = {$vm}},
@{Name = 'Ping_Response'; Expression = { 'Unable to ping' }},
DhcpIPAddress,DhcpNameServer,DhcpServer,DhcpDefaultGateway
}
}
# output the results
$result | Select-Object * -ExcludeProperty PS*, RunspaceId | Export-Csv -Path "C:\temp\TCPIP_Interface_Details.csv" -NoTypeInformation