question

NancyJ-3014 avatar image
0 Votes"
NancyJ-3014 asked NancyJ-3014 commented

Trouble with quotes in PowerShell

I have a PowerShell script that starts a process to open our FTP client and download some files. It works great on it's own but I now need to pass a variable to it but the result of that variable needs to be in quotes and I'm struggling with how best to do that.

Here is what I use now that works very well:

 Start-process "C:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe" -ArgumentList "-s TESTCONN:/ -d local:C:\Test\"

What I need to do is change the item in quotes after -ArgumentList to include a variable. I can't figure out how to set this up so that variable is in quotes when it's in the Start-process line:

 $PDate = Get-Date -Format yyyyMMdd
 $arglist = "-s TESTCONN:/" + $PDate + "* -d local:C:\Test\"
    
 Start-process "C:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe" -ArgumentList $arglist

I need the final command to look like this (assuming date of 2022-05-20):

 Start-process "C:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe" -ArgumentList "-s TESTCONN:/20220520* -d local:C:\Test\"

I've tried double quotes, single quotes and multiple combinations of the two but I cannot get it to output correctly.

What am I missing?

Thanks in advance.

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

NewbieJones-6218 avatar image
0 Votes"
NewbieJones-6218 answered NancyJ-3014 commented

Backticks to encapsulate the double quotes.

 $path = "`"C:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe`""
 $PDate = Get-Date -Format yyyyMMdd
 $parameter = "`"-s TESTCONN:/$Pdate* -d local:C:\Test\`""
 Write-Host Start-Process $path -ArgumentList $parameter
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That is exactly what I needed, thank you so very much!

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

That doesn't seem like it's very difficult to achieve. Is this what you're after?

 $arglist = "-s TESTCONN:/$PDate* -d local:C:\Test\"

There's no need to place quotes around the $arglist when it's used as the value for the -ArgList parameter.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

NancyJ-3014 avatar image
0 Votes"
NancyJ-3014 answered RichMatheisen-8856 commented

@RichMatheisen-8856 - That's neat, I didn't know you could combine like that. Thanks for the tip.

What I run into though with that option also is that the end result is:

 Start-process "C:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe" -ArgumentList -s TESTCONN:/20220520* -d local:C:\Test\

I need everything after -ArgumentList to be inside quotes like this:

 Start-process "C:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe" -ArgumentList "-s TESTCONN:/20220520* -d local:C:\Test\"
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

No, you don't! Just use the variable $arglist!

 Start-process "C:\Program Files (x86)\Ipswitch\WS_FTP 12\wsftppro.exe" -ArgumentList $arglist



0 Votes 0 ·
NancyJ-3014 avatar image
0 Votes"
NancyJ-3014 answered

I understand how to put the variable in the command but it doesn't run if I do, it just exits with: Start-Process : A positional parameter cannot be found that accepts argument 'TESTCONN:/'20220520*

I need to have the variable include the quotes so the end result is correct. Sorry if I wasn't clear but that's where I'm stuck.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.