0

I'm having trouble getting my Powershell file to run from Task Schedule during logoff. In short, I'm trying to upload my NTUSER.DAT (roaming profile) to my profiles folder on the file server. Dynamic VLAN switching does not support roaming profiles thus I have to use a script to solve this issue. My script is as follows:

start-sleep -s 15

Set-Content -Path "C:\Temp\log.txt" -Value "Hello World"

$FileServer = 10.10.5.5
$FQDN = "$Env:UserDomain"
$User = Get-WmiObject -Class Win32_NetworkLoginProfile | Sort-Object -Property LastLogon -Descending | Select-Object -Property * -First 1 | Where-Object {$_.LastLogon -match "(\d{14})"} | Foreach-Object { New-Object PSObject -Property @{ Name=$_.Name.TrimStart("$FQDN\") }} | Select -ExpandProperty Name


copy-item -path  C:\Users\$User\NTUSER.DAT -destination \\$FileServer\Profiles$\$user.V2\ -force
copy-item -path  C:\Users\$User\NTUSER.DAT -destination \\$FileServer\Profiles$\$user.V3\ -force

I have a task schedule to run as Domain Admin when event ID 4647 occurs (logoff). It is set to run with the Highest Privileges checked, also I have enabled to run weather the user is logged in or no. The Domain Admin account also has access to ever file, both at source and destination. Set-ExecutionPolicy is set to unrestricted. The way I call the powershell script in Task Scheduler is:

Action: Start a program
Program/Script: C:\temp\launch.bat

The launch.bat contains the following:

Powershell.exe -executionpolicy unrestricted -File "CALL C:\temp\powershell.ps1"

The task is triggered each time when the user logs off, I have confirmed this in the logs. But the NTUSER.DAT file fails to copy over to the network, in fact the log.txt file isn't even generated. I have confirmed my code to work, it's just the task scheduler part calling the code where I think it fails. Can you guys point me in the right direction here? I know I'm close, but not sure what I'm missing.

5
  • Why are you using the logoff event ID instead of defining a logoff script... technet.microsoft.com/en-us/library/cc753583.aspx
    – Reaces
    Dec 9, 2014 at 15:44
  • Because the NTUSER.DAT file is locked and is the last thing to be off loaded during the logoff process. I need to run this script once the user has completely logged off or else the script wont have access to copy NTUSER.DAT since it's being used. Triggering an event ID is working, it's the script I'm having a hard time with. Dec 9, 2014 at 15:46
  • I'm now able to get the Powershell to launch EACH time I log off using my method, I had to modify the batch file as such: Powershell.exe -executionpolicy unrestricted -file "C:\temp\powershell.ps1" >> error.log Now that I have that working, it seems like there is now an issue with my powershell code. It fails to copy the NTUSER.DAT (or any other file for that matter) when the destination is set to a variable (instead of hardcoding - which works). The log.txt file gets crated, but the copy parts fails. Dec 9, 2014 at 16:40
  • how did you get around the issue of not being logged on (thus not authenticated to the network) to copy the files?
    – Jim B
    Dec 9, 2014 at 18:30
  • Hi Jim, This script is run by a schedule task which is being RunAs a domain admin account (who has GPO setting to run as batch and run as service). Dec 19, 2014 at 17:03

0

You must log in to answer this question.

Browse other questions tagged .