I'm having trouble executing a PowerShell script (.PS1) through a Group Policy Object (GPO) logon script. I've set up a .CMD script like this:
```cmd
@echo off
set "PS_EXE=%windir%SysnativeWindowsPowerShellv1.0powershell.exe"
if not exist "%PS_EXE%" set "PS_EXE=%windir%System32WindowsPowerShellv1.0powershell.exe"
start "" "%PS_EXE%" -NoProfile -ExecutionPolicy Bypass -File "\domain.localNETLOGONdelete-outlookprofile.ps1"
exit /b 0
```
The script works perfectly when I run it manually, but I get an error when it's executed as a logon script, and the error window closes too quickly for me to see what's wrong. Any suggestions would be appreciated—I'm about ready to throw my laptop out the window!
3 Answers
A good way to troubleshoot is to remove the exit command from your script and add a pause instead, so you can catch any error messages. Launch a command prompt first, then run the CMD file to see if any errors show up. Remember, when executed as GPO, it runs as the system account, which might not have network access.
It's likely that using 'Sysnative' is causing issues since it's just a virtual path for 32-bit apps to access the 64-bit system32 folder. PowerShell scripts should ideally work regardless of the architecture, so try removing the direct path entirely. Also, instead of a CMD wrapper, consider running the PowerShell script directly from the GPO's PowerShell tab, which avoids these issues altogether.
Make sure to slap a 'Start-Transcript' at the top of your script to log the activity. Adding some try-catch blocks with logging would also help you figure out what's going wrong. And yes, setting scripts directly in the GPO PowerShell tab should use the correct version automatically, so double-check if you need this CMD file at all.
Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically