在工作簿中创建哈希表

问题描述 投票:0回答:1

我正在尝试在工作簿中创建一个哈希表,我已经在Parrallel中运行了一个自动化帐户。

我用这段代码试图实现的是输出什么机器执行什么动作以及在什么时间。我不是百分百肯定这会在工作流程中起作用,但我一直在努力试一试。希望得到一些帮助,了解我哪里出错了。

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
powershell azure azure-automation
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.