Below is a script I use to check the status of a service on a list of servers and export the results to a CSV.
$user = 'domain\username'
$pwd = 'password' | ConvertTo-SecureString -Force -AsPlainText
$credential = New-Object System.Management.Automation.PsCredential($user, $pwd)
foreach($vmlist in (Get-Content -Path C:\Path\Server_List.txt)){
$vms = Get-VM -Name $vmlist
$o= Invoke-VMScript -VM $vms -ScriptText 'Get-Service -Name BESClient |convertto-csv' -ScriptType Powershell -GuestCredential $credential
$o.ScriptOutput |convertfrom-csv | Select @{N='VM';E={$vms}}, Name, Status |
Export-Csv -Path C:\Path\report.csv -NoTypeInformation -UseCulture -Append
}
The script works fine is the service exists but if it doesn't, the output I get in the CSV is a loop of the server without the service listed 5 times. What I'm trying to do is check for the service and if it doesn't exist, make the 'Status' column display "Doesn't Exist."
Actual output:
Expected output:
I have an IF statement to check for the service but I cannot figure out how to export it into the CSV:
Get-Service -Name BESClient -ErrorAction SilentlyContinue
if($service -eq $null)
{
# Set "Status" value to "Doesn't Exist"
} else {
# Keep going...
}
Thank you ahead of time!
You could do something like this
$user = 'domain\username'
$pwd = 'password' | ConvertTo-SecureString -Force -AsPlainText
$credential = New-Object System.Management.Automation.PsCredential($user, $pwd)
$code = @'
$obj = New-object PSObject -Property @{
Name = 'Service does not exist'
Status = 'Not Installed'
}
$service = Get-Service -Name BESClient -ErrorAction SilentlyContinue
if($service)
{
$obj.Name = $service.Name
$obj.Status = $service.Status
}
$obj | ConvertTo-Csv
'@
Get-VM -Name (Get-Content -Path C:\Path\Server_List.txt) -PipelineVariable vm |
ForEach-Object -Process {
$o = Invoke-VMScript -VM $vm -ScriptText $code -ScriptType Powershell -GuestCredential $credential
$o.ScriptOutput | ConvertFrom-Csv |
Select-Object -Property @{N = 'VM'; E = { $vm.Name } }, Name, Status
} | Export-Csv -Path C:\Path\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could do something like this
$user = 'domain\username'
$pwd = 'password' | ConvertTo-SecureString -Force -AsPlainText
$credential = New-Object System.Management.Automation.PsCredential($user, $pwd)
$code = @'
$obj = New-object PSObject -Property @{
Name = 'Service does not exist'
Status = 'Not Installed'
}
$service = Get-Service -Name BESClient -ErrorAction SilentlyContinue
if($service)
{
$obj.Name = $service.Name
$obj.Status = $service.Status
}
$obj | ConvertTo-Csv
'@
Get-VM -Name (Get-Content -Path C:\Path\Server_List.txt) -PipelineVariable vm |
ForEach-Object -Process {
$o = Invoke-VMScript -VM $vm -ScriptText $code -ScriptType Powershell -GuestCredential $credential
$o.ScriptOutput | ConvertFrom-Csv |
Select-Object -Property @{N = 'VM'; E = { $vm.Name } }, Name, Status
} | Export-Csv -Path C:\Path\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Bingo! After changing one little typo ($o = Invoke-VMScript -VM $vm -ScriptText... instead of $o = Invoke-VMScript -VM $vms -ScriptText...) it works perfectly.
Thank you!
That is no typo, the PipelineVariable parameter on the Get-VM cmdlet stores the VM in the $vm variable.
That is if you used my snippet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Correct. Your script called for the $vms variable instead of the $vm variable. When I deleted the 's' it worked perfectly.
Ah, ok, I see.
Probably because I copied your line and forgot to edit that 🙄
I updated the code above as well, that way future visitors to this thread won't suffer from my typo 😉
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference