简体   繁体   中英

Powershell GPO startup script with parameter

I have a PowerShell script that I would like to run as a GPO on machine startup. Normally i would just run a cmd with "powershell.exe -ExecutionPolicy Bypass -File c:\\temp\\123.ps1 -Admin"

But as it is run from GPO, how do I define the file path ?

The above CMD file does not work. I also tried it as a PowerShell startup gpo script, but that does not work either.

This depends on your server version but several recent versions of servers allow you to create a GPO where you have "PowerShell Scripts" tab (as well as "Scripts") and you can simply use that tab to point to a PowerShell script. That will copy the file for you into the GPO directory under Sysvol.

Another option, using the old method you mentioned is to point to the PowerShell file using -file. You can go about this in a couple of ways. If you, for example, specify a cmd file, you can copy the PowerShell script into that folder, then in the cmd you can use something like:

%windir%\System32\WindowsPowerShell\v1.0\powershell.exe -exec bypass -file %~dp0MyPowerShellScript.ps1

Because the cmd file and PowerShell file are in the same GPO directory, which would normally be under

\\{Yourdomain}\sysvol\{YourDomain}\Policies\{SomeGuid}\Machine\Scripts\ 

(OK, typing this on top of my head, you need to check the actual path). You can of course use that full path as well, but why would you when you can just use 'current folder', ie "%~dp0"

Note that using "PowerShell Scripts" tab instead of "Scripts" gives you a couple of benefits, like taking care of ExecutionPolicy, so you should use that, if you can.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM