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.