We help IT Professionals succeed at work.

Report on all VM's "Power-on Boot Delay" value

CitizenRon
CitizenRon asked
on
I'm using the attached PowerShell code with the VI Toolkit (Snippet ID=710413) that I wrote to report on all virtual machines in my three VICs that have Snapshots.

I found the attached PowerShell code (Snippet ID=710418) that successfully sets the Boot Delay of the matching VMs to 10 seconds and it works fine for setting it.

I've been trying to figure out how to adapt the Boot Delay script to my Reporting script and have it report the Boot Delay value instead of the Snapshot details but I can't seem to figure out the proper way to have the Get-View cmdlet to show the Boot Delay instead of setting it.

Any assistance in this would be greatly appreciated.
Write-Host
$SepBarEqual = "=" * ($Host.UI.RawUI.WindowSize.Width - 1)
$ServerList = "VC1", "VC2", "VC3"
ForEach ($Server in $Serverlist)	{
	Connect-VIServer $Server | Out-Null
	$ServerNameBox = "=     ** "+$Server+" **"+" " * ($Host.UI.RawUI.WindowSize.Width - $Server.Length - 14)+"="
	Write-Host $SepBarEqual -ForegroundColor Cyan
	Write-Host $ServerNameBox -ForegroundColor Cyan
	Write-Host $SepBarEqual -ForegroundColor Cyan
	Get-VM | Get-Snapshot | Format-Table VM, Created, Name, Description -auto
}
Write-Host

Open in new window

$value = "10000"
Get-VM vm-KMSTest* | ForEach {
	$vm = Get-View $_.Id
	$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
	$vmConfigSpec.BootOptions = New-Object VMware.Vim.VirtualMachineBootOptions
	$vmConfigSpec.BootOptions.BootDelay = $value
	$vm.ReconfigVM_Task($vmConfigSpec)
}

Open in new window

Comment
Watch Question

Chris DentPowerShell Developer
Top Expert 2010

Commented:

No access to these until Monday... but do you get BootOptions back anywhere from Get-VM?

It'll be possible to add it in provided you get it back.

I did wonder if this might simplify your first line a bit:

"=     ** $Server **".PadRight($Host.UI.RawUI.WindowSize.Width - 2, ' ')+"="

It'll pad to $Host.UI.RawUI.WindowSize.Width as a maximum, automatically accounting for the length of $Server without you having to explicitly tell it to. -2 is there to give space for the other = symbol, I guessed that needed to be on the same line?

Chris

Author

Commented:
From my experience, Get-VM  doesn't do much by itself.  It's mostly a linking object to pass to  other cmdlets like Get-Snapshot  in my first script or Get-View in the second.  There's nothing returned from Get-Member that would indicate it  contains BootOptions.

Thanks for the PadRight tip though, that  works fine too and does shorten the code a bit for the $Server line.  Funny, using PadRight actually makes the $SepBarEqual line longer though!

Hmm, looking  again at the code that sets the BootDelay above...the actual thing doing  the work is $vm.ReconfigVM_Task.  The $vm object is created from the Get-View  cmdlet so I'm guessing I need to look at the methods and properties of Get-View for this???
 
 I'll check that out

Author

Commented:
Jackpot!  Why is it always easier to figure out something after you ask someone else??!?
 
 A Get-VM VM piped to Get-View has a Config property which has a BootOptions property which has a BootDelay property!
 
Here's my completed code for my Get-BootDelay.ps1 script that works nicely to show all VMs with a Boot Delay set...

For the points... Anyone want to help make this so that if I pass a VM Name (with or without wildcards) as a command line argument it will only report on those VMs whose name matches?  I'm thinking the best place (for simplicity and speed purposes) to do it is at the "Get-VM | ForEach" spot on line 10 since Get-VM supports wildcards.  Also, if I pass a command-line argument to Get-BootDelay.ps1, I want it to report the BootDelay even if it is zero.

So here's the modifications I need...

Get-BootDelay New*
This will show me the delay of all virtual machines in each VC server starting with "New", even if their delay is 0

