GPO Logon PowerShell Script not Executing

Hello,

I've created two Powershell scripts, one log off and one log on. Both scripts acre being run via a .bat file.
I had to create .bat file scripts because the Powershell scripts would not run when setting up via log on / log off setting.

The log off .bat script works just fine and calls the Powershell script which successfully does it's thing.

The log on script just won't work. When I try to run the Powershell script local, I get the execution policy error (though I thought this will be bypassed with my .bat file ..see below):

Log off script(s):
batch file which is the actual script defined in the GPO as the log off script:
@ECHO OFF
Powershell.exe -ExecutionPolicy Bypass -Command "& '%~dp0Logoff.ps1'"

Logoff.ps1 script:
Get-WmiObject -namespace root\cimv2 -Query "select * from Win32_Printer Where Default = TRUE" -Impersonation 3 | select ExpandProperty name | out-file $env:APPDATA\TestFile

Log on script (s):
batch file which is the actual script defined in the GPO as the log on script:
Powershell.exe -ExecutionPolicy Bypass -Command "& '%~dp0Logon.ps1'"

Logon.ps1 script:
(New-Object -ComObject WScript.Network).SetDefaultPrinter((get-content $env:APPDATA\TestFile))

Any help much appreciated!

The easiest way would be if it would just work by defining the powershell scripts in the GPO settings as log off and log on scripts, but if I do that, not even the log off scripts works (neither run if doing it that way, hence I created the batch files to call the ps1 files)

Thanks,

July 30th, 2015 1:07am

have you tried Bypass -Fi
Free Windows Admin Tool Kit Click here and download it now
July 30th, 2015 1:20am

Hello,

use .cmd as logon script. Here is .cmd file example:

@echo off
PowerShell.exe -nologo -file \\YourDC\scripts\YourLoginScript.ps1 -windowstyle hidden -noprofile -executionpolicy bypass

Regards

July 30th, 2015 3:08am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics