Problems using powershell to perform a source safe get by label - powershell

I'm trying to use powershell to do a "get" from srcSafe using a label that contains spaces.
I've read what seems like numerous posts about how to pass params w/spaces to exe's but nothing I've tried works. My problem appears to be supplying the label correctly.
Following is the cmd line version ( which works ).
ss get $/sandbox/TestSSCmdLine/* -R -I-N -VL"label space"
My simplest powershell version is:
ss get '$/sandbox/TestSSCmdLine/*' -R -I-N '-VL\"label space\"'
When I run the powershell cmd I get no files and $lastexitcode is "100".
Echo args shows what I think should be correct.
Arg 0 is <get>
Arg 1 is <$/sandbox/TestSSCmdLine/*>
Arg 2 is <-R>
Arg 3 is <-I-N>
Arg 4 is <-VL"label space">
Powershell ISE shows the same.
DEBUG: NativeCommandParameterBinder Information: 0 : WriteLine Raw argument string: get $/sandbox/TestSSCmdLine/* -R -I-N "-VL\"label space\""
DEBUG: NativeCommandParameterBinder Information: 0 : WriteLine Argument 0: get
DEBUG: NativeCommandParameterBinder Information: 0 : WriteLine Argument 1: $/sandbox/TestSSCmdLine/*
DEBUG: NativeCommandParameterBinder Information: 0 : WriteLine Argument 2: -R
DEBUG: NativeCommandParameterBinder Information: 0 : WriteLine Argument 3: -I-N
DEBUG: NativeCommandParameterBinder Information: 0 : WriteLine Argument 4: -VL"label space"
Just to confuse things start-process seems to work:
$cmd = "ss.exe"
$args = "get", '$/sandbox/TestSSCmdLine/*', "-R", "-I-N", '-VL"label space"'
$proc = start-process $cmd $args -Wait -NoNewWindow -PassThru -WorkingDir $pwd
$proc.ExitCode
An additional confusing item is the fact the echo args now shows the version parameter as:
Arg 4 is <-VLlabel space> -> note no spaces, also does not work from cmd line.
Thanx for any help!
John A.

In cmd, the quotes would have been used to ensure label space was passed as a part of the -VL argument. Since the Start-Process version works with a result argument of -VLlabel space, I would try calling ss with '-VLlabel space', without any embedded quotes (third option listed at the top of this answer).

Related

PowerShell arguments beginning with / are replaced with "True"

As the title says, any argument passed to a PowerShell script that starts with a '/' is replaced with True.
For a script
param (
[string]$wd = ""
)
echo $wd
And run with .\script.ps1 -wd "/etc/xyz", the output is True instead of the expected /etc/xyz. I was unable to find this behavior documented elsewhere, except for a similar issue in vbscript.
How can I get the string as-is?

PowerShell: String expansion in command mode

What are the rules of string expansion in command mode? If on cmd.exe, I would write this:
c:\asdoc.exe -doc-sources+=src
I need to convert this to a string where the actual source path ("src") is computed so somewhere above this line, $sourcePath = "src" is executed. Now I need to transform that cmd.exe command to PowerShell command. I have tried the following but it doesn't work:
& c:\asdoc.exe -doc-sources+=$sourcePath # does NOT work
& c:\asdoc.exe -doc-sources+="$sourcePath" # does NOT work
& c:\asdoc.exe -doc-sources+=($sourcePath) # does NOT work
I used the EchoArgs utility and it gives me these results:
Arg 0 is <-doc-sources+=$sourcePath> # case 1
Arg 0 is <-doc-sources+=$sourcePath> # case 2
Arg 0 is <-doc-sources+=> # case 3
Arg 1 is <src path>
How do I make the string expand "correctly" in this example?
If i understand you properly you want everything expanded and as a single argument. Try to "collect" the argument with quotes.
PS > $sourcepath = "src"
PS > & EchoArgs.exe "-doc-sources+=$($sourcePath)"
Arg 0 is <-doc-sources+=src>
So try this:
& c:\asdoc.exe "-doc-sources+=$($sourcePath)"
The example below will also works AS LONG as you want to expand a variable an not a property inside a variable (ex $myobject.sourcepath)
& c:\asdoc.exe "-doc-sources+=$sourcePath"

Escaping in Powershell for a git log

I know the escape character is (`), the backtick, but even if I try to use it toe escape the < characters I get error...
git log ORIG_HEAD --no-merges --date=short --pretty="format:<tr><td>%h</td><td>%ad</td><td>%an</td><td>%s</td></tr>" > test.txt
< was unexpected at this time.
How can I go about formatting my git log as above?
If on PowerShell v3 try this:
$out = git log ORIG_HEAD --% --no-merges --date=short --pretty="format:<tr><td>%h</td><td>%ad</td><td>%an</td><td>%s</td></tr>"
$out > test.txt
The --% puts PowerShell into a different parsing mode more suitable for native executables. See this blog post for more details.
If you're not using PowerShell v3, I suggest you use echoargs from the PowerShell Community Extensions to see the arguments as git.exe receives them from PowerShell e.g.:
PS> echoargs log ORIG_HEAD --no-merges --date=short --pretty="format:<tr><td>%h</td><td>%ad</td><td>%an</td><td>%s</td></tr>"
Arg 0 is <log>
Arg 1 is <ORIG_HEAD>
Arg 2 is <--no-merges>
Arg 3 is <--date=short>
Arg 4 is <--pretty=format:<tr><td>%h</td><td>%ad</td><td>%an</td><td>%s</td></tr>>
Command line:
"C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe" log ORIG_HEAD --no-merges --date=short --pretty=format:<tr><td>%h</td><td>%ad</td><td>%an</td><td>%s</td></tr>
If you can see how PowerShell is passing the arguments to the exe you have a fighting chance to figure out how to massage the arguments which usually involves using extra quotes.

Using command line arguments in VBscript

How can I pass and access command line arguments in VBscript?
Set args = Wscript.Arguments
For Each arg In args
Wscript.Echo arg
Next
From a command prompt, run the script like this:
CSCRIPT MyScript.vbs 1 2 A B "Arg with spaces"
Will give results like this:
1
2
A
B
Arg with spaces
If you need direct access:
WScript.Arguments.Item(0)
WScript.Arguments.Item(1)
...

How to use the # character in PowerShell

I am trying to do the following - for each *.sql file in the current directory run
sqlplus username/password#connect_identifier_specified_in_argument #file_name
Here is what I have so far:
$scripts = dir *.sql
foreach($script in $scripts) {
Write-Host sqlplus username/password"#"$args "#"$script.Name
}
(I know Write-Host outputs it to the screen; I'm just trying to debug for now.)
However, there is something funky with how PowerShell treats the # character and when I run this I always get something like:
PS C:\code\scripts> C:\utils\run_sql_scripts_on.ps1 identifier
sqlplus username/password#identifier # ALERTS.sql
See that space after the "#"? What gives?
Escape the # with a backtick (`).
Write-Host sqlplus username/password`#$args `#$script.Name
PowerShell Community Extensions has a handy little utility (echoargs) for debugging this sort of problem:
5>echoargs username/password"#"$args "#"$script.Name
Arg 0 is <username/password#>
Arg 1 is <#>
Arg 2 is <test.txt>
Try escaping with a backtick:
6>echoargs "username/password`#$args" "`#$($script.Name)"
Arg 0 is <username/password#>
Arg 1 is <#test.txt>