ScheduledTaskAction

Hi,
have some problem with my code
I try to create ScheduledTask on Win10 and Win7
code working on both systems when i run it manualy, but when push it from SCCM its working on Win10 and created ScheduledTask with userID(this is what i need) but on Win7 and on some Win 10 machines its create task with computerID insted userid

$UserID = Get-CimInstance –ClassName Win32_ComputerSystem | Select-Object -expand UserName

if([string]::IsNullOrEmpty($UserID))
{
$UserID = $env:USERNAME
#start-sleep -Seconds 60
$action = New-ScheduledTaskAction -Execute "cmd"
$action.Arguments = "/c Start /min powershell.exe -ExecutionPolicy Unrestricted -WindowStyle Hidden -NonInteractive -NoProfile -File C:\Test.ps1"
$trigger = New-ScheduledTaskTrigger -Weekly -At "10:00" -DaysOfWeek "Monday"
$principal = New-ScheduledTaskPrincipal -UserId ($UserID) -RunLevel Highest -ErrorAction SilentlyContinue
$task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal
Register-ScheduledTask "Test" -InputObject $task
}

else
{
$action = New-ScheduledTaskAction -Execute "cmd"
$action.Arguments = "/c Start /min powershell.exe -ExecutionPolicy Unrestricted -WindowStyle Hidden -NonInteractive -NoProfile -File C:\Test.ps1"
$trigger = New-ScheduledTaskTrigger -Weekly -At "10:00" -DaysOfWeek "Monday"
$principal = New-ScheduledTaskPrincipal -UserId (Get-CimInstance –ClassName Win32_ComputerSystem | Select-Object -expand UserName) -RunLevel Highest -ErrorAction SilentlyContinue
#$UserID = "$principal.UserId"
#$UserID = "$env:USERNAME"
#$principal = New-ScheduledTaskPrincipal -UserId ("$UserID") -RunLevel Highest -ErrorAction SilentlyContinue
#$principal = New-ScheduledTaskPrincipal -UserId ("$env:USERDOMAIN\$env:USERNAME") -RunLevel Highest -ErrorAction SilentlyContinue
$task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal
Register-ScheduledTask "Test" -InputObject $task
}

Thanks in advance

Hi brad-sam,

I don’t have a way of testing but my guess is this is more of an ‘environment’ issue rather than directly with the script. Here are some thoughts.

  • When you run the script manually, what user account are you running as?
  • Under what user context is the SCCM client running? Basically this will determine what ‘user account’ is running the PowerShell script, and hence what the $UserID will be.
  • Check what account the SCCM client uses on Win7 and Win10 and compare. Do they differ?

Hope some of that helps!

Hi LogicalToolkit,
SCCM running as System account for all environment also when i runnig script manualy i run it as admin
Its working on one win 10 machine but failing on other win 10 and win 7 machines

For a process which is running under the context of SYSTEM, the $env:USERNAME variable will have the value of COMPUTERNAME$.

  • On the failing machines do you know which part of the ‘if’ statement is running?
  • Does the CimCmdlets module exist on the failing machines?

Hi LogicalToolkit,
yes, you right
when i run this command “Get-CimInstance –ClassName Win32_ComputerSystem | Select-Object -expand UserName” on both machine i received different output
and on failing machines - fierst part of the ‘if’ statement is running