powershell script doesnt work in Azure

Hi All,

The powershell script below doesn't work in Microsoft Azure - The VM's do not start:

$jobs = @()
Foreach ( $VM in Get-AzureRmVM )
{
  if ( $_.Tags.Values -and $_.Tags.Keys -eq 'Start VM' )
  {
    $ResourceGroupName = $vm | select -expandproperty resourcegroupname
    Write-Output "$($VM.Name) is starting"
    $jobs += Start-Job -ScriptBlock { param($VMName, $RscGrp) Start-Azurermvm -Name $VMName -ResourceGroupName $RscGrp -ErrorAction Continue } -ArgumentList $VM.Name, $ResourceGroupName
  }
  Else
  {
    Write-output "Not starting the VM $VM.Name"
  }
} 
$jobs | Wait-Job | Remove-Job -Force

Open in new window


Please help!
Kelly GarciaSenior Systems AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEOCommented:
It's not gonna work like that.
The reason is that within the scriptsblock, you haven't done the login into AzureRmAccount, then when it tries to turn any machine ON or OFF it doesn't have the account within that section (within the job, there are treated as independent thread)

So at this points, there are 2 options:
1. Stick to Async (jobs) but you would need to create another way to login non-interactive like is shown here: https://docs.microsoft.com/en-us/powershell/azure/authenticate-azureps?view=azurermps-5.1.1
2. Do it synchronously, without the use of jobs.

here's the solution 2:
[cmdletbinding()]
param(
    [Parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0)][Validateset("Start","Stop")]$Action
)

Login-AzureRmAccount
Save-AzureRmContext -Path "D:\AzureProfle.json"


#$jobs = @()
$SbStart = {
    [Cmdletbinding()]
    Param(
        [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)]$VMName,
        [Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$true)]$RG
    )
    BEGIN{
       Import-AzureRmContext -Path "D:\AzureProfile.json"
    }
    PROCESS{
        Start-AzureRmVM -Name $VMName -ResourceGroupName $RG
    }
    END{}
}
$SbStop = {
    [Cmdletbinding()]
    Param(
        [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)]$VMName,
        [Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$true)]$RG
    )
    BEGIN{
       Import-AzureRmContext -Path "D:\AzureProfile.json"
    }
    PROCESS{
        Stop-AzureRmVM -Name $VMName -ResourceGroupName $RG -Force
    }
    END{}
}

Foreach ( $VM in (Get-AzureRmVM | select Name,ResourceGroupName,Tags) )
{
    $ResourceGroupName = $vm.ResourceGroupName
    $Name =$vm.Name
    switch($Action){
        "start" {
            Write-Output "The VM with name: $Name in ResourceGroup: $ResourceGroupName is starting"
            Start-AzureRmVM -Name $Name -ResourceGroupName $ResourceGroupName
            #$jobs += Start-Job -ScriptBlock $SbStart -ArgumentList $Name,$ResourceGroupName
        }
        "stop"{
            Write-Output "The VM with name: $Name in ResourceGroup: $ResourceGroupName is being stopped"
            Stop-AzureRmVM -Name $Name -ResourceGroupName $ResourceGroupName -Force
            #$jobs += Start-Job -ScriptBlock $SbStop -ArgumentList $Name,$ResourceGroupName
        }
    }
} 
#$jobs | Wait-Job | Receive-Job | Remove-Job -Force

Open in new window

the solution into 1 is already in my solution it is just commented and it was using a way to login saving the profile into the disk, but when I tried to import it, but it doesn't find the file. So
in here are more options to login with a non-interactive windows (creating a service principal for example)
https://docs.microsoft.com/en-us/powershell/azure/create-azure-service-principal-azureps?view=azurermps-5.1.1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jose Gabriel Ortega CastroCEOCommented:
Answered
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.