Information Technology Reference
In-Depth Information
Where{$_.SyslogServer -notlike “192.168.0.100:514”} |`
Set-VMHostSyslogServer -SysLogServer 192.168.0.100 -SysLogServerPort 514
If you are following along, you'll note that this throws up an error. We wanted to show you
how this method has changed the VMHost object type, which Set-VMHostSyslogServer cannot
accept through the pipeline. One way to i x this is by using a ForEach loop (using the common
alias %) so that you process each VMHost and changing its object type back to something Set-
VMhostSyslogServer can use:
Get-VMHost | Select Name, @{N=“SyslogServer“;E={$_ |Get-VMHostSyslogServer}} |`
Where{$_.SyslogServer -notlike “192.168.0.100:514”} |`
%{Set-VMHostSyslogServer -VMhost (Get-VMHost -Name $_.Name)`
-SysLogServer 192.168.0.100 -SysLogServerPort 514}
Thanks for following along. We recognize that we had to do a few new things there to
accomplish something relatively simple. We wanted to go through this exercise to demonstrate
the value of the pipeline, the importance of understanding objects, and how much can be done
with a single line of PowerCLI code. You should now have enough exposure to start branching
beyond one-liners into multiline PowerCLI scripts.
Building PowerCLI Scripts
You have seen that one-liners are nothing more than a series of PowerCLI cmdlets strung into a
series of PowerShell pipelines. Scripts can often be as straightforward as a one-liner saved to a
text i le with a .ps1 i lename extension for future reuse. Many times, as you have seen, it is nec-
essary to do multiple steps to accomplish your automation goal. This is where you begin to tie
all of the topics we've discussed together. With that in mind, we will cover a few more examples
of how PowerCLI can make your life easier.
Migrating All Virtual Machines on a Host
In the i rst example, you'll build a simple pipeline using multiple PowerCLI cmdlets. By combin-
ing cmdlets in a pipeline, you can build more complex commands, such as the following:
Get-VMHost <FirstHost> | Get-VM | Move-VM -destination (Get-VMHost <SecondHost>)
This command relocates all VMs on the ESXi host specii ed by FirstHost to the ESXi host
represented by SecondHost. This includes both running VMs, which are moved with VMotion,
as well as powered-off VMs. Notice that we use parentheses when dei ning the destination
VMHost. PowerShell will run the content within the parentheses i rst and use the result for the
-destination parameter. This is similar to the mathematical order of operations.
You could also do this action by storing the source and destination VMHost in a variable:
$SourceHost = Get-VMHost <FirstHost>
$DestinationHost = Get-VMHost <SecondHost>
Get-VMHost $SourceHost | Get-VM | Move-VM -destination $DestinationHost
Set-VMHost $SourceHost -State Maintenance
Sufi ce it to say there are always multiple ways to accomplish the same thing.
 
Search WWH ::




Custom Search