azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

Cannot upload Powershell 7.1 module with Set-AzAutomationModule

Open silverl opened this issue 3 years ago • 15 comments

Description

I can't figure out how to use Set-AzAutomationModule to upload a module that targets Powershell 7.1.

Our module .psd1 contains:

PowerShellVersion = '7.1'
CompatiblePSEditions = 'Core'

Modules uploaded using Set-AzAutomationModule are showing up in the module list targeting 5.1 instead of 7.1.

I don't see any cmdlet parameters that would be relevant to specifying the target platform.

Is there some way to make this work?

Issue script & Debug output

Irrelevant

Environment data

Name                           Value
----                           -----
PSVersion                      7.2.0
PSEdition                      Core
GitCommitId                    7.2.0
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Module versions

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     2.6.1                 Az.Accounts                         {Add-AzEnvironment, Clear-AzContext, Clear-AzDefault, Connect-AzAccount…}
Script     1.7.1                 Az.Automation                       {Export-AzAutomationDscConfiguration, Export-AzAutomationDscNodeReportContent, Export-AzAutomationRunbook, Get-AzAutomationAccount…}

Error output

Irrelevant

silverl avatar Nov 13 '21 23:11 silverl

Thank you for your reporting. Tagging and routing to the team member best able to assist.

dingmeng-xue avatar Nov 15 '21 02:11 dingmeng-xue

@silverl Support for 7.1 is in preview and currently uses a separate API for managing modules via the portal. The existing PowerShell cmdlets are not updated to support them.

You could however make leverage the Invoke-AzRestMethod cmdlet to perform the same calls as the portal in the meantime.

PramodValavala-MSFT avatar Nov 17 '21 16:11 PramodValavala-MSFT

I see. Is implementing support for 7.1 on the roadmap? Any ETA on adding that to the PowerShell cmdlets?

Do we leave this open until it's resolved?

silverl avatar Nov 19 '21 19:11 silverl

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @jaspkaur28.

Issue Details

Description

I can't figure out how to use Set-AzAutomationModule to upload a module that targets Powershell 7.1.

Our module .psd1 contains:

PowerShellVersion = '7.1'
CompatiblePSEditions = 'Core'

Modules uploaded using Set-AzAutomationModule are showing up in the module list targeting 5.1 instead of 7.1.

I don't see any cmdlet parameters that would be relevant to specifying the target platform.

Is there some way to make this work?

Issue script & Debug output

Irrelevant

Environment data

Name                           Value
----                           -----
PSVersion                      7.2.0
PSEdition                      Core
GitCommitId                    7.2.0
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Module versions

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     2.6.1                 Az.Accounts                         {Add-AzEnvironment, Clear-AzContext, Clear-AzDefault, Connect-AzAccount…}
Script     1.7.1                 Az.Automation                       {Export-AzAutomationDscConfiguration, Export-AzAutomationDscNodeReportContent, Export-AzAutomationRunbook, Get-AzAutomationAccount…}

Error output

Irrelevant
Author: silverl
Assignees: PramodValavala-MSFT
Labels:

Automation, Service Attention, bug, question, customer-reported, CXP Attention

Milestone: -

msftbot[bot] avatar Nov 22 '21 16:11 msftbot[bot]

@jaspkaur28 Could you share insights on any ETA for the above?

PramodValavala-MSFT avatar Nov 22 '21 16:11 PramodValavala-MSFT

@silverl Support for 7.1 is in preview and currently uses a separate API for managing modules via the portal. The existing PowerShell cmdlets are not updated to support them.

You could however make leverage the Invoke-AzRestMethod cmdlet to perform the same calls as the portal in the meantime.

Hi, I stumbled upon this thread for same reasons (Automation and managed identity). How do I use the Invoke-AzRestMethod for this purpose specifically?

OfficeUnlimited avatar Nov 30 '21 12:11 OfficeUnlimited

What is the apiversion I need to specify?

