【问题标题】:Azure Powershell Command was found in the module, but the module could not be loaded在模块中找到 Azure Powershell 命令,但无法加载模块
【发布时间】: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


【解决方案1】:

此错误似乎是由于潜在的模块不兼容而发生的。请在此处检查相同的可能原因:Symptom: Cannot load Az modules in Windows PowerShell。请完成文档中给出的所有故障排除,以交叉检查并在安装所有最新模块后重新启动系统。

其他参考资料:

尽管尝试了上述步骤,如果问题仍然存在,请告知我们,我们可以进一步调查。感谢您的耐心等待!

【讨论】:

    【解决方案2】:

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

    尝试运行下面的命令来解决这个问题,一旦这不起作用,那么问题可能是模块的问题,首先尝试执行策略

    set-ExecutionPolicy RemoteSigned -Scope CurrentUser

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 2019-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      相关资源
      最近更新 更多