What am I Missing in PowerShell in Azure Automation that prevents Start-AzureVM from working?

1.6k Views Asked by At

I have the following PS script that runs fine locally:

Get-AzureVM | Where-Object { $_.Name -eq "my-server-selector" }  | select name | ForEach-Object {
    Write-Output $_.Name 
    Start-AzureVM $_.Name $_.Name
}     

In the context of my local PS console, I add my subscription info and the code executes without a problem; all VMs are printed to the output and the servers are started up.

When I move it to the cloud I need to do a few other things, namely, bring the subscription in scope. I do that by creating the credential asset in the portal, adding the account to my script via said credentials, then selecting the correct subscription in the script. I also wrap it in a workflow (there are aspects I intent to parametrize at a later date).

The final code is as follows:

workflow StartServer
{   
    $credential = GetAutomationPSCredential -Name "credential-asset-name" 

    Add-AzureAccount -Credential $credential

    Select-AzureSubscription -SubscriptionName "subscription-name"


    Write-Output "Starting the server."

    Get-AzureVM | Where-Object { $_.Name -Contains "my-server-selector" }  | select name | ForEach-Object {
        Write-Output $_.Name 
        Start-AzureVM $_.Name $_.Name
    }            

    Write-Output "Execution Complete."

}

If I remove the Start-AzureVM command, the workflow runs as expected. I get a listing of all the matching VMs printed out. If I attempt to put the command back in, I get the following error:

Parameter set cannot be resolved using the specified named parameters.

So, things I think I know:

  • the credentials are working as I'm getting the correct list of VMs
  • the subscription is being correctly set, as it's dumped to the output
  • the inner part of the script works on a local powershell console without any changes

Can anyone provide any ideas as to what needs to be done differently in an Azure Automation workflow to get this to work?

2

There are 2 best solutions below

0
On BEST ANSWER

The fix was to be more explicit in the naming of parameters, both in the filter for the Where-Object as well as in the call to Start-AzureVM. I'm not sure why this would make a difference; as I said, the call to write the names of the servers worked without the explicit parameter name, but low and behold, here it works with it set.

The final code of the inner block is as follows:

Get-AzureVM | Where-Object -FilterScript { $_.Name -Contains "my-server-selector" }  | select name | ForEach-Object {
    Write-Output $_.Name 
    Start-AzureVM -ServiceName $_.Name -Name $_.Name
}   

Thanks to @DexterPOSH on Twitter for the direction on -FilterScript.

0
On

Please take a look at http://azure.microsoft.com/blog/2014/11/25/introducing-the-azure-automation-script-converter/ which talks about this exact issue. When authoring Powershell in the ISE to move into Azure Automation, make sure you are testing / writing as Powershell Workflow in the ISE, since Powershell workflow has some differences vs Powershell script.

Or, if you need to take PS script and use it in Azure Automation, make sure you import the script, not copy paste it in. Azure Automation will then convert the PS script to PS Workflow for you. The link above has more details on this.