OfficeUnlimited avatar Nov 30 '21 13:11 OfficeUnlimited

Is this the endpoint https://docs.microsoft.com/en-us/rest/api/automation/module/create-or-update

If so, I'm not sure I see any argument that specifies the target runtime.

silverl avatar Nov 30 '21 20:11 silverl

@silverl @OfficeUnlimited There seems to be a separate endpoint which is like the one above except that instead of modules it is powershell7Modules. You can check the requests being sent by the browser when performing these operations from the portal.

The API Version used by the portal is 2019-06-01.

Here is a sample request using Invoke-AzRestMethod

Invoke-AzRestMethod `
     -Method PUT `
     -SubscriptionId $subscriptionId `
     -ResourceGroupName $rgName `
     -ResourceProviderName Microsoft.Automation `
     -ResourceType automationAccounts `
     -Name $aaName/powershell7Modules/PSKubectl `
     -ApiVersion 2019-06-01 `
     -Payload '{"properties":{"contentLink":{"uri":"https://devopsgallerystorage.blob.core.windows.net/packages/pskubectl.0.13.0.nupkg"}}}'

@jaspkaur28 Could you share more insights here?

PramodValavala-MSFT avatar Dec 10 '21 09:12 PramodValavala-MSFT

@PramodValavala-MSFT @silverl - Currently PS7.1 is only supported through the portal. API changes for ARM and PS cmdlets are not yet implemented. We are tracking this as part of PS7.1 GA. Tentative ETA is early Q2 CY22.

jaspkaur28 avatar Jan 03 '22 12:01 jaspkaur28

@jaspkaur28 Thanks for the update! I am reassigning this issue to you to share updates as you know more.

@silverl I hope the workaround should suffice until then.

PramodValavala-MSFT avatar Jan 04 '22 01:01 PramodValavala-MSFT

The workaround will do, thank you.

silverl avatar Jan 04 '22 15:01 silverl

Used the suggestion from @PramodValavala-MSFT and got it working. Below is a mostly complete example, minus error handling and filling in all the variable values:

Invoke-AzRestMethod -Method "PUT" -ResourceGroupName $ResourceGroupName -ResourceProviderName "Microsoft.Automation" `
	-ResourceType "automationAccounts" -Name "${AutomationAccountName}/runbooks/${RunbookName}" -ApiVersion "2017-05-15-preview" `
	-Payload "{`"properties`":{`"runbookType`":`"PowerShell7`", `"logProgress`":false, `"logVerbose`":false, `"draft`":{}}, `"location`":`"${Location}`"}"

Invoke-AzRestMethod -Method "PUT" -ResourceGroupName $ResourceGroupName -ResourceProviderName "Microsoft.Automation" `
	-ResourceType automationAccounts -Name "${AutomationAccountName}/runbooks/${RunbookName}/draft/content" -ApiVersion 2015-10-31 `
	-Payload "$scriptContent"

Publish-AzAutomationRunbook -Name $RunbookName -AutomationAccountName $AutomationAccountName -ResourceGroupName $ResourceGroupName

fullmetalcache avatar Mar 11 '22 15:03 fullmetalcache

Update on PS7.1 GA that's mentioned in one of the above comment: The tentative ETA is moved to Q4 CY22 as service is undergoing some of the major architecture level changes.

KrishnaG-MSFT avatar Mar 25 '22 06:03 KrishnaG-MSFT

Used the suggestion from @PramodValavala-MSFT and got it working. Below is a mostly complete example, minus error handling and filling in all the variable values:

Invoke-AzRestMethod -Method "PUT" -ResourceGroupName $ResourceGroupName -ResourceProviderName "Microsoft.Automation" `
   -ResourceType "automationAccounts" -Name "${AutomationAccountName}/runbooks/${RunbookName}" -ApiVersion "2017-05-15-preview" `
   -Payload "{`"properties`":{`"runbookType`":`"PowerShell7`", `"logProgress`":false, `"logVerbose`":false, `"draft`":{}}, `"location`":`"${Location}`"}"

