Aya*_*ick 5 powershell workflow virtual-machine azure
我正在尝试通过工作流部署Azure VM,以便可以并行完成.代码在工作流之外工作正常.但是在尝试通过工作流程时遇到此错误.
我正在通过csv文件导入VM参数.是否有通过工作流部署Azure VM的其他注意事项?
Workflow Deploy-VMs {
$cred1= New-Object System.Management.Automation.PSCredential "User",$(ConvertTo-SecureString "Password" -asplaintext -force)
$b=Import-Csv Y:\NLG\vms1.csv -Verbose|? type -eq 'VM'
foreach ($c in $b) {
AzureRM.Resources\Login-AzureRmAccount -Credential $cred1 -SubscriptionId subscription id
$nic = New-AzureRmNetworkInterface -Name $c.Name -ResourceGroupName nlg -Location $c.Location -SubnetId $c.SubnetID
$cred= New-Object System.Management.Automation.PSCredential "nladmin",$(ConvertTo-SecureString $c.Password -asplaintext -force)
$vmConfig = New-AzureRmVMConfig -VMName $c.Name -VMSize "Standard_D1"
$vmConfig = Set-AzureRmVMOperatingSystem -VM $vmConfig -Windows -ComputerName $c.Name -Credential $cred
$vmConfig = Set-AzureRmVMSourceImage -VM $vmConfig -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2012-R2-Datacenter-smalldisk" -Version "latest"
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id
$vmConfig = Set-AzureRmVMOSDisk -VM $vmConfig -Name $c.Name -CreateOption FromImage
New-AzureRmVM -ResourceGroupName $c.RG -Location $c.Location -VM $vmConfig
}
}
Run Code Online (Sandbox Code Playgroud)
并得到这个错误
无法绑定参数'VM'.无法将值"Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine"转换为"Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine".错误:"无法将类型为"Deserialized.Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine"的"Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine"值转换为"Microsoft.Azure.Commands.Compute.Models". PSVirtualMachine "" + CategoryInfo:InvalidArgument:(:) [Set-AzureRmVMOperatingSystem],ParameterBindingException + FullyQualifiedErrorId:CannotConvertArgumentNoMessage,Microsoft.Azure.Commands.Compute.SetAzureVMOperatingSystemCommand + PSComputerName:[localhost]
使用内联脚本解决不兼容的 cmdlet。
Workflow Deploy-VMs {
$cred1 = New-Object System.Management.Automation.PSCredential "User", $(ConvertTo-SecureString "Password" -AsPlainText -Force)
$b = Import-Csv Y:\NLG\vms1.csv -Verbose|? type -eq 'VM'
foreach -Parallel ($c in $b) {
AzureRM.Resources\Login-AzureRmAccount -Credential $cred1 -SubscriptionId c2d7e81b-ed6a-4de9-a4cd-36e679ec4259
$nic = New-AzureRmNetworkInterface -Name $c.Name -ResourceGroupName nlg -Location $c.Location -SubnetId $c.SubnetID
$cred = New-Object System.Management.Automation.PSCredential "nladmin", $(ConvertTo-SecureString $c.Password -AsPlainText -Force)
InlineScript {
$vmConfig = New-AzureRmVMConfig -VMName $using:c.Name -VMSize "Standard_D1"
$vmConfig = Set-AzureRmVMOperatingSystem -VM $vmConfig -Windows -ComputerName $using:c.Name -Credential $using:cred
$vmConfig = Set-AzureRmVMSourceImage -VM $vmConfig -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2012-R2-Datacenter-smalldisk" -Version "latest"
$vmConfig = Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $using:nic.Id
$vmConfig = Set-AzureRmVMOSDisk -VM $vmConfig -Name $using:c.Name -CreateOption FromImage
New-AzureRmVM -ResourceGroupName $using:c.RG -Location $using:c.Location -VM $vmConfig
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
514 次 |
| 最近记录: |