У меня есть сценарий 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 to PS:
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"
}
]
Обратите внимание, как оригинальный скрипт показывает, что массив результатов будет VMinstalled
и IDinstalled
, тогда как рабочий процесс отображается как VMnotAltered
иIDnotAltered
, означающее, что на виртуальных машинах не установлено Dynatrace (строка 19), он не работает (строка 20) и просто добавил его в массив неизмененных.
Любые идеи, в которых яне так?
ОБНОВЛЕНО ДЛЯ ТЕО ОТВЕТ
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>