VMware Cloud Community
nava_thulasi39
Jump to solution

VMs status in the list

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.

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
nava_thulasi39
Jump to solution

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.

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
nava_thulasi39
Jump to solution

Hi LucD,

Thanks for the quick reply again. Still the VM status is blank.

vmnames.txt file looks like

VMName1

VMName2

VMName3

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
nava_thulasi39
Jump to solution

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?

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
nava_thulasi39
Jump to solution

Awesome Smiley Happy. It works as a charm Smiley Happy

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos