'Powershell - If is not working (windows services checks)

I am trying to build my own script to check some Windows services (status and start mode) and I am facing an issue on the IF ... For example even if the service is "Running", it will never run the code inside the IF...

let me share my code below (I am a newbie on powershell so be gentle xD)

For info, I will do more actions inside the IF and ELSE, it is just for the example.

# import computers list, 1 by line
$Computers = get-content .\computers.txt

# define variable of services we want to check
$ServiceNetbios = "netbt"

# define variable to ask credentials
$Cred = Get-Credential

# declare Function to open a session a remote computer
Function EndPSS { Get-PSSession | Remove-PSSession }

EndPSS

########################################################
#                   BEGINNING OF SCRIPT                #
#                         by xxx                       #
#                       2022-02-03                     #
########################################################


# loop for each computer imported from the file
foreach ($computer in $computers) {
    
    # show name of computer in progress
    $computer

    # connect remotely to the computer
    $session = New-PSSession -ComputerName $computer -Credential $Cred

    # check Netbios service
    $StatusServiceNetbios = Invoke-Command -Session $session -ScriptBlock { Get-Service -Name $Using:ServiceNetbios | select -property * }
    
    # Check Netbios service started or not
    write-host $StatusServiceNetbios.Status
    
    if ($StatusServiceNetbios.Status -eq 'Running')
        {
           Write-host "IF Running"
        }
    else
    {
        write-host "IF NOT Running"
    }
    
EndPSS

}

and what return my script :

computername
Running           (<= the variable $StatusServiceNetbios.Status )
IF NOT Running    (<= the ELSE action)

Thanks you in advance for your help, this drive me crazy and maybe this is very simple...



Solution 1:[1]

To complement Cpt.Whale's helpful answer, this is likely to be caused by the serialization and deserialization done by Invoke-Command:

using namespace System.Management.Automation

$service = Get-Service netbt
$afterInvokeCmd = [PSSerializer]::Deserialize(([PSSerializer]::Serialize($service)))

$service.Status -eq 'Running'                   # => True
$afterInvokeCmd.Status -eq 'Running'            # => False
$afterInvokeCmd.Status.Value -eq 'Running'      # => True
$afterInvokeCmd.Status.ToString() -eq 'Running' # => True

To put some context to my answer, this is a nice quote from about_Remote_Output that can better explain why and what is happening:

Because most live Microsoft .NET Framework objects (such as the objects that PowerShell cmdlets return) cannot be transmitted over the network, the live objects are "serialized". In other words, the live objects are converted into XML representations of the object and its properties. Then, the XML-based serialized object is transmitted across the network.

On the local computer, PowerShell receives the XML-based serialized object and "deserializes" it by converting the XML-based object into a standard .NET Framework object.

However, the deserialized object is not a live object. It is a snapshot of the object at the time that it was serialized, and it includes properties but no methods.

Solution 2:[2]

You could use

@Query("SELECT count() FROM user_table WHERE id=:id")
fun ifUserExists(id: Int): Boolean
  • This uses the count aggregate function to get the number of rows according to the WHERE clause. As the value returned will be either 0 (doesn't exist) or probably 1 (shouldn't be greater) does exist then this can directly be interpreted as a Boolean.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2