Invoke-AzRestMethod -Method "PUT" -ResourceGroupName $ResourceGroupName -ResourceProviderName "Microsoft.Automation" `
   -ResourceType automationAccounts -Name "${AutomationAccountName}/runbooks/${RunbookName}/draft/content" -ApiVersion 2015-10-31 `
   -Payload "$scriptContent"

Publish-AzAutomationRunbook -Name $RunbookName -AutomationAccountName $AutomationAccountName -ResourceGroupName $ResourceGroupName

@fullmetalcache ; please, how the heck do i send a PowerShell JSON payload? what was your $scriptContent?

fluffy-cakes avatar Aug 02 '22 12:08 fluffy-cakes

@fluffy-cakes The $scriptContent can be anything. As simple as

$scriptContent = "Write-Output 'aa' `
   Write-Output 'bb'"

Jassingh819 avatar Sep 26 '22 05:09 Jassingh819

The workaround has been provided in this PR

SnehaSudhirG avatar Oct 26 '22 06:10 SnehaSudhirG

#please-close

SnehaSudhirG avatar Oct 26 '22 06:10 SnehaSudhirG

@SnehaSudhirG ; what PR? i don't see any link. this is an issue, not a PR

fluffy-cakes avatar Oct 26 '22 07:10 fluffy-cakes

Hi All,

I would like to see, that we can manage the Powershell Modules with the AZ PowerShell Module.

Until then, here is how you can Manage the PowerShell 7 Modules until then https://blog.icewolf.ch/archive/2022/11/19/how-to-manage-powershell-5-and-7-modules-on-azure.aspx

Regards Andres

BohrenAn avatar Nov 19 '22 11:11 BohrenAn

Any updates on this? We still cannot create/update PowerShell 7 runbooks using Bicep

mzaatar avatar Jan 18 '23 07:01 mzaatar

Any news ?

ghost avatar Feb 02 '23 07:02 ghost

like to know the update as well, need powershell7 module feature via bicep/ARM

ksooner avatar Feb 24 '23 10:02 ksooner

We are now in Q2 of 2023. Is there any update on full PowerShell 7 support in run books, including PS CmdLets/ARM template support to manage these?

HRading avatar Apr 03 '23 09:04 HRading

How can this be closed when it seems like 7.1 modules aren't being handled at all either via the portal or powershell.

hippygold42 avatar Apr 26 '23 13:04 hippygold42

For those that have a need to create a runbook with PowerShell 7.2:

