0

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'}

Lubot
  • 3
  • 1
  • so you want the part _before_ the 1st space? if so, use `.Split(' ')[0]` to get that 1st item. – Lee_Dailey Apr 24 '20 at 13:46
  • also, would you please wrap your code in code formatting? the instructions for that are linked on the same page that you used to create your Question. [*grin*] – Lee_Dailey Apr 24 '20 at 13:47

2 Answers2

0

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

Wasif
  • 14,755
  • 3
  • 14
  • 34
0

Or something like this perhaps:

'./cbb editHyperVPlan -n backupname -v {0}' -f ((Get-VM | Where-Object {$_.Notes -like 'Test'}).Name -join '-v ')
Theo
  • 57,719
  • 8
  • 24
  • 41