If else not working in powershell

DHoss 61 Reputation points
2022-06-06T12:44:10.21+00:00

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"

            }
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,363 questions
{count} votes

Accepted answer
  1. MotoX80 31,571 Reputation points
    2022-06-06T15:28:06.927+00:00

    Line 111. There is no $Connectivity.DNS variable. It;s $Connectivity.DNSCheck.

         if (($Connectivity.Ping) -and ($Connectivity.WMIAccess) -and ($Connectivity.DNSCheck))
    

2 additional answers

Sort by: Most helpful
  1. Newbie Jones 1,306 Reputation points
    2022-06-06T14:25:31.05+00:00

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

  2. Rich Matheisen 44,776 Reputation points
    2022-06-06T14:26:57.443+00:00

    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?