Scripting with Powershell 7
We script against some 3rd party powershell modules that require Powershell 7. Running these scripts through Atera fails because it defaults to powershell.exe (Powershell 5.1) when powershell 7 requires pwsh.exe.
I found a workaround here that is working for us:
You can definitely do this it's just not builtin using a pwsh syntax. The easiest way I know is to just start the new process and use a scriptblock as the argument.
pwsh.exe { code inside here runs in the new process }PowerShell internally will convert that scriptblock into
-EncodedCommand ...
and output the data as actual objects so you can get richer objects back to use in the main process. They will be serialized so essentially a read only copy of what was output.
Comments
-
Hi @dfletcher !
Thank you for sharing your script! That's very helpful for a lot of the tasks I've been trying to remediate in my environment. Have you submitted it to the Shared Script Library? This would make it super easy for others to clone in their environments as well. I'm not the world's best script-er so this is very helpful for people like me.
Thank you again!
Sincerely,
Kim
1 -
That's awesome! I'm sure it'll be accepted soon!
1 -
Might be good to build some logic around this to run one version or another based on the PS version on the machine.
if ($PSVersionTable.PSVersion -lt 7){
# Do things in PS v5
}else {
pwsh.exe {
# Do things in PS v7
}
}0 -
Except that PowerShell 7 is a side by side installation. The default powershell is always 5.1 and as far as the current documentation it will stay that way. Calling powershell.exe gets you 5.1. Calling pwsh.exe gets 7.
2 -
That makes sense, @dfletcher look your script has posted! https://app.atera.com/new/admin/scripts and search for "ps7wrapper" to find your posted script in the Shared Script Library! I've already cloned it to use in my environment, so thank you in advance for this awesomeness!
Sincerely,
Kim
2 -
#teamwork
0
Topics
- All Topics
- 40 Getting started
- 24 Read before posting
- 8 Meet and greet
- 220 General
- 59 News and announcements
- Swag
- 1 Roadmap updates
- 68 Resources
- 9 Knowledge Base
- 13 Webinars
- 1 Shared Script Library
- 2 Blog
- 15 Pro Tips
- 26 Got an idea?
- 1 Atera Academy
- 1 ActionAI
- 1 Copilot
- 125 Remote Monitoring and Management
- 78 Remote Monitoring
- 24 Patch Management
- 98 Professional Services Automation
- 61 Helpdesk
- 17 Billing
- 18 Reporting
- 34 Integrations & add-ons
- 20 Integrations
- 10 Add-ons
- 100 Scripting and automations
- 59 Scripts
- 29 Automations