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
- 41 Getting started
- 25 Read before posting
- 8 Meet and greet
- 238 General
- 64 News and announcements
- 1 Swag
- 1 Roadmap updates
- 79 Resources
- 12 Knowledge Base
- 16 Webinars
- 1 Shared Script Library
- 2 Blog
- 19 Pro Tips
- 27 Got an idea?
- 3 Atera Academy
- 2 ActionAI
- 1 Copilot
- 140 Remote Monitoring and Management
- 84 Remote Monitoring
- 27 Patch Management
- 106 Professional Services Automation
- 65 Helpdesk
- 17 Billing
- 21 Reporting
- 37 Integrations & add-ons
- 20 Integrations
- 10 Add-ons
- 103 Scripting and automations
- 61 Scripts
- 30 Automations