gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:04:07 25 4
gpt4 key购买 nike

我在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com