I'm trying to create a hash table within a workbook, I've got it running in Parrallel for an automation account.
What I'm trying to achieve with this bit of code is to output what machine performed what action and at what time. I'm not 100% sure this would work in a workflow, but I've been trying to give it a go. Would appreciate some help understanding where I went wrong.
if($shouldStop -eq $true -and $scheduleAllowStop -eq $true){
Write-Output "$($resource.Name) -- STOP --"
$Action = 'STOP'
[int]$TimeTaken = (Measure-command {Stop-AzureRmVm -Name $virtualMachine.Name -ResourceGroup $virtualMachine.ResourceGroupName -Force}).TotalMinutes
}
elseif($shouldStart -eq $true -and $scheduleAllowStart -eq $true){
Write-Output "$($resource.Name) -- START --"
$Action = 'START'
[int]$TimeTaken = (Measure-command{Start-AzureRmVm -Name $virtualMachine.Name -ResourceGroup $virtualMachine.ResourceGroupName}).TotalMinutes
}
else{
$Action = 'IGNORE'
$TimeTaken = 0
Write-Output "$($resource.Name) -- IGNORE --"
}
$result = @{
VMName = $virtualMachine.Name
Action = $Action
TotalMinutes = $TimeTaken
}
$output = New-Object -TypeName PSObject -Property $result
$output | Select-Object VMName, Action, TotalMinutes
You can use a hash like this, for the key you'd be best to use Azure's ID from the VM object as the are unique.
$result = @{}
# Declare the # before you loop through VMs
if($shouldStop -eq $true -and $scheduleAllowStop -eq $true){
Write-Output "$($resource.Name) -- STOP --"
$Action = 'STOP'
[int]$TimeTaken = (Measure-command {Stop-AzureRmVm -Name $virtualMachine.Name -ResourceGroup $virtualMachine.ResourceGroupName -Force}).TotalMinutes
}
elseif($shouldStart -eq $true -and $scheduleAllowStart -eq $true){
Write-Output "$($resource.Name) -- START --"
$Action = 'START'
[int]$TimeTaken = (Measure-command{Start-AzureRmVm -Name $virtualMachine.Name -ResourceGroup $virtualMachine.ResourceGroupName}).TotalMinutes
}
else{
$Action = 'IGNORE'
$TimeTaken = 0
Write-Output "$($resource.Name) -- IGNORE --"
}
$resultObj = [PSCustomObject]@{
VMName = $virtualMachine.Name
Action = $Action
TotalMinutes = $TimeTaken
}
$result.Add($virtualMachine.Name, $resultObj)