Forum Discussion

isht1008's avatar
isht1008
Copper Contributor
Feb 16, 2023

Get-ChildItem and Get-WmiObject cim_datafile showing different results

I am using below code to Check if the server is pinging then for Pingable Servers I am checking the Service Status and a file version but I see Get-ChildItem and Get-WmiObject cim_datafile showing different results. Get-WmiObject cim_datafile showing correct results. Looks like Get-ChildItem giving wrong results because it does not support -computername. any idea how can I use any cmdlet which does not support -computername in this script and still get correct results.

 

 

 

 

 

 

function Get-EncaseInfo {
    [CmdletBinding()]
    param (
        #[Parameter(Mandatory = $True)]
        #[string[]]$serverName
    )
    process {
        $servers = (Get-Content D:\IT\servers.txt)
        $server_error = $false
        $servers_down = @()
        $servers_up = @()
        foreach ($server in $servers) {
            if(Test-Connection $server -Count 1 -ea 0 -Quiet){
            #Write-Host $server "is Pinging" -BackgroundColor Green -ForegroundColor White
            $servers_up += "$server"
            }else{
            #Write-Host $server "is not Pinging" -BackgroundColor Red -ForegroundColor White
            $servers_down += "$server"
            $server_error = $true
            }}
            Write-Host ""
            Write-Host "INFO: Below Servers are up" -ForegroundColor Green
            Write-output $servers_up 
            if($server_error){
            $error_msg = "Below Servers are not Pinging" 
            Write-Host ""
            Write-Host $error_msg -ForegroundColor Red
            Write-output $servers_down
            }else{

            Write-Host "INFO: ALL Servers are Pinging" -ForegroundColor Blue -BackgroundColor White}

            foreach ($Servup in $servers_up){
                
                $Service=Get-Service -Name enstart64 -ComputerName $Servup -ErrorAction SilentlyContinue -ErrorVariable Errs
                #$EnVer = ((Get-ChildItem C:\Windows\system32\enstart64.exe).VersionInfo).FileVersion
                $EnVer = (Get-WmiObject cim_datafile -ComputerName $Servup -Filter {Name='C:\\Windows\\system32\\enstart64.exe'}).version

                [PSCustomObject]@{
                'ComputerName'=$Servup;
                'Service'=$Service.Status;
                'StartType'=$Service.StartType;
                'EnVersion'=$EnVer;
                'Error'=$Errs;
            }
        }
    }
}


Get-EncaseInfo | FT -AutoSize

 

 

 

 

 

 

Resources