Link to home
Start Free TrialLog in
Avatar of spencerturbine
spencerturbine

asked on

Powershell Logon Script Failing

I am testing logon scripts pushed out through a GPO linked to a specific OU.

Its a simple script that just enters the user name into a remote SQL server I set up on the domain.

$conn = New-Object System.Data.SqlClient.SqlConnection
$conn.ConnectionString = "Data Source=MYSERVER;Initial Catalog=data;Integrated Security=SSPI;"
$conn.open()
$cmd = New-Object System.Data.SqlClient.SqlCommand
$cmd.connection = $conn
$cmd.commandtext = "INSERT INTO tbl_Logon(username) VALUES('{0}')" -f $env:username
$cmd.executenonquery()
$conn.close()

The script does not run automatically but once logged in, I can launch it on the target computer/user from the command line using the following:

Powershell.exe -File \\my-domain.com\NETLOGON\myscript.ps1

and the data is inserted into the table. RSOP does show the correct path and executable name so I know the policy is right, so what am I missing? Why won't it run automatically when I login?

Thanks
Avatar of becraig
becraig
Flag of United States of America image

Is the GPO configured at:
Computer Configuration/Windows Settings/Scripts/Startup
Avatar of spencerturbine
spencerturbine

ASKER

No, this is a logon script, not a startup script.
Is this the only script ?

Or are there others that run, but just not this one  ?
This is the only script.
ASKER CERTIFIED SOLUTION
Avatar of becraig
becraig
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I linked the GPO to the root of the domain and it started working.

When I un-link it from the root of the domain and link it to the OU that contains the user for which I want the script to run, it doesn't run.
Here is an Excerpt from the output of the command GPRESULT /R from the command line of one of the users in the OU showing the LogonScriptTest GPO has been applied.

Applied Group Policy Objects
 -----------------------------
     Default Domain Policy
     LogonScriptTest
     Local Group Policy
It seems there may be an override that prevents your GPO from working, you may want to block inheritance on that OU and try again.
I kept reading the documentation and while I couldn't spot anything specific wrong, I just kept trying and it finally started to work. I must have been doing something slightly wrong and continued trial and error with information at the provided links must have finally set me straight.