1

Is there a way to exclude the colon (:), so you can print it via parameters as String?

Little example:

PowerShellTest.ps1:

param (
    [string]$message="Error: No message defined!"
);
"Info: Test-Information"
$message;

if you now starts this script via powershell:

powershell.exe D:\PowerShellTest.ps1 -message "Error: Test-Error"

Now you get only the output string Error: the rest will be cut off

What do I have to do to get the whole string Error: Test-Error?

Ahmed Salman Tahir
  • 1,783
  • 1
  • 17
  • 26
5im
  • 224
  • 2
  • 13
  • Related post - [Powershell: Colon in commandlet parameters](https://stackoverflow.com/q/8525572/465053) – RBT Nov 18 '21 at 04:20

3 Answers3

1

The problem is not the colon, but the space. You need to escape it using the backtick:

powershell.exe D:\PowerShellTest.ps1 -message 'Error:` Test-Error'
arco444
  • 22,002
  • 12
  • 63
  • 67
  • This is it! Ty But why is it necessary to exclude a blank? What is the logic behind it? – 5im Feb 11 '14 at 10:29
  • I'd imagine its something to do with you being in a powershell process and starting another one with those arguments. The first shell needs to escape all the data in order to properly send it to the new one. As vonPryz suggested, using single quotes is sufficient if running from a normal cmd command prompt. – arco444 Feb 11 '14 at 10:55
0

Use single quotes ' instead of double quotes " like so,

C:\temp>powershell c:\temp\test.ps1 -message 'Error: Test-Error'
Info: Test-Information
Error: Test-Error
vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • In the PowerShell this works (no cut off): .\Test.ps1 -message 'Error: Test-Error' But also in PowerShell, this is not working (cut off): powershell.exe Test.ps1 -message 'Error: Test-Error' – 5im Feb 11 '14 at 10:25
0

Why are you running it that way? When you run powershell.exe from inside Powershell you are forcing the arguments to go through the old Windows cmd.exe style command line processing.

Instead you should just invoke your script directly:

PS C:\scripts> .\PowershellTest.ps1 -message "Error: Test-Error"
Info: Test-Information
Error: Test-Error

This way all of the arguments will pass right through without being converted to strings, having double quotes stripped and then being re-parsed.

If you get an error because running scripts is not permitted then the answer is to change your execution policy, not to start another copy of powershell.exe:

PS C:\scripts> .\PowershellTest.ps1  -message "Error: Test-Error"
.\PowershellTest.ps1 : File C:\scripts\PowershellTest.ps1 cannot be loaded because running scripts is disabled on this
system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\PowershellTest.ps1  -message "Error: Test-Error"
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

If you see this error then start a Powershell window using the 'run as administrator' option and enter the command:

Set-ExecutionPolicy RemoteSigned

Close the administrator powershell and restart any other powershell windows you have running and now you should be able to run locally created scripts without any problems.

Duncan
  • 92,073
  • 11
  • 122
  • 156
  • That's clear. I just reduced my problem to the minimum. In the real world it will be executed and monitored from a Java application. – 5im Feb 11 '14 at 12:05
  • 1
    @5im I think in reducing your problem you changed it into a different one. Legacy programs receive their arguments through a single string. When you invoke such a program with Powershell you have to consider how PS converts the command parameters into the command line string and then how the command parses that string. When you run it from Java you have a completely different set of conversions into the command line string to consider, and then the same parsing. Any escaping of e.g. spaces will likely be different between the two. – Duncan Feb 11 '14 at 14:07