# ScheduledTaskAction

**URL:** <https://forums.powershell.org/t/scheduledtaskaction/17678>\
**Category:** PowerShell Help\
**Created:** [October 1, 2021, 8:33pm UTC](https://forums.powershell.org/t/scheduledtaskaction/17678 "2021-10-01T20:33:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [October 1, 2021, 8:33pm UTC](https://forums.powershell.org/t/scheduledtaskaction/17678/1 "2021-10-01T20:33:27Z")

</div>

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

```auto
$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

---

<div class="post-metadata">

**Author:** ![LogicalToolkit](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/logicaltoolkit/32/1647_2.png) [@LogicalToolkit](https://forums.powershell.org/u/LogicalToolkit)\
**Post date:** [October 3, 2021, 7:59pm UTC](https://forums.powershell.org/t/scheduledtaskaction/17678/2 "2021-10-03T19:59:05Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [October 3, 2021, 9:46pm UTC](https://forums.powershell.org/t/scheduledtaskaction/17678/3 "2021-10-03T21:46:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LogicalToolkit](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/logicaltoolkit/32/1647_2.png) [@LogicalToolkit](https://forums.powershell.org/u/LogicalToolkit)\
**Post date:** [October 4, 2021, 7:33am UTC](https://forums.powershell.org/t/scheduledtaskaction/17678/4 "2021-10-04T07:33:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [October 4, 2021, 1:03pm UTC](https://forums.powershell.org/t/scheduledtaskaction/17678/5 "2021-10-04T13:03:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:21pm UTC](https://forums.powershell.org/t/scheduledtaskaction/17678/6 "2024-05-16T20:21:15Z")

</div>

Closed on May 16, 2024, 8:21pm UTC


