locked
Azure PowerShell - Runspaces and Snapshots RRS feed

  • Question

  • Hello! 

    I am developing a script that, when run from within a Runspace, breaks.

    If I do this:

                [System.Collections.ArrayList] $returns = @()
                $osdisk = $vm.StorageProfile.OsDisk
                $dataDisks = $vm.StorageProfile.DataDisks
    
                $sourceDiskSnapshotName = "dmstest$($vm.Name)-C-snapshot"
                $sourceDiskConfig = Get-AzureRMDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $osDisk.Name
                [Microsoft.Azure.Management.Compute.Models.StorageAccountTypes, Microsoft.Azure.Management.Compute, Version=14.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35] $accountType =  $sourceDiskConfig.AccountType
                $SourceDiskSnapshotConfig =  New-AzureRmSnapshotConfig -SourceUri $sourceDiskConfig.Id.ToString() -CreateOption Copy -Location $sourceDiskConfig.Location.ToString() -AccountType $accountType
                $returns.Add((New-AzureRmSnapshot -Snapshot $SourceDiskSnapshotConfig -SnapshotName  $sourceDiskSnapshotName -ResourceGroupName $destRGName))
                "just run new-azurermsnapshot. errors: $Error"


    with $vm populated from a previous command, that's fine. It works as expected.

    The issue happens if I try to run the same commands within a Runspace:

    foreach ($vm in $VMs){ Write-Host "VM Name: $($vm.Name)" $PowerShell = [powershell]::Create() $PowerShell.runspace = [runspacefactory]::CreateRunspace() [void] $PowerShell.AddScript({ [System.Collections.ArrayList] $returns = @() $osdisk = $vm.StorageProfile.OsDisk $dataDisks = $vm.StorageProfile.DataDisks $sourceDiskSnapshotName = "dmstest$($vm.Name)-C-snapshot" $sourceDiskConfig = Get-AzureRMDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $osDisk.Name [Microsoft.Azure.Management.Compute.Models.StorageAccountTypes, Microsoft.Azure.Management.Compute, Version=14.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35] $accountType = $sourceDiskConfig.AccountType $SourceDiskSnapshotConfig = New-AzureRmSnapshotConfig -SourceUri $sourceDiskConfig.Id.ToString() -CreateOption Copy -Location $sourceDiskConfig.Location.ToString() -AccountType $accountType $returns.Add((New-AzureRmSnapshot -Snapshot $SourceDiskSnapshotConfig -SnapshotName $sourceDiskSnapshotName -ResourceGroupName $destRGName)) "just run new-azurermsnapshot. errors: $Error" return $returns[0] })

            $PowerShell.Runspace.Open()
        }

    Notice how I've added ToString() to the New-AzureRMSnapShotConfig cmdlet parameters. That's how it started: that guy started complaining that the parameters weren't of the expected type. And since SourceUri and Location are of type String, the fix was simple.

    It's AccountType I'm having issues with. Its type is System.Nullable`1[[Microsoft.Azure.Management.Compute.Models.StorageAccountTypes, Microsoft.Azure.Management.Compute, Version=14.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] as found here (https://docs.microsoft.com/en-us/powershell/module/azurerm.compute/new-azurermsnapshotconfig?view=azurermps-4.0.0)

    And it's telling me I can't cast it like that. I have tried a few ways, but I haven't gotten it to work yet.

    How can I cast this, or stop the cmdlet from being so picky on the parameters types and infer just like it does when it's not running in the Runspace I create for it?

    Thanks!

    Dimas



    • Edited by Dimas Aguirre Thursday, June 1, 2017 12:44 PM completed foreach statement
    Thursday, June 1, 2017 12:40 PM

Answers

  • I found the right cast

    [System.Nullable`1 
     [Microsoft.Azure.Management.Compute.Models.StorageAccountTypes]  ] $accountType =  $sourceDiskConfig.AccountType

    Simplified reading to notice the right brackets

    [Nullable[StorageAccountTypes]] $accountType =  $sourceDiskConfig.AccountType

    • Marked as answer by Dimas Aguirre Thursday, June 1, 2017 2:16 PM
    Thursday, June 1, 2017 2:16 PM