1

In my Powershell script, I use "rmtshare.exe" to get information about share level permission. The "rmtshare.exe" can run perfectly under CMD environment with following command:

rmtshare.exe \\fs-sw-206\"C&C FQT" 

However, when I bring it to powershell environment. I can't figure out how to escape the space and the ampersand. Here is what I have tried so far which it is not working:

$rmtShare = "C:\rmtshare.exe" $ServerName = "fs-sw-206" $ShareName = "C&C FQT" Invoke-Expression ($rmtShare + " " + "\\" + $ServerName + "\" + $ShareName) 

The script above will give error message from the CMD, it said "if a sharename or path contains spaces, it should be enclosed in quotes". If I changed the last line to this:

Invoke-Expression ($rmtShare + " " + "\\" + $ServerName + "\" + "'"+'"'+"'" + $ShareName +"'"+'"'+"'") 

The error message was from Powershell itself, it said "The ampersand (&) character is not allowed". NOTE: if there is no ampersand, it works. So, now I am stuck because I need to escape both characters at the same time.

Please offer your solution.

You may need to download the rmtshare.exe to test out yourself. Download site: (https://www.symantec.com/connect/downloads/remove-folder-share-remote-computer)

2
  • 1
    --% escapes everything after itself, you can incorporate variables, check this: ss64.com/ps/syntax-esc.html Commented Dec 13, 2016 at 19:57
  • THANKS for the tip! YES, it's working by incorporate with --%. I will post the final solution later. Commented Dec 13, 2016 at 21:14

1 Answer 1

1

so, here is the code in Powershell that overcame the problem - escape space and ampersand in same time in powershell script that execute CMD

$s = " " $q = '"' $Verbatim = '--%' $rmtShare = "C:\rmtshare.exe" $ServerName = "fs-sw-206" $ShareName = "C&C FQT" Invoke-Expression ($rmtShare +$s+$Verbatim+$s+"\\"+$ServerName+"\"+$q+$ShareName+$q) 

There should be other solutions as well. Please post if you know it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.