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?
Disconnect-AzAccount
command? Looks it isn't in the script you provided. – Joy Wang-MSFTAzureRm
command? – Joy Wang-MSFT