Line 111. There is no $Connectivity.DNS variable. It;s $Connectivity.DNSCheck.
if (($Connectivity.Ping) -and ($Connectivity.WMIAccess) -and ($Connectivity.DNSCheck))
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi All
I have compiled this script to check the wmic status and for some reason it is not giving me the write output in host , if anyone can suggest what I am doing wrong that would be great!
$Computer = read-host -Prompt "Enter Computer Name"
function Global:Test-WMI
{
param(
#[string]$Computer = read-host -Prompt "Enter Computer Name"
[int]$WMITimeOutSec = 10,
[switch]$CleanUp
)
$Hash = @{"ComputerName" = $Computer}
$start = Get-Date
if ($computer -as [ipaddress]){$isValidIP = $true}else{$isValidIP = $false}
if ($isValidIP)
{
if (Test-Connection -ComputerName $Computer -Count 1 -Quiet)
{
$IP4 = $computer
}
else
{
$IP4 = $null
}
}
else
{
try{$IP4 = Test-Connection -ErrorAction 'stop' -ComputerName $Computer -Count 1 | select -ExpandProperty IPV4Address | select -ExpandProperty IPAddressToString}catch{}
}
if ($IP4)
{
Get-Job | where {$_.Command -match "Test-WMI" -and $_.State -eq "Completed"} | Remove-Job -force
$JobName = Start-Job -ArgumentList $IP4 -ScriptBlock { "Test-WMI"; (GWMI Win32_ComputerSystem -ComputerName $args).name } | select -ExpandProperty Name
do{
$state = Get-Job -name $JobName | select -ExpandProperty State
Start-Sleep -Milliseconds 50
$TimeSpan = New-TimeSpan $start (Get-Date)
}until($TimeSpan.TotalSeconds -ge $WMITimeOutSec -or $State -eq "Completed")
if ($State -eq "Completed")
{
$Name = (Receive-Job -name $JobName)[1]
$Hash.Set_Item("WMIAccess", $true)
if ($Name){ $Hash.Set_Item("ComputerName", $name) }
Remove-Job -Name $JobName -Force
}
else
{
$Hash.Set_Item("WMIAccess", $false)
if ($CleanUp)
{
Get-Job -Name $JobName | Remove-Job -force
}
}
$Hash.Set_Item("Ping", $true)
$Hash.Set_Item("WMI_Timer", (New-TimeSpan $start (Get-Date) | select -ExpandProperty TotalMilliseconds))
}
else
{
$Hash.Set_Item("Ping", $false)
$Hash.Set_Item("WMIAccess", $false)
}
if ( $name -eq $computer.trim() )
{
$Hash.Set_Item("DNSCheck", $true)
}
else
{
if ($isValidIP)
{
$Hash.Set_Item("DNSCheck", $true)
}
else
{
$Hash.Set_Item("DNSCheck", $false)
}
}
$Hash.Set_Item("isValidIP", $isValidIP)
New-Object -TypeName psobject -Property $Hash
}
$Connectivity = Test-WMI -computer $Computer
# Build hash table
$Hash = @{
Computer = $Computer
ComputerName = $Connectivity.ComputerName
Ping = $Connectivity.Ping
WMIAccess = $Connectivity.WMIAccess
#WMI_Timer = $Connectivity.WMI_Timer
C_Permissions = $false
WinRM = $false
RDP = $false
DNS = $Connectivity.DNSCheck
#isValidIP = $Connectivity.isValidIP
}
# Return Connectivity Object
# New-Object -TypeName PSObject -Property $Hash
#--------------------------------------------------
# }`
if ($Connectivity.Ping -and $Connectivity.WMIAccess -and $Connectivity.DNS -eq $true)
{
Write-host " $computer is Good"
}
else {
Write-Host " $computer has WMIC issue, please fix it"
}
Line 111. There is no $Connectivity.DNS variable. It;s $Connectivity.DNSCheck.
if (($Connectivity.Ping) -and ($Connectivity.WMIAccess) -and ($Connectivity.DNSCheck))
Should that be...
if ($Connectivity.Ping -eq $true -and $Connectivity.WMIAccess -eq $true -and $Connectivity.DNS -eq $true)
# or
if (($Connectivity.Ping -eq $true) -and ($Connectivity.WMIAccess -eq $true) -and ($Connectivity.DNS -eq $true))
If you're entering a computer NAME in the Read-Host, then the code on line 15 is going to do absolutely nothing!
On line 29, if the Test-Connection throws an exception, the "Catch" block does nothing.
The result is that $IPV4 remains undefined.
That may, or may not, be your problem . . . but you haven't said what you've done to debug your code. Because you don't know where it's working (incorrectly!) as you've coded it to, but producing results you didn't expect, you don't even know where to begin to correct it!
Have you run this code in a debugging console (PowerShell ISE, VS Code, Visual Studio, etc.) and set breakpoints to check the actual results against your expectations?