Share via


PowerShell: Run a Script with Parameters

This article describes how to add a PowerShell script that contains arguments into the task scheduler. We will do this for a recent script, https://gallery.technet.microsoft.com/scriptcenter/Process-Killer-for-local-836f5b46.

This script can take two different parameter sets (Computer and file) and one parameter common to both called "processname".

So to run this script we can provide the parameters as follows:

  • .\ProcessKiller.ps1 -file "path\to\file.txt" -processname "notepad.exe"  

This will run the script against all the computers within the file.txt (one computer by line).

  • .\ProcessKiller.ps1 -computername singlecomputer -processname "notepad.exe"

This will run the processkiller against the computer called "singlecomputer"  and will close the process called: "Notepad.exe" on that specific computer

  • .\ProcessKiller.ps1 -coomputername computerA,computerB,computerC -processname ``"notepad.exe"

This will run on multiple computers: computerA, computerB, and computerC and will remove the process "notepad.exe" in all of them.

Also, the script works using WMI, so all the permissions must be guaranteed to get the best of it.

Now that we understand the script that we're going to run, now it's necessary to learn how the Task scheduler works.

The task scheduler behind runs based on CMD (command.com under MS-DOS) command line execution, so to run a PowerShell script you'd need to tell MS-DOS to run a PowerShell script if you have the ProcessKiller.ps1 file on the Desktop of your computer and also have a file 

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file "C:\Users\jortega\Desktop\ProcessKiller.ps1" -File "C:\Users\jortega\Desktop\computers.txt" -processname notepad.exe

Note: The first parameter is an array of string ([string[]) that works like a charm to add one computer or several in the PowerShell command line, but it fails big time when you're trying to run it in the Task Scheduler. This means that: 

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file "C:\Users\jortega\Desktop\ProcessKiller.ps1" -computername computerA, computerB, computerC -processname notepad.exe

Will just not work on the task scheduler since it's unable to receive the string[] from the CMD. For this reason, should be run using the -File instead of the computername. Just in the case that is just one computer, it will run with the -computername parameter.

Steps required to run a PowerShell script with arguments in task scheduler

To run successfully this script under the task scheduler we should follow the following steps:

  1. Go to Task Scheduler (Win key and type: task Scheduler). 
  2. Create a Task by clicking "Create Task" on the Action menu
  3. Fill out the Name
  4. Select the "Run whether user is logged on or not" in the security options and enable the "Run with highest privileges"
  5. Then move to the "triggers" tab and select how often and when would you run the task as you usually do
  6. Move to the "Actions" tab  and click on "New"
  7. In the Action drop-down list select: "start a program"
  8. In the program/script field add: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
  9. In the "Add arguments (optional)" field: -file "C:\Users\jortega\Desktop\ProcessKiller.ps1" -File "C:\Users\jortega\Desktop\computers.txt" -processname notepad.exe      (Note that there are two "-file" listed. The first one is related to the "powershell.exe -file" command and the second one is the file parameter of the script. Also, note that all the parameters are after the -file "path\to\the\ps1\file.ps1" [parameters])
  10.  Finally, the Start in (optional) field should be added like this: C:\Users\jortega\Desktop\(without quotes)

For this example we had the files on the desktop of users, here you can use a local or shared (\serverA\folder\script.ps1).

Here's a video of how the scripts normally work:

View

XML view of the final tasks

This will be the XML of the tasks to be imported if you are too lazy to type the fields out:

<?xml version="1.0" encoding="UTF-16"?>
 
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
 
  <RegistrationInfo>
 
    <Date>2017-11-01T14:08:19.4254049</Date>
 
    <Author>J0RT3G4\jortega</Author>
 
  </RegistrationInfo>
 
  <Triggers />
 
  <Principals>
 
    <Principal id="Author">
 
      <UserId>J0RT3G4\jortega</UserId>
 
      <LogonType>Password</LogonType>
 
      <RunLevel>HighestAvailable</RunLevel>
 
    </Principal>
 
  </Principals>
 
  <Settings>
 
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
 
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
 
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
 
    <AllowHardTerminate>true</AllowHardTerminate>
 
    <StartWhenAvailable>false</StartWhenAvailable>
 
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
 
    <IdleSettings>
 
      <StopOnIdleEnd>true</StopOnIdleEnd>
 
      <RestartOnIdle>false</RestartOnIdle>
 
    </IdleSettings>
 
    <AllowStartOnDemand>true</AllowStartOnDemand>
 
    <Enabled>true</Enabled>
 
    <Hidden>false</Hidden>
 
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
 
    <WakeToRun>false</WakeToRun>
 
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
 
    <Priority>7</Priority>
 
  </Settings>
 
  <Actions Context="Author">
 
    <Exec>
 
      <Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command>
 
      <Arguments>-file "C:\Users\jortega\Desktop\ProcessKiller.ps1" -File "C:\Users\jortega\Desktop\computers.txt" -processname notepad.exe</Arguments>
 
      <WorkingDirectory>C:\Users\jortega\Desktop\</WorkingDirectory>
 
    </Exec>
 
  </Actions>
 
</Task>