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.
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
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.
I tried below but it is not getting service status instead getting {HRESULT 0x80041001,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand}
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.