The following forum(s) have migrated to Microsoft Q&A (Preview): Azure Virtual Machines!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
Azure Powershell Commend no working, Command was found in the module, but the module could not be loaded RRS feed

  • Question

  • 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 is worked in my computer, but when I publish the exe to another computer, it the function is no working. and I get the error

    (0) = {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'.}

    (1) = {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?

    Friday, November 15, 2019 3:22 AM

All replies

  • Hi,

    In the machine where the exe didnt work, Open the powershell ans execute the below commands and let us know the outputs

    Get-InstalledModule -Name Az*

    This command will list all the installed modules in that machine. Make sure Az.Accounts and Az.Compute modules are there.

    PS C:\Users\jakaruna> Get-InstalledModule -Name Az*
    
    Version    Name                                Repository           Description
    -------    ----                                ----------           -----------
    2.8.0      Az                                  PSGallery            Microsoft Azure PowerShell - Cmdlets to manage resources in Azure. This module is compatible with WindowsPowerShell and PowerShell Core....
    1.6.3      Az.Accounts                         PSGallery            Microsoft Azure PowerShell - Accounts credential management cmdlets for Azure Resource Manager in Windows PowerShell and PowerShell Core....
    1.0.2      Az.Aks                              PSGallery            Microsoft Azure PowerShell - Azure managed Kubernetes cmdlets for Windows PowerShell and PowerShell Core....
    1.1.1      Az.AnalysisServices                 PSGallery            Microsoft Azure PowerShell - Analysis Services cmdlets for Windows PowerShell and PowerShell Core....
    1.3.2      Az.ApiManagement                    PSGallery            Microsoft Azure PowerShell - Api Management service cmdlets for Azure Resource Manager in Windows PowerShell and PowerShell Core....
    1.0.2      Az.ApplicationInsights              PSGallery            Microsoft Azure PowerShell - Application Insights management cmdlets for Azure Resource Manager in Windows PowerShell and PowerShell Core.  Creates and manag...
    1.3.4      Az.Automation                       PSGallery            Microsoft Azure PowerShell - Automation service cmdlets for Azure Resource Manager in Windows PowerShell and PowerShell Core....
    1.1.2      Az.Batch                            PSGallery            Microsoft Azure PowerShell - Batch service cmdlets for Azure Resource Manager in Windows PowerShell and PowerShell Core....
    1.0.1      Az.Billing                          PSGallery            Microsoft Azure PowerShell - Billing service cmdlets for Azure Resource Manager in Windows PowerShell and PowerShell Core....
    1.3.1      Az.Cdn                              PSGallery            Microsoft Azure PowerShell - CDN service cmdlets for Azure Resource Manager in Windows PowerShell and PowerShell Core....
    1.2.1      Az.CognitiveServices                PSGallery            Microsoft Azure PowerShell - Cognitive Services management cmdlets for Azure Resource Manager in Windows PowerShell and PowerShell Core. Creates and manages ...
    2.7.0      Az.Compute                          PSGallery            Microsoft Azure PowerShell - Compute service cmdlets for Azure Resource Manager in Windows PowerShell and PowerShell Core.  Manages virtual machines, hosted

    Also let me know the version of the Az module.

    If the modules are there, Then execute "Get-AzSnapshot" on powershell and check the result.

    if it errors out, Then try importing the module using import-module Az

    Try this out and let me know.

    Friday, November 15, 2019 8:20 AM
  • hi , "Get-AzSnapshot"  it is worked in the target computer powershell, and I try all commned is work if I direct  execute in powershell. But in winform  PowerShell.Create() the function will error 


    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. 


    In my develop computer same winform is worked, but in new environment powershell function will error. But I check the powershell version, and az module is installed in target computer.


    • Edited by chauchun Friday, November 15, 2019 8:33 AM
    Friday, November 15, 2019 8:33 AM
  • Version    Name                                Repository           Description
    -------    ----                                ----------           -----------
    1.6.4      Az.Accounts                         PSGallery            Microsoft Azure PowerShell - Accounts credential...
    1.1.0      Az.Advisor                          PSGallery            Microsoft Azure PowerShell - Azure Advisor Cmdle...
    1.0.2      Az.Aks                              PSGallery            Microsoft Azure PowerShell - Azure managed Kuber...
    1.1.1      Az.AnalysisServices                 PSGallery            Microsoft Azure PowerShell - Analysis Services c...
    1.3.2      Az.ApiManagement                    PSGallery            Microsoft Azure PowerShell - Api Management serv...
    1.0.2      Az.ApplicationInsights              PSGallery            Microsoft Azure PowerShell - Application Insight...
    1.3.4      Az.Automation                       PSGallery            Microsoft Azure PowerShell - Automation service ...
    2.0.1      Az.Batch                            PSGallery            Microsoft Azure PowerShell - Batch service cmdle...
    1.0.1      Az.Billing                          PSGallery            Microsoft Azure PowerShell - Billing service cmd...
    1.4.0      Az.Cdn                              PSGallery            Microsoft Azure PowerShell - CDN service cmdlets...
    1.2.1      Az.CognitiveServices                PSGallery            Microsoft Azure PowerShell - Cognitive Services ...
    3.0.0      Az.Compute                          PSGallery            Microsoft Azure PowerShell - Compute service cmd...
    1.0.1      Az.ContainerInstance                PSGallery            Microsoft Azure PowerShell - Container Instance ...
    1.1.0      Az.ContainerRegistry                PSGallery            Microsoft Azure PowerShell - Container Registry ...
    1.4.1      Az.DataFactory                      PSGallery            Microsoft Azure PowerShell - Data Factory servic...
    1.0.1      Az.DataLakeAnalytics                PSGallery            Microsoft Azure PowerShell - Data Lake Analytics...
    1.2.4      Az.DataLakeStore                    PSGallery            Microsoft Azure PowerShell - Azure Data Lake Sto...
    1.0.1      Az.DeploymentManager                PSGallery            PowerShell .Net Core Microsoft Azure PowerShell ...
    1.0.0      Az.DevTestLabs                      PSGallery            Microsoft Azure PowerShell - DevTest Labs servic...
    1.1.1      Az.Dns                              PSGallery            Microsoft Azure PowerShell - DNS service cmdlets...
    1.2.2      Az.EventGrid                        PSGallery            Microsoft Azure PowerShell - Event Grid service ...
    1.4.0      Az.EventHub                         PSGallery            Microsoft Azure PowerShell - Event Hubs service ...
    1.1.2      Az.FrontDoor                        PSGallery            Microsoft Azure PowerShell - Front Door service ...
    3.0.0      Az.HDInsight                        PSGallery            Microsoft Azure PowerShell - HDInsight service c...
    1.0.0      Az.HealthcareApis                   PSGallery            Microsoft Azure PowerShell - HealthcareApis mana...
    2.0.0      Az.IotHub                           PSGallery            Microsoft Azure PowerShell - IoT Hub service cmd...
    1.3.1      Az.KeyVault                         PSGallery            Microsoft Azure PowerShell - Key Vault service c...
    1.3.1      Az.LogicApp                         PSGallery            Microsoft Azure PowerShell - Logic Apps cmdlets ...
    1.1.1      Az.MachineLearning                  PSGallery            Microsoft Azure PowerShell - Machine Learning We...
    1.0.1      Az.ManagedServices                  PSGallery            Microsoft Azure PowerShell - ManagedServices cmd...
    1.0.1      Az.MarketplaceOrdering              PSGallery            Microsoft Azure PowerShell - Marketplace Orderin...
    1.1.0      Az.Media                            PSGallery            Microsoft Azure PowerShell - Media service cmdle...
    1.4.0      Az.Monitor                          PSGallery            Microsoft Azure PowerShell - Monitor service cmd...
    2.0.0      Az.Network                          PSGallery            Microsoft Azure PowerShell - Networking service ...
    1.1.0      Az.NotificationHubs                 PSGallery            Microsoft Azure PowerShell - Notification Hubs c...
    1.3.3      Az.OperationalInsights              PSGallery            Microsoft Azure PowerShell - Operational Insight...
    1.1.3      Az.PolicyInsights                   PSGallery            Microsoft Azure PowerShell - Azure Policy Insigh...
    1.1.0      Az.PowerBIEmbedded                  PSGallery            Microsoft Azure PowerShell - Power BI Embedded s...
    1.0.0      Az.PrivateDns                       PSGallery            Microsoft Azure PowerShell - Private DNS service...
    2.0.1      Az.RecoveryServices                 PSGallery            Microsoft Azure PowerShell - Recovery Services c...
    1.1.1      Az.RedisCache                       PSGallery            Microsoft Azure PowerShell - Redis Cache service...
    1.0.2      Az.Relay                            PSGallery            Microsoft Azure PowerShell - Relay service cmdle...
    1.7.1      Az.Resources                        PSGallery            Microsoft Azure PowerShell - Azure Resource Mana...
    1.4.0      Az.ServiceBus                       PSGallery            Microsoft Azure PowerShell - Service Bus service...
    3.0.0      Az                                  PSGallery            Microsoft Azure PowerShell - Cmdlets to manage r...
    2.0.0      Az.ServiceFabric                    PSGallery            Microsoft Azure PowerShell - Service Fabric cmdl...
    1.1.0      Az.SignalR                          PSGallery            Microsoft Azure PowerShell - Azure SignalR servi...
    2.0.0      Az.Sql                              PSGallery            Microsoft Azure PowerShell - SQL service cmdlets...
    1.9.0      Az.Storage                          PSGallery            Microsoft Azure PowerShell - Storage service dat...
    1.2.1      Az.StorageSync                      PSGallery            Microsoft Azure PowerShell - Storage Sync cmdlet...
    1.0.0      Az.StreamAnalytics                  PSGallery            Microsoft Azure PowerShell - Stream Analytics se...
    1.0.2      Az.TrafficManager                   PSGallery            Microsoft Azure PowerShell - Traffic Manager ser...
    1.5.0      Az.Websites                         PSGallery            Microsoft Azure PowerShell - App Service (Web Ap...


    • Edited by chauchun Friday, November 15, 2019 8:35 AM
    Friday, November 15, 2019 8:35 AM
  • ok got it.

    Can you try adding "import-module Az" in the powershell code in winform  and check?

    Friday, November 15, 2019 9:39 AM
  • VERBOSE: Loading module from path 'C:\Users\VM User\Documents\WindowsPowerShell\Modules\Az\3.0.0\Az.psd1'.
    VERBOSE: Populating RepositorySourceLocation property for module Az.
    VERBOSE: Loading module from path 'C:\Users\VM User\Documents\WindowsPowerShell\Modules\Az\3.0.0\Az.psm1'.

    and I try again still cant work, should I installed any framework to computer?
    Friday, November 15, 2019 10:08 AM
  • Hi,

    Is that issue resolved?

    Existing frameworks should work well because in powershell, Commands are working fine.

    Are you running as the same user for the exe and commands in powershell as well

    Tuesday, November 19, 2019 8:30 AM
  • Hello,

    Any update on the issue?

    If the suggested response helped you resolve your issue, do click on "Mark as Answer" and "Up-Vote" on the answer that helped you for benefit of community.

    Thanks.

    Tuesday, November 26, 2019 10:41 AM
  • Hello,

    Any update on the issue?

    Thanks.

    Friday, November 29, 2019 8:40 AM