Invoke-AzVMRunCommand Force Powershell Version?

Luke Uhren 191 Reputation points
2021-04-15T22:07:52.823+00:00

Is there a way to force the powershell version on a script ran with the invoke-azvmruncommand to be powershell version 7? I have it installed on the server as I have tested running the script with it and it runs fine, but some of the syntaxes like for azcopy in it do not run properly with pshell 5.1 which the azure runbook kicking off that command will run it as.

Example here from transcript text in the script

Ran locally on powershell 7 it works fine and shows...

Machine: servername (Microsoft Windows NT 6.3.9600.0)
Host Application: C:\Program Files\PowerShell\7\pwsh.dll -WorkingDirectory ~
Process ID: 3464
PSVersion: 7.1.3
PSEdition: Core
GitCommitId: 7.1.3
OS: Microsoft Windows 6.3.9600
Platform: Win32NT
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.10032.0, 6.0.0, 6.1.0, 6.2.0, 7.0.0, 7.1.3

Running from a runbook in azure automation account with the invoke-azvmruncommand

Machine: servername (Microsoft Windows NT 6.3.9600.0)
Host Application: powershell -ExecutionPolicy Unrestricted -File script8.ps1
Process ID: 8576
PSVersion: 5.1.14409.1018
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1018

also example of a simple test running azcopy take a pfx file down and try to import, the azcopy command fails in 5.1 and is OK in 7. I have tested locally as well.

Sample of runbook code

-# Build a command that will be run inside the VM.
$remoteCommand =
@"
-#Start verbose log output
$ErrorActionPreference="SilentlyContinue"
Start-Transcript -path C:\temp\log.txt -append
Invoke-Command -ScriptBlock {
Set-Location C:\azcopy
.\azcopy cp "https://storageaccount.blob.core.windows.net/lecert/file.pfxSASKEY" "c:\temp\file.pfx" --recursive=true
Import-PFXCertificate -CertStoreLocation Cert:\LocalMachine\My -FilePath 'c:\Temp\file.pfx' -Password (ConvertTo-SecureString -String 'password' -AsPlainText -Force)
}
Set-Location C:\temp
-#Stop Verbose Output
Stop-Transcript
"@
-# Save the command to a local file
Set-Content -Path .\test-le.ps1 -Value $remoteCommand
-# Invoke the command on the VM, using the local file
Invoke-AzVMRunCommand -Name $vmname -ResourceGroupName $rgname -CommandId 'RunPowerShellScript' -ScriptPath .\test-le.ps1
-# Clean-up the local file
Remove-Item .\test-le.ps1

I believe it's due to the version of powershell running 5.1 instead of 7 due to syntaxes for commands such as azcopy and the version its using. If I run it .\script.ps1 on 7 locally works fine but not 5.1.

Is there anyway to force the powershell version to be ran so it's 7 on the VM while it runs this from the runbook?

Thanks

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,171 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,443 questions
0 comments No comments
{count} votes

Accepted answer
  1. tbgangav-MSFT 10,416 Reputation points
    2021-04-20T11:25:32.827+00:00

    Hi @Luke Uhren ,

    Firstly just FYI, as per this Uservoice / product feedback item, support for Powershell 7.0 in Azure Automation would be towards CY21 Q4 timeframe.

    Next, if the issue is with azcopy line in your runbook then replace it with Get-AzStorageFileContent cmdlet if the file is in storage file share or Get-AzStorageBlobContent cmdlet if the file is in storage blob and see if it resolves the issue. For illustration, check below sample commands to download file from storage file share. Also, note that only dependency for it is, you would need to make sure the cmdlet's modules are pre-installed before you use them.

     $context = New-AzStorageContext -StorageAccountName "<StorageAccountName>" -StorageAccountKey "<StorageAccessKey>"  
     $StorageShare = Get-AzStorageShare -Name "<FileShareName>" –Context $context  
     Get-AzStorageFileContent –Share $StorageShare –Path "<FileName>" -Destination "c:\temp\file.pfx"  
    

    If that doesn't resolve the issue then leverage Hybrid Runbook Worker(HRW) and see if it resolves the issue. Basically HRW helps to run your runbooks directly on the machine that's hosting the role and against resources in the environment to manage those local resources.

    0 comments No comments

0 additional answers

Sort by: Most helpful