Invoke-AzRestMethod -Method "PUT" -ResourceGroupName $resourceGroupName -ResourceProviderName "Microsoft.Automation" `
            -ResourceType "automationAccounts" -Name "${AutomationAccountName}/runbooks/${RunbookName}" -ApiVersion 2022-06-30-preview `
            -Payload "{`"properties`":{`"runbookType`":`"PowerShell`",`"runtime`":`"PowerShell-7.2`", `"logProgress`":false, `"logVerbose`":false, `"draft`":{}}, `"location`":`"$($location)`"}"

ChrisNotABot avatar Jun 20 '23 07:06 ChrisNotABot

Seems like you can't get this to work for modifying the runtime version of Python runbooks. Source control syncing is great, the inability to easily control runtime versioning is frustrating. Try to set to Python-3.8 get error signaling can only be set to 3.10, set to 3.10 get error that it can't be modified. ¯_(ツ)_/¯

Content : {"code":"BadRequest","message":"Runtime property is invalid for runbookType Python. Supported values are - Python-3.10."}

Content : {"code":"BadRequest","message":"Runbook Language cannot be modified."}

Manually create runbook without source control - NO PROBLEMS

USCSseble avatar Jun 30 '23 14:06 USCSseble

Hello everyone, I managed to upload some 7.2 modules and here is the construct that helped me upload modules for 5.1, 7.1 & 7.2 for anyone whom this can help.

$AutomationAccountName = 'Your-AACC-for-modules'

$createRunbookParam = @{
  Method               = "PUT" 
  ResourceGroupName    = 'Your-RG-With-AAcc'
  ResourceProviderName = "Microsoft.Automation"
  ResourceType         = "automationAccounts" 
}

$moduleInfo = @{
  Name                   = 'Microsoft.Graph.Authentication'
  Version                = '2.1.0'
  PSVersion              = '7.2'
  VersionIndependentLink = 'https://www.powershellgallery.com/api/v2/package/Microsoft.Graph.Authentication'
}

$moduleEndpointEnum = @{
  '7.1' = 'powershell7Modules'
  '7.2' = 'powershell7Modules'
  '5.1' = 'Modules'
}

# payload with uri for module from gallery
$modulePayload = @{ 
  properties = @{ 
    contentLink = @{ 
      uri = "$($moduleInfo.VersionIndependentLink)/$($moduleInfo.Version)" 
    } 
  } 
} | ConvertTo-Json -Compress

# version of API that enables 7.1 as default and runtimeVersion uri variable for 7.2
$createRunbookParam.ApiVersion = '2019-06-01' 
if($moduleEndpointEnum[$moduleInfo.PSVersion]){ $createRunbookParam.ApiVersion += '&runtimeVersion=7.2' }

# 'Your-RG-With-AAcc/powershell7Modules/Microsoft.Graph.Authentication?ApiVersion=2019-06-01&runtimeVersion=7.2'
Invoke-AzRestMethod @createRunbookParam -Name "${AutomationAccountName}/$($moduleEndpointEnum[$moduleInfo.PSVersion])/$($moduleInfo.Name)" -Payload $modulePayload

VLoub avatar Aug 04 '23 07:08 VLoub

I'm sharing back my set of PowerShell functions for uploading either 7.1 or 7.2 Runbooks. Hat tip to @fullmetalcache, @ChrisNotABot and @PramodValavala-MSFT, thank you.

function Write-RestError {
    param (
        $result
    )
    Write-Output "Error creating runbook`n"
    $result

    # Convert the response content to a PowerShell object and write it
    $parsedContent = $result.Content | ConvertFrom-Json
    Write-Output "Code: $($parsedContent.code)"
    Write-Output "Message: $($parsedContent.message)"
    throw $result
}
function Publish-Runbook {
    param (
        [parameter(Mandatory = $true)]
        [string] $RunbookPath,
        [parameter(Mandatory = $true)]
        [string] $ResourceGroupName,
        [parameter(Mandatory = $true)]
        [string] $AutomationAccountName,
        [string] $Location,
        [parameter(Mandatory = $true)]
        [ValidateSet("PowerShell-7.1", "PowerShell-7.2")]
        [string] $RunbookType
    )

    $ErrorActionPreference = "Stop"

    # Load the file into a variable as a string
    $scriptContent = Get-Content -Path $RunbookPath -Raw

    # Get the name of the runbook from the filename portion of the $Path without the extenions.
    $runbookName = Get-RunbookNameFromPath($RunbookPath)

    # This call creates the runbook if it doesn't exist.
    if ($RunbookType -eq "PowerShell-7.1") {
        $result = Invoke-AzRestMethod -Method "PUT" -ResourceGroupName $ResourceGroupName -ResourceProviderName "Microsoft.Automation" `
        -ResourceType "automationAccounts" -Name "${AutomationAccountName}/runbooks/${RunbookName}" -ApiVersion "2019-06-01" `
        -Payload "{`"properties`":{`"runbookType`":`"PowerShell7`", `"logProgress`":false, `"logVerbose`":false, `"draft`":{}}, `"location`":`"${Location}`"}"

    }
    else {
        $result = Invoke-AzRestMethod -Method "PUT" -ResourceGroupName $ResourceGroupName -ResourceProviderName "Microsoft.Automation" `
        -ResourceType "automationAccounts" -Name "${AutomationAccountName}/runbooks/${RunbookName}" -ApiVersion "2022-06-30-preview" `
        -Payload "{`"properties`":{`"runbookType`":`"PowerShell`", `"runtime`":`"PowerShell-7.2`", `"logProgress`":false, `"logVerbose`":false, `"draft`":{}}, `"location`":`"${Location}`"}"
    }

    if ($result.StatusCode -gt 202) {
        Write-RestError -result $result
    }

    # This call creates the draft content of the runbook.
    $result = Invoke-AzRestMethod -Method "PUT" -ResourceGroupName $ResourceGroupName -ResourceProviderName "Microsoft.Automation" `
        -ResourceType automationAccounts -Name "${AutomationAccountName}/runbooks/${RunbookName}/draft/content" -ApiVersion "2019-06-01" `
        -Payload "$scriptContent"

    if ($result.StatusCode -gt 202) {
        Write-RestError -result $result
    }

    # This call publishes the runbook.
    Publish-AzAutomationRunbook -Name $runbookName -AutomationAccountName $AutomationAccountName -ResourceGroupName $ResourceGroupName

    Write-Output "Successfully published runbook: $runbookName`n"
}

