I mostly use PowerShell to automate tasks with VMware PowerCLI and I have recently started working on my first advanced functions. I created one that gets virtual machines that are inside a specific folder in vCenter. The main PowerCLI cmdlet used in this function is Get-VM so the output for Get-VM and my function is basically the same as far as I can tell. However, -PipelineVariable doesn’t work for me as it does with Get-VM. When I run the following commands -which essentially do the same- the {$VM} is empty in the output of Get-SgVm (my function) but populated with the virtual machine name when using Get-Folder piped to Get-VM, everything else in the output is exactly the same.
[pre]Get-SgVm -PipelineVariable VM | Get-NetworkAdapter | select {$VM},Name,NetworkName
Get-Folder RootFolder-001 | Get-VM -PipelineVariable VM | Get-NetworkAdapter | select {$VM},Name,NetworkName[/pre]
Here is the code of the function.
[pre]
Function Get-SgVm {
[CmdletBinding()]
param ([Parameter (Mandatory=$true,Position = 0)]
[ValidateNotNullOrEmpty()]
[int]$SGNumber,
[switch]$NoRecursion,
[switch]$Legacy)
BEGIN {
[string]$SGNumber = ‘{0:000}’ -f $SGNumber
Write-Verbose “[$((Get-Date).TimeOfDay.ToString()) BEGIN ] Formatting SG Number to: $SGNumber”
}
PROCESS {
If ($Legacy){
$Folder = Get-Folder -Name (-join (‘RootFolder’,$SGNumber,'’))
Write-Verbose “[$((Get-Date).TimeOfDay.ToString()) BEGIN ] Folder found: $Folder”
}
Else {
Write-Warning ‘Legacy not selected.’
$Folder = Get-Folder -Name (-join (‘RootFolder-‘,$SGNumber,’’))
Write-Verbose “[$((Get-Date).TimeOfDay.ToString()) BEGIN ] Folder found: $Folder”
} #If Legacy
If ($NoRecursion){
Write-Verbose “[$((Get-Date).TimeOfDay.ToString()) BEGIN ] Getting VMs”
$VMs = $Folder | Get-VM -NoRecursion
Write-Warning ‘NoRecursion switch used, getting VMs in root of SG folder only.’
}
Else {
Write-Verbose “[$((Get-Date).TimeOfDay.ToString()) BEGIN ] Getting VMs”
$VMs = $Folder | Get-VM
Write-Warning ‘NoRecursion switch not used, getting all VMs in SG folder.’
} #If Recursion
If ($Folder -ne $null){
Write-Verbose “[$((Get-Date).TimeOfDay.ToString()) PROCESS] Working on folder: $Folder”
}
Else {
Write-Warning ‘No folder found. Check the switches used in the command.’
} #If Folder
If ($VMs -ne $null) {
Write-Verbose “[$((Get-Date).TimeOfDay.ToString()) PROCESS] VM(s) found”
}
Else {
Write-Verbose “[$((Get-Date).TimeOfDay.ToString()) PROCESS] No VM(s) found”
} #If VMS
If (!$global:DefaultVIServer) {
Throw ‘Not connected to a vCenter server’
}
Else {
Write-Verbose “[$((Get-Date).TimeOfDay.ToString()) PROCESS] Writing output.”
Write-Output $VMs
} #If vCenter connection
} #Process
END {}
} #Function[/pre]