PowerShell工作流未按预期运行

时间:2018-12-14 10:39:22

标签: azure powershell workflow

我有一个PowerShell脚本,已将其转换为工作流程。该脚本几乎相同,除了需要更改几行才能使其成为工作流。

但是,由于某种原因,它不会像我认为的那样输出,对于这种情况,我绝对一无所知。

原始PS脚本:

$DynatraceTenantID = "asdf"
$DynatraceToken = "asdf"
$DynatraceServer = "asdf"

$TagName = "env"
$TagValue = "dynatrace"

$result_array_altered = @()
$result_array_installed = @()
$result_array_notaltered = @()

$vms = Get-AzureRmResource -TagName $TagName -TagValue $TagValue | Where-Object {$_.ResourceType -like "Microsoft.Compute/virtualMachines"} | Where-Object {$_.Name -inotlike "MyVMx0"}

Foreach ($vm in $vms) {
    $vm0 = Get-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Status
    $vm1 = Get-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name
    $vm2 = $vm1.Extensions
    if ($vm2.Publisher -notcontains "dynatrace.ruxit"){
        if ($vm0.Statuses.DisplayStatus -eq "VM running") {
            Set-AzureRmVmExtension `
                -Name "Dynatrace" `
                -Publisher "dynatrace.ruxit" `
                -ResourceGroupName "$($vm1.ResourceGroupName)" `
                -Location "$($vm1.Location)" `
                -VMName "$($vm1.Name)" `
                -ExtensionType "oneAgentWindows" `
                -TypeHandlerVersion "2.3" `
                -Settings @{ "tenantId"=$DynatraceTenantID; "token"=$DynatraceToken;"server"=$DynatraceServer; }

            $objAltered = New-Object -TypeName PSObject -Property @{
                VMaltered = $($vm1.Name)
                IDaltered = $($vm1.Id)
                }

            $result_array_altered += $objAltered
            } else {
                $objNotAltered = New-Object -TypeName PSObject -Property @{
                    VMnotAltered = $($vm1.Name)
                    IDnotAltered = $($vm1.Id)
                }

                $result_array_notaltered += $objNotAltered
            }
        } else {
            $objInstalled = New-Object -TypeName PSObject -Property @{
                VMinstalled = $($vm1.Name)
                IDinstalled = $($vm1.Id)
            }

            $result_array_installed += $objInstalled
        }
    }

$result_array_altered | ConvertTo-Json

$result_array_installed | ConvertTo-Json

$result_array_notaltered | ConvertTo-Json

输出以下内容:

[
    {
        "IDinstalled":  "/subscriptions/MY-SUBSCRIPTION/resourceGroups/virtual-machines/providers/Microsoft.Compute/virtualMachines/MyVMw0",
        "VMinstalled":  "MyVMw0"
    },
    {
        "IDinstalled":  "/subscriptions/MY-SUBSCRIPTION/resourceGroups/virtual-machines/providers/Microsoft.Compute/virtualMachines/MyVMw1",
        "VMinstalled":  "MyVMw1"
    }
]

将PowerShell转换为PS Workflow:

workflow InstallDynatrace {
    $DynatraceTenantID = "asdf"
    $DynatraceToken = "asdf"
    $DynatraceServer = "asdf"

    $TagName = "env"
    $TagValue = "dynatrace"

    $result_array_altered = @()
    $result_array_installed = @()
    $result_array_notaltered = @()

    $vms = Get-AzureRmResource -TagName $TagName -TagValue $TagValue | Where-Object {$_.ResourceType -like "Microsoft.Compute/virtualMachines"} | Where-Object {$_.Name -inotlike "MyVMx0"}

    Foreach -parallel ($vm in $vms) {
        $vm0 = Get-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Status
        $vm1 = Get-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name
        $vm2 = $vm1.Extensions
        if ($vm2.Publisher -notcontains "dynatrace.ruxit"){
            if ($vm0.Statuses.DisplayStatus -eq "VM running") {
                Set-AzureRmVmExtension `
                    -Name "Dynatrace" `
                    -Publisher "dynatrace.ruxit" `
                    -ResourceGroupName "$($vm1.ResourceGroupName)" `
                    -Location "$($vm1.Location)" `
                    -VMName "$($vm1.Name)" `
                    -ExtensionType "oneAgentWindows" `
                    -TypeHandlerVersion "2.3" `
                    -Settings @{ "tenantId"=$DynatraceTenantID; "token"=$DynatraceToken;"server"=$DynatraceServer; }

                $objAltered = New-Object -TypeName PSObject -Property @{
                    VMaltered = $($vm1.Name)
                    IDaltered = $($vm1.Id)
                    }


                } else {
                    $objNotAltered = New-Object -TypeName PSObject -Property @{
                        VMnotAltered = $($vm1.Name)
                        IDnotAltered = $($vm1.Id)
                    }


                }
            } else {
                $objInstalled = New-Object -TypeName PSObject -Property @{
                    VMinstalled = $($vm1.Name)
                    IDinstalled = $($vm1.Id)
                }


            }
        $workflow:result_array_altered += $objAltered
        $workflow:result_array_notaltered += $objNotAltered
        $workflow:result_array_installed += $objInstalled
        }

    $result_array_altered | ConvertTo-Json

    $result_array_installed | ConvertTo-Json

    $result_array_notaltered | ConvertTo-Json
}

