Home > Blockchain >  Unable to add the Ping Response to Output variable
Unable to add the Ping Response to Output variable

Time:04-02

I have written a script to export specific registry keys and the sub keys inside it with the server ping response, but my scripts works as expected but the ping response value when I add it to output it is giving null

Please help me to get the ping response value for each server.

## 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

## Get full list of servers
$Servers = GC -Path ".\Servers.txt"

## 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}

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 = {$Ping}}, 
              DhcpIPAddress, @{Name = 'DhcpNameServer'; Expression = {$_.DhcpNameServer -split ' ' -join '; '}},
              DhcpServer,    @{Name = 'DhcpDefaultGateway'; Expression = {$_.DhcpDefaultGateway -join '; '}}
}}
$Result | Select-Object * -Exclude PS*, RunspaceId

CodePudding user response:

As commented, just change your calculated property into

@{Name = 'Ping_Response'; Expression = {$using:Ping}}

By scoping the variable $Ping with using:, it will be known in the scriptblock for Invoke-Command. Without that, the scritblock simply sees it as new, undefined variable (i.e. $null)

If as you commented, you would rather see some text instead of just True or False, have the expression in that calculated property output whatever you want like:

@{Name = 'Ping_Response'; Expression = {if($using:Ping) {'Pinging'} else {'Unable to ping'}}}

Another way of letting the scriptblock know what $Ping is, is by adding this to the command as parameter:

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
  • Related