1
votes

I can run the below command without any problem:

Invoke-Command -ScriptBlock {
    Param($rgName,$VMname)
    Get-AzureRmVM -Name $VMname -ResourceGroupName $rgName
} -ArgumentList $rgname,$vmname

But what I really need is to be able to run the command as a job, so I tried the below:

Invoke-Command -ScriptBlock {
    Param($rgName,$VMname)
    Get-AzureRmVM -Name $VMname -ResourceGroupName $rgName
} -ArgumentList $rgname,$vmname -AsJob

And I'm receiving the following error:

Invoke-Command : Parameter set cannot be resolved using the specified named
parameters.
At line:1 char:1
+ Invoke-Command -ScriptBlock { param($rgName,$VMname) Get-AzureRmVM -N ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand

I've also tried to run the command using Start-Job instead but I'm also receiving errors.

1

1 Answers

1
votes

You have to pass the argumentlist as an array

[Array]$ArgumentList = @('rg','vm')

[ScriptBlock]$ScriptBlock = {
  Param(
    [string]$rgName,
    [string]$VMname
  )
  Get-AzureRmVM -Name $VMname -ResourceGroupName $rgName   
}

$nj = Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $ArgumentList -ComputerName $env:COMPUTERNAME -Credential (Get-Credential) -JobName 'NewJobName' |Wait-Job

Interestingly, I found that I had to specify -ComputerName [-Credntial] to get this to work - modified above.

Get the results of the job as follows...

$nj | Receive-Job