输出以下内容:

PS C:\WINDOWS\system32> InstallDynatrace
[
    {
        "VMnotAltered":  "MyVMw1",
        "IDnotAltered":  "/subscriptions/MY-SUBSCRIPTION/resourceGroups/virtual-machines/providers/Microsoft.Compute/virtualMachines/MyVMw1",
        "PSComputerName":  "localhost",
        "PSShowComputerName":  true,
        "PSSourceJobInstanceId":  "36c288df-41c3-4b61-9a6c-445707f76bea"
    },
    {
        "VMnotAltered":  "MyVMw0",
        "IDnotAltered":  "/subscriptions/MY-SUBSCRIPTION/resourceGroups/virtual-machines/providers/Microsoft.Compute/virtualMachines/MyVMw0",
        "PSComputerName":  "localhost",
        "PSShowComputerName":  true,
        "PSSourceJobInstanceId":  "36c288df-41c3-4b61-9a6c-445707f76bea"
    }
]

请注意原始脚本如何将结果数组显示为VMinstalledIDinstalled,而工作流程显示为VMnotAlteredIDnotAltered,这意味着它将VM视为没有安装Dynatrace(第19行),没有看到它正在运行(第20行),只是将其添加到了Not Alterted阵列中。

有什么想法我要去哪里吗?

已针对THEO回答进行了更新

PS C:\WINDOWS\system32> 
workflow InstallDynatrace {
    $DynatraceTenantID = "asdf"
    $DynatraceToken = "asdf"
    $DynatraceServer = "asdf"

    $TagName = "env"
    $TagValue = "dynatrace"

    $result_array_altered = @()
    $result_array_installed = @()
    $result_array_notaltered = @()

    $vms = Get-AzureRmResource -TagName $TagName -TagValue $TagValue | Where-Object {$_.ResourceType -like "Microsoft.Compute/virtualMachines"} | Where-Object {$_.Name -inotlike "MyVMx0"}

    Foreach -parallel ($vm in $vms) {
        $vm0 = Get-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Status
        $vm1 = Get-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name
        $vm2 = $vm1.Extensions
        if ($vm2.Publisher -ne "dynatrace.ruxit"){
            if ($vm0.Statuses.DisplayStatus -eq "VM running") {
                Set-AzureRmVmExtension `
                    -Name "Dynatrace" `
                    -Publisher "dynatrace.ruxit" `
                    -ResourceGroupName "$($vm1.ResourceGroupName)" `
                    -Location "$($vm1.Location)" `
                    -VMName "$($vm1.Name)" `
                    -ExtensionType "oneAgentWindows" `
                    -TypeHandlerVersion "2.3" `
                    -Settings @{ "tenantId"=$DynatraceTenantID; "token"=$DynatraceToken;"server"=$DynatraceServer; }

                $objAltered = New-Object -TypeName PSObject -Property @{
                    VMaltered = $($vm1.Name)
                    IDaltered = $($vm1.Id)
                    }

                $workflow:result_array_altered += $objAltered
                } else {
                    $objNotAltered = New-Object -TypeName PSObject -Property @{
                        VMnotAltered = $($vm1.Name)
                        IDnotAltered = $($vm1.Id)
                    }

                $workflow:result_array_notaltered += $objNotAltered
                }
            } else {
                $objInstalled = New-Object -TypeName PSObject -Property @{
                    VMinstalled = $($vm1.Name)
                    IDinstalled = $($vm1.Id)
                }

                $workflow:result_array_installed += $objInstalled
            }
        }

    $workflow:result_array_altered | ConvertTo-Json
    $workflow:result_array_installed | ConvertTo-Json
    $workflow:result_array_notaltered | ConvertTo-Json
}

