0
votes

I have used Az module in winform to control the Azure Winform but getting error while executing the script from 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

It worked on my computer, but when I publish the exe to another computer, the function is not working anymore. I get these errors

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'.

and

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'

but I make sure the target computer Azure powershell, az module, .net core and .net framework is installed and updated. Is there any action I need to install in the target computer?

2
Did you use the Disconnect-AzAccount command? Looks it isn't in the script you provided.Joy Wang-MSFT
Please check every line in your script, did you use the old AzureRm command?Joy Wang-MSFT
hi, checked all function is using az module commend, and this fucntion is worked in my computer, but when I copy the exe to new environment, it will error in powershell function. Should I install any framework in new environment? I installed Azure modiule, .net framework 4.7.2, .net core, azure powershell in new environment but still no working.Chau Chun

2 Answers

0
votes

This error seems to be happening due to a potential module incompatibility. Please check the possible reasons for the same here: Symptom: Cannot load Az modules in Windows PowerShell. Please run through all the troubleshooting given in the doc to cross-check and reboot your system once all the latest modules are installed.

Additional references:

Please let us know if the issue still persists despite trying the above steps, and we can investigate further. Thanks for your patience!

0
votes

Most of the time the issue is because, the script is not allowed to executed from the machine where its running. If you try Build the command from Powershell ISE, there in the helper tool you can see the option "Run" is disabled. This is because the current policy of the powershell is not initiated with the user, so the commands execution are limited

Try running the below command to solve this problem, once this is not working then the issue could be problem with the modules, try the policy execution at the first place

set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Thanks