mikesUSD
Contributor
Contributor

Get-VM name and quoted output

Greetings,

I have a need to get the names of all VMs in a specific folder into a variable so that I can "mass-migrate" using William Lam's awesome

New-XVCMRequest function

e.g. PS c:\>  New-XVCMRequest -opType Relocate -SrcSite CALIF -DstSite OREGON -SrcDatacenter SDDC-Datacenter -srcVMs @($CALIFservers) -DstDatacenter SDDC-Datacenter -DstCluster "Cluster-1" -DstHost $null -DstDatastore WorkloadDatastore -NetworkMapping @{”CALIF-sddc-VMs”="OREGON-sddc-VMs"} -DstPool Compute-ResourcePool -DstFolder Workloads

if I manually add VMnames like this:

PS C:\>  $CALIFservers = "Calif-Server-1","Calif-Server-2"

then output is

PS  c:\>  $CALIFservers

Calif-Server-1

Calif-Server-2

--- and the above command migrates 2 servers.  sweet!

I thought I was getting close to getting the list with:

PS C:\>  Get-VM -Location (Get-Folder -Name Workloads) | select Name | ft -HideTableHeaders

Calif-Server-1

Calif-Server-2

...

etc....

So, I thought, cool:  then I'll just do:

PS C:\>  $CALIFservers = Get-VM -Location (Get-Folder -Name Workloads) | select Name | ft -HideTableHeaders

as the output of C:\> $CALIFservers is:

Calif-Server-1

Calif-Server-2

...

etc....

But, No...  I cannot seem to get this:   $CALIFservers = Get-VM -Location (Get-Folder -Name Workloads) | select Name | ft -HideTableHeaders

to yield the same result as:    $CALIFservers = "Calif-Server-1","Calif-Server-2"

I need $CALIFservers to provide something of the format  "Calif-Server-1","Calif-Server-2",.... so the syntax will be correct for the above New-XVCMRequest function....

Any PowerCLI ideas?

thanks so much!!

~mikes

0 Kudos
1 Reply

It's not quoted output as such, it's an array of strings.

about_Arrays | Microsoft Docs

Powershell Arrays - PowerShell - SS64.com

I haven't tested it but possibly something like :

$srcVMs = (Get-VM | Select Name).Name

And then call New-XVCMRequest with "-srcVMs $srcVMs".

0 Kudos