InstallDynatrace
[
    {
        "VMnotAltered":  "MyVMw0",
        "IDnotAltered":  "/subscriptions/asdf/resourceGroups/virtual-machines/providers/Microsoft.Compute/virtualMachines/MyVMw0",
        "PSComputerName":  "localhost",
        "PSShowComputerName":  true,
        "PSSourceJobInstanceId":  "d23d8e5b-dc3c-475f-82fe-968a3eeef946"
    },
    {
        "VMnotAltered":  "MyVMw1",
        "IDnotAltered":  "/subscriptions/asdf/resourceGroups/virtual-machines/providers/Microsoft.Compute/virtualMachines/MyVMw1",
        "PSComputerName":  "localhost",
        "PSShowComputerName":  true,
        "PSSourceJobInstanceId":  "d23d8e5b-dc3c-475f-82fe-968a3eeef946"
    }
]

PS C:\WINDOWS\system32> 

1 个答案:

答案 0 :(得分:0)

恐怕您犯了与之前相同的错误。
在脚本中将对象添加到应 添加到的相应数组中的位置,即在ifelse块中创建对象之后,即在添加的工作流中它们都在循环的末尾。

这意味着不管 if {...} else {...}测试的结果如何,您还是要添加它们。

这应该在正确的位置进行操作:

Foreach -parallel ($vm in $vms) {
    $vm0 = Get-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Status
    $vm1 = Get-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name
    $vm2 = $vm1.Extensions
    if ($vm2.Publisher -notcontains "dynatrace.ruxit"){  # should be -ne I think
        if ($vm0.Statuses.DisplayStatus -eq "VM running") {
            Set-AzureRmVmExtension `
                -Name "Dynatrace" `
                -Publisher "dynatrace.ruxit" `
                -ResourceGroupName "$($vm1.ResourceGroupName)" `
                -Location "$($vm1.Location)" `
                -VMName "$($vm1.Name)" `
                -ExtensionType "oneAgentWindows" `
                -TypeHandlerVersion "2.3" `
                -Settings @{ "tenantId"=$DynatraceTenantID; "token"=$DynatraceToken;"server"=$DynatraceServer; }

            $objAltered = New-Object -TypeName PSObject -Property @{
                VMaltered = $($vm1.Name)
                IDaltered = $($vm1.Id)
                }
            # this is where you add the object to the $workflow:result_array_altered array
            $workflow:result_array_altered += $objAltered
        } 
        else {
            $objNotAltered = New-Object -TypeName PSObject -Property @{
                VMnotAltered = $($vm1.Name)
                IDnotAltered = $($vm1.Id)
            }
            # this is where you add the object to the $workflow:result_array_notaltered array
            $workflow:result_array_notaltered += $objNotAltered
            }
        } 
    else {
        $objInstalled = New-Object -TypeName PSObject -Property @{
            VMinstalled = $($vm1.Name)
            IDinstalled = $($vm1.Id)
        }
        # this is where you add the object to the $workflow:result_array_installed array
        $workflow:result_array_installed += $objInstalled
    }
}

$workflow:result_array_altered    | ConvertTo-Json
$workflow:result_array_notaltered | ConvertTo-Json
$workflow:result_array_installed  | ConvertTo-Json

然后,在工作流程结束时,您输出了早期脚本版本($result_array_altered | ConvertTo-Json等中仍在内存中的阵列。我已对其进行了更改,以为您匹配新的阵列。

而且,$vm2.Publisher不仅仅是一个字符串吗?在这种情况下,更改行

if ($vm2.Publisher -notcontains "dynatrace.ruxit")

进入

if ($vm2.Publisher -ne "dynatrace.ruxit")

({-notcontains检查一个数组中的值,其中-ne测试单个对象的“不相等”,因此在这种情况下为字符串)


更新

我可能已经找到了不需要的额外信息的答案here

它看起来很丑,但是请在工作流程结束时尝试一下:

$workflow:result_array_altered    | Select-Object -Property * -ExcludeProperty PSComputerName, PSShowComputerName, PSSourceJobInstanceId | ConvertTo-Json
$workflow:result_array_installed  | Select-Object -Property * -ExcludeProperty PSComputerName, PSShowComputerName, PSSourceJobInstanceId | ConvertTo-Json
$workflow:result_array_notaltered | Select-Object -Property * -ExcludeProperty PSComputerName, PSShowComputerName, PSSourceJobInstanceId | ConvertTo-Json