Start a Powershell script with a scheduled task

Start a Powershell script with a scheduled task

I was in a situation where i wanted to know which SCCM device collections a specific users devices where a member of. Therefore I created script which was executed as a scheduled task on a server. Here is an example on how to do that.

First create the scheduled task and run it with highest privileges and as another user if wanted. Could be an SCCM service account that only have access to certain WMI classes.

In the Actions pane create an action that starts a program. In this case start powershell and put this into the argument field:

-ExecutionPolicy Unrestricted -Command “& { . C:\$PathToScript\$Script.ps1; $NameOfFunction $ArgumentName $Argument }”

Where $PathToScript is the path to your deisred script and $Script is the name of your script. Example:
-ExecutionPolicy Unrestricted -Command “& { . C:\ImbaScripts\get-collections.ps1; get-collections computer mroenborg01 }”

If the script doesn’t require to run a specific function just clear the function part:

-ExecutionPolicy Unrestricted -Command “& { . C:\ImbaScripts\get-collections.ps1}

If you have any questions just post a comment!

Thanks

 

 

Leave a comment