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:temp123.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%System32WindowsPowerShellv1.0powershell.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}MachineScripts 

(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!

链接地址: http://www.djcxy.com/p/61084.html

上一篇: 另一个任务计划程序不执行PowerShell

下一篇: 带有参数的Powershell GPO启动脚本