Invoke-Command with Variables

PowerShell script exchange

Invoke-Command with Variables

Veeam Logoby estoeffels » Sat Jan 07, 2012 2:19 am

Ok I'm still a novice but I have a script that will check for new resource pool in vCenter then add new backup job in Veeam

This is just a part of the script that will run INVOKE-COMMAND portion and it is not passing Variables from local to remote session

Just a part of script ((((assume $targetfolder, $crmid, $pool)))) have been correctly supplied locally

$remotesession = New-PSSession -Name VBUserver -ComputerName $VBUserver -credential $cred
invoke-command -computername $VBUserver -credential $cred -Script{
add-pssnapin VeeamPSSnapIn
$vbrserver = Get-VBRServer | Where {$_.Type -eq "Local"}
add-vbrbackupjob $crmid VDDK $vbrserver $targetfolder -objects $pool

Any help would be awesome

Posts: 3
Liked: never
Joined: Wed Jan 04, 2012 11:37 pm
Full Name: Erik Stoeffels

Re: Invoke-Command with Variables

Veeam Logoby Sethbartlett » Sat Jan 07, 2012 5:43 pm

invoke-command doesn't have a -script flag from what I know, it only has scriptblock. Try changing it to scriptblock and see if that works. Also, you could do the following:

Code: Select all
invoke-command -Computer BNRBox -ScriptBlock {add-pssnapin VeeamPSSnapIn;$vbrserver = Get-VBRServer | Where {$_.Type -eq "Local"};add-vbrbackupjob $crmid VDDK $vbrserver $targetfolder -objects $pool}
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
Posts: 282
Liked: 25 times
Joined: Wed Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett

Re: Invoke-Command with Variables

Veeam Logoby ThomasMc » Sat Jan 07, 2012 7:50 pm

This should work;

Code: Select all
$remoteSession = New-PSSession -Name VBUserver -ComputerName $VBUserver -Credential $cred
Invoke-Command -Session $remoteSession -ScriptBlock {
   add-pssnapin VeeamPSSnapIn
   $vbrserver = Get-VBRServer | Where {$_.Type -eq "Local"}
   add-vbrbackupjob $crmid VDDK $vbrserver $targetfolder -objects $pool
} -ArgumentList "$targetfolderValue", "$crmidValue", "$poolValue"

Is this v5 or v6?
Posts: 293
Liked: 17 times
Joined: Wed Apr 13, 2011 12:45 pm
Full Name: Thomas McConnell

Return to PowerShell

Who is online

Users browsing this forum: No registered users and 1 guest