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

Please do not create new threads if it’s still the same issue like in your last question.

Using Write-Host is considered very bad style. Try to avoid it. :wink:

That does not work because you do NOT query a remote computer - you query the computer the code runs on!! :wink: If you want to query a remote computer you’d need to use either a UNC path like \\$($ComputerName)\C$\Windows\system32\enstart64.exe or use explicit remoting by

Here is how I would approach this task

function Get-EncaseInfo {
    [CmdletBinding()]
    param (
        [Alias('hostname', 'ComputerName', 'Server')]
        [string[]]
        $ComputerNameList
    )
    process {
        foreach ($ComputerName in $ComputerNameList) {
            $HashTable = 
            [ordered]@{
                ComputerName = $ComputerName
                Online       = $false
                Service      = 'n/a'
                StartType    = 'n/a'
                EnVersion    = 'n/a'
            }
            $HashTable.Online = 
            Test-Connection -ComputerName $ComputerName -Count 1 -Quiet
            if ($HashTable.Online) {
                $CimSession = New-CimSession -ComputerName $ComputerName

                $Service = Get-CimInstance -ClassName CIM_Service -Filter 'Name = "enstart64"' -CimSession $CimSession
                $EnVer = Get-CimInstance -ClassName CIM_DataFile -Filter 'Name = "C:\\Windows\\system32\\enstart64.exe"' -CimSession $CimSession

                $HashTable.Service = $Service.State
                $HashTable.StartType = $Service.StartMode
                $HashTable.EnVersion = $EnVer.Version
            }
            [PSCustomObject]$HashTable
        }
    }
}

If you want to use a file as input you can use it like this:

Get-EncaseInfo -ComputerName (Get-Content D:\IT\servers.txt) |
Format-Table -AutoSize

or like this:

$ComputerNameList = Get-Content D:\IT\servers.txt
Get-EncaseInfo -ComputerName $ComputerNameList |
Format-Table -AutoSize

Please keep in mind that if you use -AutoSize PowerShell will wait for the last element to be processed to determine the proper width of the columns for all elements.

What does this mean.

Please use your prefered internet search engine before you ask something in a forum like this.

I tried below but it is not getting service status instead getting {HRESULT 0x80041001,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand}

function Get-EncaseInfo {
    [CmdletBinding()]
    param (
        [Alias('hostname', 'ComputerName', 'Server')]
        [string[]]
        $ComputerNameList
    )
    process {
        foreach ($ComputerName in $ComputerNameList) {
            $HashTable = 
            [ordered]@{
                ComputerName = $ComputerName
                Online       = $false
                Service      = 'n/a'
                StartType    = 'n/a'
                EnVersion    = 'n/a'
            }
            $HashTable.Online = 
            Test-Connection -ComputerName $ComputerName -Count 1 -Quiet
            if ($HashTable.Online) {
                $CimSession = New-CimSession -ComputerName $ComputerName

                $Service = Get-CimInstance -ClassName CIM_Service -Filter 'Name = "enstart64"' -CimSession $CimSession -ErrorAction SilentlyContinue -ErrorVariable EnServError
                $EnVer = Get-CimInstance -ClassName CIM_DataFile -Filter 'Name = "C:\\Windows\\system32\\enstart64.exe"' -CimSession $CimSession -ErrorAction SilentlyContinue -ErrorVariable EnVerError

                $HashTable.Service = $Service.Status
                $HashTable.StartType = $Service.StartMode
                $HashTable.EnVersion = $EnVer.Version
                $HashTable.EnServError = $EnServError
                $HashTable.EnVerError = $EnVerError
            }
            [PSCustomObject]$HashTable
        }
    }
}

Get-EncaseInfo -ComputerName (Get-Content D:\IT\servers.txt) | Format-Table

Then I changed the code a bit and it worked. any idea why above code giving error 0x80041001

function Get-EncaseInfo {
    [CmdletBinding()]
    param (
        [Alias('hostname', 'ComputerName', 'Server')]
        [string[]]
        $ComputerNameList
    )
    process {
        foreach ($ComputerName in $ComputerNameList) {
            $HashTable = 
            [ordered]@{
                ComputerName = $ComputerName
                Online       = $false
                Service      = 'n/a'
                StartType    = 'n/a'
                EnVersion    = 'n/a'
            }
            $HashTable.Online = 
            Test-Connection -ComputerName $ComputerName -Count 1 -Quiet
            if ($HashTable.Online) {
                $CimSession = New-CimSession -ComputerName $ComputerName

                $Service = Get-Service -Name 'enstart64' -ComputerName $ComputerName -ErrorAction SilentlyContinue -ErrorVariable EnServError
                $EnVer = Get-CimInstance -ClassName CIM_DataFile -Filter 'Name = "C:\\Windows\\system32\\enstart64.exe"' -CimSession $CimSession -ErrorAction SilentlyContinue -ErrorVariable EnVerError

                $HashTable.Service = $Service.Status
                $HashTable.StartType = $Service.StartMode
                $HashTable.EnVersion = $EnVer.Version
                $HashTable.EnServError = $EnServError
                $HashTable.EnVerError = $EnVerError
            }
            [PSCustomObject]$HashTable
        }
    }
}


Get-EncaseInfo -ComputerName (Get-Content D:\IT\servers.txt) |
Format-Table -AutoSize

No. It works in my environment. :man_shrugging:t4:

But BTW I just noticed that the status of the service is returned with the property State - not Status. I updated my code suggestion above accordingly.