function Get-RunbookNameFromPath {
    param (
        [string] $Path
    )

    # Get the name of the runbook from the filename portion of the $Path without the extenions.
    return (Get-Item $Path).Name.Replace(".ps1", "")
}

function Publish-AllRunbooks {
    param (
        # Validate that $Path is a folder that exists.
        [parameter(Mandatory = $true)]
        [ValidateScript({ Test-Path $_ -PathType Container })]
        [string] $Path,
        [parameter(Mandatory = $true)]
        [ValidateSet("PowerShell-7.1", "PowerShell-7.2")]
        [string] $RunbookType
    )
    # Get a list of all Runbooks matching only *.ps1 files.
    $ErrorActionPreference = "Stop"
    $files = Get-ChildItem -Path $Path -Recurse -Filter *.ps1

    # Append successes to array named $successes
    # Append failures to array named $failures
    $successes = @()
    $failures = @()

    foreach ($file in $files) {
        $runbookName = Get-RunbookNameFromPath($file.FullName)

        Write-Output "----------------------------------------"
        Write-Output "Publishing runbook $runbookName`nResult:"
        try {
            Publish-Runbook -RunbookPath $file.FullName -RunbookType $RunbookType
            $successes += $runbookName
        }
        catch {
            Write-Host "Failed!" -ForegroundColor Red
            $failures += $runbookName
        }
    }

    # Output the successes first and then the failures so they'll be seen.
    Write-Host "Successes:" -ForegroundColor Green
    $successes | ForEach-Object { Write-Host $_ -ForegroundColor Green }

    # Write the failures in red.
    Write-Host "Failures:" -ForegroundColor Red
    $failures | ForEach-Object { Write-Host $_ -ForegroundColor Red }
}

silverl avatar Aug 23 '23 21:08 silverl

For those that have a need to create a runbook with PowerShell 7.2:

Invoke-AzRestMethod -Method "PUT" -ResourceGroupName $resourceGroupName -ResourceProviderName "Microsoft.Automation" `
            -ResourceType "automationAccounts" -Name "${AutomationAccountName}/runbooks/${RunbookName}" -ApiVersion 2022-06-30-preview `
            -Payload "{`"properties`":{`"runbookType`":`"PowerShell`",`"runtime`":`"PowerShell-7.2`", `"logProgress`":false, `"logVerbose`":false, `"draft`":{}}, `"location`":`"$($location)`"}"

@ChrisNotABot where did you find that 2022-06-30-preview api documented?

silverl avatar Aug 23 '23 21:08 silverl