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