Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must PowerShell scripts be called using only a single line?

I have some PowerShell scripts that accept many long parameters, like,

myScript.ps1 -completePathToFile "C:\...\...\...\file.txt" -completePathForOutput "C:\...\...\...\output.log" -recipients ("[email protected]") -etc.

I can't seem to make PowerShell run such scripts unless all the parameters are on a single line. Is there a way to invoke the script more like this?

myScript.ps1
  -completePathToFile "C:\...\...\...\file.txt"
  -completePathForOutput "C:\...\...\...\output.log"
  -recipients (
    "[email protected]",
    "[email protected]"
   )
  -etc

The lack of readability is driving me nuts, but the scripts really do need to be this parametric.

like image 479
Jeff Stewart Avatar asked Jan 13 '10 15:01

Jeff Stewart


People also ask

How do you call a PowerShell script from the command line?

Running a PowerShell script from the Command Prompt If you would like to run a PowerShell script in CMD, you'll need to execute it by calling the PowerShell process with the -File parameter, as shown below: PowerShell -File C:\TEMP\MyNotepadScript. ps1. PowerShell -File C:\TEMP\MyNotepadScript.

What must you set before you can run a PowerShell script?

Before you can run a script on Windows, you need to change the default PowerShell execution policy. Execution policy does not apply to PowerShell running on non-Windows platforms. The default execution policy, Restricted , prevents all scripts from running, including scripts that you write on the local computer.

How do PowerShell scripts work?

The PowerShell scripting language operates by executing a series of PowerShell commands (or a single one), with each command appearing on a separate line. For the text file to be treated as a PowerShell script, its filename needs to end in . PS1 to connote a PowerShell extension.

What is the default policy for executing scripts in PowerShell?

By default, PowerShell's execution policy is set to Restricted; this means that scripts will not run. You can verify the execution policy setting by using the Get-ExecutionPolicy PowerShell command as shown below. You can change the PowerShell script execution behavior using "Set-ExecutionPolicy".


1 Answers

PowerShell thinks the command is complete at the end of the line unless it sees certain characters like a pipe, open paren or open curly. Just put a line continuation character `` ` at the end of each line but make sure there are no spaces after that continuation character:

myScript.ps1 `
  -completePathToFile "C:\...\...\...\file.txt" `
  -completePathForOutput "C:\...\...\...\output.log" `
  -recipients (
    "[email protected]", `
    "[email protected]" `
   ) 

If you're on PowerShell 2.0 you can also put those parameters in a hashtable and use splatting e.g:

$parms = @{
    CompletePathToFile   = 'C:\...\...\...\file.txt'
    CompletPathForOutput = 'C:\...\...\...\output.log'
    Recipients           = '[email protected]','[email protected]'
}
myScript.ps1 @parms
like image 119
Keith Hill Avatar answered Oct 07 '22 01:10

Keith Hill