Skip to content

Instantly share code, notes, and snippets.

@darrenjrobinson
Created January 5, 2017 10:16
Show Gist options
  • Save darrenjrobinson/b2cb7e5a26065bae8cf76500dbdae851 to your computer and use it in GitHub Desktop.
Save darrenjrobinson/b2cb7e5a26065bae8cf76500dbdae851 to your computer and use it in GitHub Desktop.
# Get the input request
$in = Get-Content $req -Raw | ConvertFrom-Json
write-output "====Mode====" $in.mode
write-output "==Resource Group==" $in.resourcegroup
# Application ID for our Azure Security Principal that we created and provided via Function Application Settings
$username = $env:AzureAutomationAppID
# Password for connection to Azure via Function Application Settings
$pw = $env:AzureAutomationPWD
# Credentials password (encrypted)
$keypath = 'D:\home\site\wwwroot\<YOURFUNCTIONAPP>\keys\AzureAutomationPassKey.key'
$password = $pw | ConvertTo-SecureString -key (Get-Content $keypath)
# Create PS Creds
$credentials = New-Object System.Management.Automation.PSCredential $username,$password
# Load the Invoke-Parallel Powershell Script
. "D:\home\site\wwwroot\<YOURFUNCTIONAPP>\bin\Invoke-Parallel.ps1"
# Import the AzureRM Powershell Module
import-module 'D:\home\site\wwwroot\<YOURFUNCTIONAPP>\bin\AzureRM.profile\2.4.0\AzureRM.Profile.psm1'
# Login
$AzureRMAccount = Add-AzureRmAccount -Credential $credentials -ServicePrincipal -TenantId $env:AzureAutomationTennatID
If ($AzureRMAccount) {
# Import the AzureRM Compute Module
import-module 'D:\home\site\wwwroot\<YOURFUNCTIONAPP>\bin\AzureRM.Compute\2.4.0\AzureRM.Compute.psm1'
$vms = Get-AzureRMVM -ResourceGroupName $in.resourcegroup
$vmrunninglist = @()
$vmstoppedlist = @()
Foreach($vm in $vms)
{
$vmstatus = Get-AzureRMVM -ResourceGroupName $in.resourcegroup -name $vm.name -Status
$PowerState = (get-culture).TextInfo.ToTitleCase(($vmstatus.statuses)[1].code.split("/")[1])
if ($Powerstate -eq 'Running')
{
$vmrunninglist = $vmrunninglist + $vm.name
}
if ($Powerstate -eq 'Deallocated')
{
$vmstoppedlist = $vmstoppedlist + $vm.name
}
}
write-output "====VMs Running====" $vmrunninglist
write-output "====VMs Stopped====" $vmstoppedlist
}
# Started Stopped VMs
if ($in.mode -eq 'start') {
write-output "+++Starting VM's+++" $vmstoppedlist
$output = $vmstoppedlist
$vmstoppedlist | Invoke-Parallel -ImportVariables -NoCloseOnTimeout -RunspaceTimeout 30 -logfile 'D:\home\site\wwwroot\<YOURFUNCTIONAPP>\logs\invoke.log' -ScriptBlock {
Start-AzureRMVM -ResourceGroupName $in.resourcegroup -Name $_ -Verbose }
}
# Stop Started VM's
if ($in.mode -eq 'stop') {
write-output "+++Stopping VM's+++" $vmrunninglist
$output = $vmrunninglist
$vmrunninglist | Invoke-Parallel -ImportVariables -NoCloseOnTimeout -RunspaceTimeout 30 -logfile 'D:\home\site\wwwroot\<YOURFUNCTIONAPP>\logs\invoke.log' -ScriptBlock {
Stop-AzureRMVM -ResourceGroupName $in.resourcegroup -Name $_ -Verbose -Force }
}
Out-File -Encoding Ascii -FilePath $res -inputObject $output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment