【发布时间】:2021-05-27 01:56:47
【问题描述】:
我在 winform 中使用 Az 模块来控制 Azure Winform,但在从 winform 执行脚本时出错。
Using objPS As PowerShell = PowerShell.Create()
'Create the Selected Snapshot to VM OS Disk
Dim strPowerShellScriptCreateOsDisk = "$storageType = 'StandardSSD_LRS'
$location = 'Southeast Asia'
$diskSizeGB = 64
$snapshot = Get-AzSnapshot -ResourceGroupName '" & gstrResourceGroupName & "' -SnapshotName '" & gstrSelectedSnapshot & "'
$osDisk = New-AzDisk -DiskName '" & gstrSelectedSnapshot & "_" & gstrVmName & "' -Disk `
(New-AzDiskConfig -AccountType $storageType -DiskSizeGB $diskSizeGB `
-Location $location -CreateOption Copy `
-SourceResourceId $snapshot.Id) `
-ResourceGroupName '" & gstrResourceGroupName & "'"
'Swap the Created VM OS Disk to Virtual Machine
Dim strPowerShellScriptSwapOsDisk = "$vm = Get-AzVM -ResourceGroupName '" & gstrResourceGroupName & "' -Name '" & gstrVmName & "'
$disk = Get-AzDisk -ResourceGroupName '" & gstrResourceGroupName & "' -Name '" & gstrSelectedSnapshot & "_" & gstrVmName & "'
Set-AzVMOSDisk -VM $vm -ManagedDiskId $disk.Id -Name $disk.Name
Update-AzVM -ResourceGroupName '" & gstrResourceGroupName & "' -VM $vm"
'Delete the Replaced Old Disk from Azure Storage
Dim strPowerShellScriptDeleteDisk = "Remove-AzDisk -ResourceGroupName '" & gstrResourceGroupName & "' -DiskName '" & strOldDisk & "' -Force"
objPS.AddScript(strPowerShellScriptConnectAccount + vbNewLine + strPowerShellScriptCreateOsDisk + vbNewLine + strPowerShellScriptSwapOsDisk + vbNewLine + strPowerShellScriptDeleteDisk)
' Check if objResult is Nothing then Ignore
For Each objResult As PSObject In objPS.Invoke()
If objResult IsNot Nothing Then Debug.WriteLine(objResult.ToString())
Next
End Using
它在我的计算机上运行,但是当我将 exe 发布到另一台计算机时,该功能不再起作用。我收到这些错误
在模块“Az.Accounts”中找到了“Disconnect-AzAccount”命令,但无法加载该模块。有关详细信息,请运行“Import-Module Az.Accounts”。
和
在模块“Az.Compute”中找到“Get-AzSnapshot”命令,但无法加载该模块。有关更多信息,请运行“Import-Module Az.Compute”
但我确保目标计算机 Azure powershell、az 模块、.net 核心和 .net 框架已安装和更新。我需要在目标计算机上安装什么操作吗?
【问题讨论】:
-
您使用了
Disconnect-AzAccount命令吗?看起来它不在您提供的脚本中。 -
请检查您脚本中的每一行,您是否使用了旧的
AzureRm命令? -
嗨,检查了所有功能都使用了az模块推荐,并且这个功能在我的电脑上工作,但是当我将exe复制到新环境时,它会在powershell函数中出错。我应该在新环境中安装任何框架吗?我在新环境中安装了 Azure 模块、.net framework 4.7.2、.net core、azure powershell,但仍然没有工作。
标签: winforms azure powershell azure-virtual-machine