我试图在工作簿中创建哈希表,我已经在Parrallel中运行了自动化帐户。
我试图通过这段代码实现的是输出什么机器执行了什么动作以及在什么时间执行。我并非100%确定这可以在工作流程中发挥作用,但我一直试图试一试。希望得到一些帮助,了解我哪里出错了。
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
答案 0 :(得分:1)
您可以使用这样的哈希,对于您最好使用来自VM对象的Azure ID的密钥,因为它是唯一的。
$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)