winforms - 在模块中找到 Azure Powershell 命令,但无法加载模块

标签 winforms azure powershell azure-virtual-machine

我在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发布到另一台计算机时,该功能不再工作。我收到这些错误

The 'Disconnect-AzAccount' command was found in the module 'Az.Accounts', but the module could not be loaded. For more information, run 'Import-Module Az.Accounts'.

The 'Get-AzSnapshot' command was found in the module 'Az.Compute', but the module could not be loaded. For more information, run 'Import-Module Az.Compute'

但我确保目标计算机 Azure powershell、az 模块、.net core 和 .net Framework 已安装并更新。我需要在目标计算机上安装任何操作吗?

最佳答案

大多数情况下,问题是因为不允许从运行脚本的计算机上执行脚本。 如果您尝试从 Powershell ISE 构建命令,则在帮助程序工具中您可以看到选项“运行”被禁用。这是因为powershell当前的策略不是由用户发起的,所以命令执行受到限制

尝试运行以下命令来解决此问题,一旦不起作用,则问题可能是模块问题,请首先尝试执行策略

set-ExecutionPolicy RemoteSigned -Scope CurrentUser

谢谢

关于winforms - 在模块中找到 Azure Powershell 命令,但无法加载模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58869833/

相关文章:

winforms - 将 argb 转换为字符串

Azure 负载均衡器静态 IP

visual-studio - 从 Visual Studio 将多个项目部署到单个 Azure 虚拟机

scripting - 从批处理中调用 PowerShell,并检索脚本中设置的临时环境变量的新值?

unit-testing - 带和不带 ParameterFilter 的模拟 Get-ADUser

powershell - 从 "other people"证书存储中删除证书

c# - DataGridView keydown 事件在 C# 中不起作用

c# - 我如何修改我输入的记录?

azure - 使用 Fluent API 跟踪异步 Azure 操作

c# - 如何在 mshtml 和 c# 中启用多级撤消/重做