# Get-ChildItem and Get-WmiObject cim\_datafile showing different results

**URL:** <https://forums.powershell.org/t/get-childitem-and-get-wmiobject-cim-datafile-showing-different-results/21677>\
**Category:** PowerShell Help\
**Created:** [February 16, 2023, 4:39pm UTC](https://forums.powershell.org/t/get-childitem-and-get-wmiobject-cim-datafile-showing-different-results/21677 "2023-02-16T16:39:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![isht1008](https://avatars.discourse-cdn.com/v4/letter/i/4af34b/32.png) [@isht1008](https://forums.powershell.org/u/isht1008)\
**Post date:** [February 16, 2023, 4:39pm UTC](https://forums.powershell.org/t/get-childitem-and-get-wmiobject-cim-datafile-showing-different-results/21677/1 "2023-02-16T16:39:32Z")

</div>

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.

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 16, 2023, 9:58pm UTC](https://forums.powershell.org/t/get-childitem-and-get-wmiobject-cim-datafile-showing-different-results/21677/2 "2023-02-16T21:58:23Z")

</div>

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. 😉

> [@isht1008](#):
>
> ```auto
> #$EnVer = ((Get-ChildItem C:\Windows\system32\enstart64.exe).VersionInfo).FileVersion
> 
> ```

That does not work because you do NOT query a remote computer - you query the computer the code runs on!! 😉 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

> **[Invoke-Command (Microsoft.PowerShell.Core) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7.3)**
>
> The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors. Using a single Invoke-Command command, you can run commands on multiple computers. To run a single command on a remote...

Here is how I would approach this task

```auto
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:

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

```

or like this:

```auto
$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.

---

<div class="post-metadata">

**Author:** ![isht1008](https://avatars.discourse-cdn.com/v4/letter/i/4af34b/32.png) [@isht1008](https://forums.powershell.org/u/isht1008)\
**Post date:** [February 16, 2023, 10:06pm UTC](https://forums.powershell.org/t/get-childitem-and-get-wmiobject-cim-datafile-showing-different-results/21677/3 "2023-02-16T22:06:16Z")

</div>

> [@Olaf](#):
>
> ```auto
> [Alias('hostname', 'ComputerName', 'Server')]
> [string[]]
> $ComputerNameList
> 
> ```

What does this mean.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 16, 2023, 10:10pm UTC](https://forums.powershell.org/t/get-childitem-and-get-wmiobject-cim-datafile-showing-different-results/21677/4 "2023-02-16T22:10:12Z")

</div>

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

> **[about Functions Advanced Parameters - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.3#alias-attribute)**
>
> Explains how to add parameters to advanced functions.

---

<div class="post-metadata">

**Author:** ![isht1008](https://avatars.discourse-cdn.com/v4/letter/i/4af34b/32.png) [@isht1008](https://forums.powershell.org/u/isht1008)\
**Post date:** [February 16, 2023, 10:27pm UTC](https://forums.powershell.org/t/get-childitem-and-get-wmiobject-cim-datafile-showing-different-results/21677/5 "2023-02-16T22:27:48Z")

</div>

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

```auto
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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 16, 2023, 10:39pm UTC](https://forums.powershell.org/t/get-childitem-and-get-wmiobject-cim-datafile-showing-different-results/21677/6 "2023-02-16T22:39:53Z")

</div>

> [@isht1008](#):
>
> any idea why above code giving error 0x80041001

No. It works in my environment. 🤷🏽‍♂️

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:15pm UTC](https://forums.powershell.org/t/get-childitem-and-get-wmiobject-cim-datafile-showing-different-results/21677/7 "2024-05-16T20:15:11Z")

</div>


