user259286
user259286

Reputation: 1005

Powershell: Script not running from command line but runs from ISE?

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!

Upvotes: 3

Views: 7502

Answers (1)

manojlds
manojlds

Reputation: 301087

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?

Upvotes: 7

Related Questions