• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 74
  • Last Modified:

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!
0
Kelly Garcia
Asked:
Kelly Garcia
  • 2
1 Solution
 
Jose Gabriel Ortega CCEO J0rt3g4 Consulting ServicesCommented:
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
 
Jose Gabriel Ortega CCEO J0rt3g4 Consulting ServicesCommented:
Answered
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now