PowerCLI Scripts – multiline

Just as VMware, PowerCLI is considered a very powerful tool to enable your environment, and its ability to do things with a single line can be very powerful. However, some things are better done in multiline scripts in order to be successful..

Multi-line scripts can be either typed in the PowerCLI command screen or entered in a separate file and called from the command line. To make use of a separate script file, create a file with the .ps1 extension, then right-click on it and select Edit. This will open the script in Windows PowerShell ISE. You can now enter the script. The following script lists the number of paths per datastore per host:

Save the script, then go back to the PowerCLI command line and simply call the name of the script. Be sure to use the full path of the script location, or use . if the script is in the current path. You may need to adjust your PowerShell security parameters to allow remote scripts to run from the command line:

A small sample of multi-line scripts is included in the following list to leverage the understanding of this tool:

  • This script lets you shut down a VM, change it's memory and CPU requirements and power on the system:
$vm_name = Get-VM “VMware vCenter Log Insight 1.0.4” ForEach ($vm in $vm_name){ $vm_name | Shutdown-VMGuest –Confirm:$False Sleep 60 $vm_name | Set-VM –MemoryGB 8 –NumCpu 2 –Confirm:$False $vm_name | Start-VM }
  • This script will let you know whether a datastore had been upgraded from VMFS v3 to v5 or has been recreated:
foreach ($myHost in get-VMHost) 
{
Write-Host ‘$myHost = ‘ $myHost
$esxcli = Get-EsxCli -VMHost $myHost
$esxcli.storage.core.device.partition.list() |
Where {$_.StartSector -eq "128"} |
Select Device, StartSector
}

This is just a small sample; an average VMware PowerCLI script will have dozens of lines, if not hundreds, of text and would be prohibitive to include here. Do take advantage of the community and blogs that are ripe with wonderful, detailed, and complex scripts ready for you to use with PowerCLI.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.64.132