adbertram / AzureVmSnapshots

A small PowerShell module to automate creating and restoring Azure VM snapshots
12 stars 6 forks source link

Unable to run module in Azure Automation Powershell Runbook #4

Closed asote closed 4 years ago

asote commented 4 years ago

Tried to use the AzureVmSnapshot module in PowerShell runbook but failed.

Code:

`param ( [Parameter(Mandatory = $true)][string]$Subscription, [Parameter(Mandatory = $true)][string]$VMName, [Parameter(Mandatory = $true)][string]$resourceGroupName,
[Parameter(Mandatory = $false)]$storageType = "Premium_LRS"

)

Import-Module Az.Accounts Import-Module Az.Automation Import-Module Az.Compute Import-Module AzureVmSnapshots

Azure Automation connection settings

$connectionName = "AzureRunAsConnection" try {

Get the connection "AzureRunAsConnection "

$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName         

"Logging in to Azure..."
Connect-AzAccount `
    -ServicePrincipal `
    -Subscription $Subscription `
    -TenantId $servicePrincipalConnection.TenantId `
    -ApplicationId $servicePrincipalConnection.ApplicationId `
    -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 

} catch { if (!$servicePrincipalConnection) { $ErrorMessage = "Connection $connectionName not found." throw $ErrorMessage } else { Write-Error -Message $.Exception throw $.Exception } }

OS DISK

New-AzVmSnapshot -VmName $VMName -ResourceGroupName $resourceGroupName

Error message:

Logging in to Azure...

Environments


{[AzureChinaCloud, AzureChinaCloud], [AzureCloud, AzureCloud], [AzureGermanCloud, AzureGermanCloud], [AzureUSGovernme... The 'Get-AzVm' command was found in the module 'Az.Compute', but the module could not be loaded. For more information, run 'Import-Module Az.Compute'.

adbertram commented 4 years ago

This isn't related to the module. The module requires the Az.Compute module. For some reason, that module isn't available in the runbook.

On Tue, Feb 18, 2020 at 11:18 AM Antonio Sotelo notifications@github.com wrote:

Tried to use the AzureVmSnapshot module in PowerShell runbook but failed.

Code:

`param ( [Parameter(Mandatory = $true)][string]$Subscription, [Parameter(Mandatory = $true)][string]$VMName, [Parameter(Mandatory = $true)][string]$resourceGroupName, [Parameter(Mandatory = $false)]$storageType = "Premium_LRS"

)

Import-Module Az.Accounts Import-Module Az.Automation Import-Module Az.Compute Import-Module AzureVmSnapshots

Azure Automation connection settings

$connectionName = "AzureRunAsConnection" try {

Get the connection "AzureRunAsConnection "

$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName

"Logging in to Azure..." Connect-AzAccount -ServicePrincipal -Subscription $Subscription -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationId ` -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint

} catch { if (!$servicePrincipalConnection) { $ErrorMessage = "Connection $connectionName not found." throw $ErrorMessage } else { Write-Error -Message $ .Exception throw $.Exception } }

OS DISK

New-AzVmSnapshot -VmName $VMName -ResourceGroupName $resourceGroupName

Error message:

Logging in to Azure... Environments

{[AzureChinaCloud, AzureChinaCloud], [AzureCloud, AzureCloud], [AzureGermanCloud, AzureGermanCloud], [AzureUSGovernme... The 'Get-AzVm' command was found in the module 'Az.Compute', but the module could not be loaded. For more information, run 'Import-Module Az.Compute'.

  • CategoryInfo : ObjectNotFound: (Get-AzVm:String) [], CommandNotFoundException
  • FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
  • PSComputerName : localhost

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/adbertram/AzureVmSnapshots/issues/4?email_source=notifications&email_token=ABA3P6WK5YZLQW7E6GNLGOLRDQJ6NA5CNFSM4KXI4JV2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IOMLWAA, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABA3P6QM3WYG4REVCR5OV5DRDQJ6NANCNFSM4KXI4JVQ .

--

PB-Enable commented 4 years ago

I can run other Az.Compute cmdlets from a powershell runbook but cannot run the AzureVmSnapshots module. Right above my line to execute New-AzVmSnapshot I did a Get-AzVm -ResourceGroupName $myrg and get output just fine.

Verbose logging shows the following: image

It appears this is due to a limitation in AA Powershell runbooks. They cannot use parallel processing such as start-job.