Hi,
I have list of VMs in the txt format, i want to check the status of the VMs.
Some of the VMs doesn't exist in the VC. Could anyone help me with the script to get the result?
Need the details like VMName Status in the excel sheet. If the VM is not found, the status should be "Not found".
Thanks in advance.
It looks as if there is a <CR><LF> missing from your copy, those should be 2 lines.
I attached the script, including the Export-Csv line
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You mean something like this ?
Get-Content -Path C:\vmnames.txt |
Select @{N="VMname";E={$_}},
@{N="Status";E={
$vm = Get-VM -Name $_ -ErrorAction SilentlyContinue
if($vm){$vm.PowerState}else{"Not found"}
}}
The vmnames.txt file is assumed to have 1 name per line
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the quick reply. Yes, looking for the same script.
But I have "Blank" status for all servers.
As well as I want to convert the output as .csv file. Can you help me on this? Thanks in advance.
How does your vmnames,txt file look ?
To write the result to a CSV file you can append
| Export-Csv C:\report.csv -NoTypeInformation -UseCulture
to the end.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Thanks for the quick reply again. Still the VM status is blank.
vmnames.txt file looks like
VMName1
VMName2
VMName3
Just noticed there was a typo in the code (twice quotes on the "Status" label).
I corrected that. Can you try again ?
For me it seems to work with a similar input file
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the quick reply again and Thanks for the great help.
I already removed the quote from the "status".
But when i tried run just below line, I am getting the below error.
<
PowerCLI C:\Scripts> $vm = Get-VM VMname1 if($vm) {$vm.powerstate} else {"Not found"}
Get-VM : A positional parameter cannot be found that accepts argument 'if'.
At line:1 char:13
+ $vm = Get-VM <<<< VMName1 if($vm) {$vm.powerstate} else {"Not found"}
+ CategoryInfo : InvalidArgument: (:) [Get-VM], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM
>
Not sure, if it helps you to find what i am doing wrong in the script?
It looks as if there is a <CR><LF> missing from your copy, those should be 2 lines.
I attached the script, including the Export-Csv line
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Awesome . It works as a charm