Can I "print" the name of a VM in Powershell?

177 Views Asked by At

Good Morning,

I want to print only the VM name so I can use it in the CLI from another program. We use Backup-Plans according to the use of the VM. For this we write stuff like "Test" into the Vm's comments.

For example:

I have:
VM1 Test
VM2 Linux
VM3 Test
VM4 Test

The other command line (from cloudberry) handels Backupplans like this:

./cbb editHyperVPlan -n backupname -v VM1 -v VM3 -v VM4

The thing is, I want to make this automatically, so I dont have to put everytime someone makes a new VM the VM myself into the list.

BUT the Cloudberry CLI does not seem to support the Powershell Commands (it works over powershell).

So my idea was that I try to print VM1 VM3 and VM4 into the -v -v -v ... Is that somehow possible? Sorry if this is written confusing, I can't really explain it better..

Edit: I use this command to get the VM's I need: Get-VM | Where-Object { $_.Notes -like 'Test'}

2

There are 2 best solutions below

0
Wasif On

Try this:

$cmd = '& ./cbb editHyperVPlan -n backupname '
 Get-VM | Where-Object {$_.Notes -like 'Test'} | ForEach {
  $cmd += "-v $($_.vmname) "
}

Command is stored in variable $cmd. To execute use Invoke-Expression $cmd

0
Theo On

Or something like this perhaps:

'./cbb editHyperVPlan -n backupname -v {0}' -f ((Get-VM | Where-Object {$_.Notes -like 'Test'}).Name -join '-v ')