Hi All,
I am looking for a script to list all VM's in VC which are not enable with AutoStartup when ESX host powered on. Can anyone help me to create a script.
Regards,
Boopathy S
Try something like this
foreach($esx in Get-VMHost){
$vmNotEnabled = $null
if($esx.ExtensionData.Config.AutoStart.Defaults.Enabled){
$autoVM = $esx.ExtensionData.Config.AutoStart.PowerInfo | where{$_.STartAction -eq 'PowerOn'} | Select -ExpandProperty Key
$vmNotEnabled = $esx.ExtensionData.Vm | where{$autoVM -notcontains $_} | %{Get-View -Id $_ -Property Name | Select -ExpandProperty Name}
}
$esx | Select Name,
@{N='AutoStart Enabled';E={$esx.ExtensionData.Config.AutoStart.Defaults.Enabled}},
@{N='VMs no AutoStart';E={$vmNotEnabled -join '|'}}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Thanks for the reply, i have a question. Do i need to execute towards Vcenter or this for single Host?
Regards,
Boopathy S
You connect to the vCenter
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, really excellent and it's working... then how can i get the output in CSV file.
Hi LucD,
It's working, but providing only the Host which are enabled with AutoStart, can you help to get VM details on all host.
Try like this
$report = foreach($esx in Get-VMHost){
$autoVM = $esx.ExtensionData.Config.AutoStart.PowerInfo | where{$_.STartAction -eq 'PowerOn'} | Select -ExpandProperty Key
$vmNotEnabled = $esx.ExtensionData.Vm | where{$autoVM -notcontains $_} | %{Get-View -Id $_ -Property Name | Select -ExpandProperty Name}
$esx | Select Name,
@{N='AutoStart Enabled';E={$esx.ExtensionData.Config.AutoStart.Defaults.Enabled}},
@{N='VMs no AutoStart';E={$vmNotEnabled -join '|'}}
}
$report | Export-Csv c:\VM-autostart.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Below is the Error received for above code,
Export-Csv : Object reference not set to an instance of an object.
At C:\Users\Documents\Scripts\VMWARE\AutoEnable1.ps1:15 char:21
+ $report | Export-Csv <<<< C:\Users\Documents\Scripts\VMWARE\VM-autostart.csv -NoTypeInformation -UseCulture
+ CategoryInfo : NotSpecified: (:) [Export-Csv], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.ExportCsvCommand
Looks like $report is empty.
Can you show the complete script you have running ?
Or perhaps run the script without the capture in the $report variable. Does that produce results ?
foreach($esx in Get-VMHost){
$autoVM = $esx.ExtensionData.Config.AutoStart.PowerInfo | where{$_.STartAction -eq 'PowerOn'} | Select -ExpandProperty Key
$vmNotEnabled = $esx.ExtensionData.Vm | where{$autoVM -notcontains $_} | %{Get-View -Id $_ -Property Name | Select -ExpandProperty Name}
$esx | Select Name,
@{N='AutoStart Enabled';E={$esx.ExtensionData.Config.AutoStart.Defaults.Enabled}},
@{N='VMs no AutoStart';E={$vmNotEnabled -join '|'}}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes the above script getting results, but on VM's No AutoStart colum VM names are piped if the names exceeds 2 it's displaying like ............
Example: XXXXXXYYYYYYZZZZZ | XXXXXXYYYYYYZZZZZZ | XXXX................. - here some security reason it is mentioned XYZ for VM name's
Apart this your script rocks.....
Since we can't display an array with the Select-Object cmdlet, the script creates one string of all the names with the Join function.
You can change the Join separator with whatever character you want.
The partial display of some fields on the screen, is due to the fact that the PowerShell output engine has to work with a limited line length.
If you redirect the result to a CSV, you will see the complete field in the file.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I tried to get output in .txt and csv but results are same....:smileyconfused:
With an Export-Csv cmdlet ?
Did you do it like this ?
Remember, a ForEach doesn't place any objects in the pipeline, so you have to capture the output created within the ForEach loop in a variable.
And then you can pipe that variable to the Export-Csv cmdlet.
$report = foreach($esx in Get-VMHost){
$autoVM = $esx.ExtensionData.Config.AutoStart.PowerInfo | where{$_.STartAction -eq 'PowerOn'} | Select -ExpandProperty Key
$vmNotEnabled = $esx.ExtensionData.Vm | where{$autoVM -notcontains $_} | %{Get-View -Id $_ -Property Name | Select -ExpandProperty Name}
$esx | Select Name,
@{N='AutoStart Enabled';E={$esx.ExtensionData.Config.AutoStart.Defaults.Enabled}},
@{N='VMs no AutoStart';E={$vmNotEnabled -join '|'}}
}
$report | Export-Csv report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I am getting same error as below for above script also it's not producing any results.
+ $report | Export-Csv <<<< report.csv -NoTypeInformation -UseCulture
+ CategoryInfo : NotSpecified: (:) [Export-Csv], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.ExportCsvCommand
Tried using > Report.txt and report.csv both are not getting any results.
I can manage to get Host alone using your script with some modification,
foreach($esx in Get-VMHost){
$autoVM = $esx.ExtensionData.Config.AutoStart.PowerInfo | where{$_.STartAction -eq 'PowerOn'} | Select -ExpandProperty Key
$esx | Select Name,
@{N='AutoStart Enabled';E={$esx.ExtensionData.Config.AutoStart.Defaults.Enabled}}
}
Attached screenshot of script.
I tried multiple ways, but I can't reproduce your issue.
How, and from where, do you launch the script ?
A screenshot would be handy.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I really appreciate your efforts on this, I am launching this script from my Terminal server powercli by connecting VCenter using command Connect-VIServer.
Then i am executing this script.
Not producing any results ending with below error.
That looks ok, no idea what might be going wrong.
More so since it works in my environment.
Can you attach your .ps1 file as a file to the is thread ?
On a side note, the PowerCLI version you are using is quite old.
Any specific reason you stay on this old version ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Sorry for late reply, at last it working and i am able to get what i want through your script.
What went wrong?.... Power CLI version having some issues on getting correct report.
I have upgraded Power CLI now it's getting reports in CSV.
Thanks a lot for your kind help...