Get-BootDelay *
This will show me the delay of all virtual machines in each VC server even if their delay is 0

Get-BootDelay
This will show me the delay of all virtual machines in each VC server only where the delay is greater than 0.

Write-Host
$SepBarEqual = "=" * ($Host.UI.RawUI.WindowSize.Width - 1)
$ServerList = "VC1", "VC2", "VC3"
ForEach ($Server in $Serverlist)	{
	Connect-VIServer $Server | Out-Null
	$ServerNameBox = "=     ** "+$Server+" **"+" " * ($Host.UI.RawUI.WindowSize.Width - $Server.Length - 14)+"="
	Write-Host $SepBarEqual -ForegroundColor Cyan
	Write-Host $ServerNameBox -ForegroundColor Cyan
	Write-Host $SepBarEqual -ForegroundColor Cyan
	Get-VM | ForEach {
	$VMView = Get-View $_.Id
	If ($VMView.Config.BootOptions.BootDelay -gt 0) {
		($_.Name).PadRight(40) + "Delay: " + $VMView.Config.BootOptions.BootDelay
		}
	}
}
Write-Host

Open in new window

PowerShell Developer
Top Expert 2010
Commented:

Depending on Get-VM...

Get-VM | Where-Object { $_.Name -Like $Filter } | ForEach-Object {
  ...

Or:

Get-VM -Name $Filter | ForEach-Object {
  ...

Adding parameters to the script. I feel it's worth separating out the filter and the option to show 0, it'll get rather confusing while trying to combine those.

Finally, personally I would return an Object containing the pertinent information below rather than using Write-Host. I haven't changed it because sometimes Write-Host is indeed appropriate. However, an object would allow far more flexibility, and mean you could pass to any of Format-List, Format-Table, Sort-Object, Out-GridView, Export-Csv, ConvertTo-Csv, ConvertTo-Html, etc, etc.

If you need to convert this back to PS 1, remove the comment block at the top, and remove the CmdLetBinding statement. The block at the top lets you return help like this:

Get-Help C:\Stuff\Scripts\PowerShell\ScriptName.ps1

Parameters can be validated in the Param block if required (using [Validate*( Setting )], where * is Count, Set, Pattern, Range or Script) although I haven't done so below.

Chris
<#
  .Synopsis
    A script to find and return the BootDelay for the specified virtual machines.
  .Description
    This script reads BootDelay from the BootOptions of each virtual machine matching the ]
    filter passed into this script.

    Requires VSphere PS CmdLets and requires PowerShell 2

    Adding this section makes the script comp
  .Parameter Filter
    Filters the Virtual Machine name. Supports wildcards. By default the filter is *.
  .Parameter Server
    A list of servers to perform this operation against.
  .Parameter ShowAll
    Display the BootDelay for all machines, even if the value is 0.
#>

[CmdLetBinding()]
Param(
  [String]$Filter = "*",
  [String[]]$Server = @("VC1", "VC2", "VC3"),
  [Switch]$ShowAll
)

Write-Host
$SepBarEqual = "=" * ($Host.UI.RawUI.WindowSize.Width - 1)
$Server | ForEach-Object {
  Connect-VIServer $_ | Out-Null

  $ServerNameBox = "=     ** "+$_+" **"+" " * ($Host.UI.RawUI.WindowSize.Width - $Server.Length - 14)+"="
  Write-Host $SepBarEqual -ForegroundColor Cyan
  Write-Host $ServerNameBox -ForegroundColor Cyan
  Write-Host $SepBarEqual -ForegroundColor Cyan

  Get-VM -Name $Filter | ForEach-Object {
    $VMView = Get-View $_.Id
    If ($VMView.Config.BootOptions.BootDelay -gt 0 -Or $ShowAll) {
      ($_.Name).PadRight(40) + "Delay: " + $VMView.Config.BootOptions.BootDelay
    }
  }
}
Write-Host

Open in new window

Author

Commented:
Thanks again Chris!  Wonderful coding as always.  This time I'm going to commit the Param stuff to long-term memory!