5

I am trying to create a scheduled task that will run a Powershell script with 1 parameter - this is on Server 2012 with Powershell v5 installed. So far I have been able to get the task to run, but it doesn't recognize my param value - the value is null in my script that I am calling. Here is what I have to create the scheduled task:

$TicketTime = "9:00pm"
$EmpID = "1234"
$Firstname = "Test"
$Lastname = "User"

$Action = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-ExecutionPolicy ByPass -File C:\Scripts\Term.ps1 -EmpID '$EmpID'"
$Trigger = New-ScheduledTaskTrigger -Once -At "03/29/2016 $TicketTime"
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings (New-ScheduledTaskSettingsSet)
$Task | Register-ScheduledTask -TaskName "$Firstname $Lastname" -User "myuser" -Password "mypass"

This will execute successfully, but will not recognize the -EmpID parameter. I have also tried -Command instead of -File for the Argument value but I get the same result.

-ExecutionPolicy ByPass -Command "& C:\Scripts\Term.ps1 -EmpID 1234"

Single quotes around the param doesn't seem to do anything either

-ExecutionPolicy ByPass -Command "& C:\Scripts\Term.ps1 -EmpID '1234'"

So far nothing I do will get the param to pass. What am I missing?

2 Answers 2

3

Can you try and declare your parameters as positional rather than named:

[CmdletBinding()]
Param (
    [Parameter(Position=0)]
    [string]$Ticket
)

"-ExecutionPolicy ByPass -File C:\Scripts\Term.ps1 '$EmpID'"

Then you can drop the -ParamName and pass it in using position.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the -File option, you just need to separate out the string:

Change

-ExecutionPolicy ByPass -File "& C:\Scripts\Term.ps1 -EmpID 1234"

to

-ExecutionPolicy ByPass -File "C:\Scripts\Term.ps1" -EmpID "1234"

The full string out look like this:

$Action = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-ExecutionPolicy ByPass -File `"C:\Scripts\Term.ps1`" -EmpID `"$EmpID`""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.