3

Here is the function I call in my script:

Function SetUp-ScheduledTasks
{
param
(
    [string]$Server = "",
    [string]$TaskName = "",
    [string]$ReleasePath = "",
    [string]$User = "",
    [string]$Pwd = ""   
)

try
{
    Set-ExecutionPolicy RemoteSigned

    Remove-ScheduledTask -ComputerName $Server -TaskName $TaskName Get-ScheduledTask
    Create-ScheduledTask -ComputerName $Server -TaskName $TaskName -TaskRun $ReleasePath  -Schedule "DAILY" -StartTime "02:00:00" -RunAsUser $User -RunAsPwd $Pwd

    exit 1
}   
catch
{           
    exit 0
}

}

When I call this from within the Powershell_ISE in the script file but outside any function, it works perfectly, here's what I do for that: SetUp-ScheduledTasks "myserver" "MyTask1" "c:\release" "theuser" "thepassword"

However when I call it from PS command line like this: . .\ScheduledTasks.ps1 SetUp-ScheduledTasks "myserver" "MyTask1" "c:\release" "theuser" "thepassword" It does not do anything.

I also tried qualifying each parameter with a dash and name, but that still didn't work.

What am I missing?

Thanks!

4
  • When you run from the ISE are you running at the command prompt or from within the editor window at the top? Commented Sep 2, 2011 at 17:47
  • From ISE I just click on the green run button, in the editor window. Commented Sep 2, 2011 at 18:01
  • So then you are doing two different things. You are adding it to the script, then trying to run it as a parameter from a command line which is totally different. Commented Sep 2, 2011 at 18:04
  • No, I'm running from within the script just as test to see if the functions work. But I actually comment that call out because i really want to run it from the command line. Commented Sep 2, 2011 at 18:09

1 Answer 1

7

Let me repeat what you are doing, but with a simpler example:

You have a function, like so:

function a{
write-host "this is function a"
}

Let's say you save it in test.ps1

Now, to test this in ISE, you do below in test.ps1:

function a{
write-host "this is function a"
}

a

And press the Run button and you get the expected output, in this case this is function a

Now, you use the original test.ps1 without the bottom line (a) and, call it like so from console:

. .\test.ps1 a

And it doesn't give the output. Why? a, the intended function call is being passed as parameter to the script and the function a doesn't get called.

You have to do like this:

. .\test.ps1; a

PS: Aren't you using exit 0 and exit 1 in wrong places?

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

2 Comments

Yes, that was it! Thank you. As for the exit codes, I'm under the impression that to signify success, you exit with a 1 inside the try block and with a 0 in the catch. Is that incorrect? Should they be elsewhere? Thanks again!!
Oh so it's just a preference thing? Or it has to be like that?

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.