1

I use this script to get the results of a url:

@Echo off
For /f "delims=" %%A in (
  'powershell -NonI -NoP -C "(Invoke-Webrequest "%~1").content"'
) Do set Line=%%A

I can call it in a command prompt window like this:

 WebResponse.cmd "http://websiteremoved.com/teststat.php?username^=%friend1%"

I need to use the caret (^) to cancel out the Equal sign. However when I run that same line of code in my batch file it does not work. According to the test, %~1 is set to "http://websiteremoved.com/teststat.php?username^=Lukaka" but the powershell portion returns this:

Invoke-WebRequest : A positional parameter cannot be found that accepts argument 'Lukaka'. [] http://websiteremoved.com/teststat.php?username^=Lukaka 
At line:1 char:2 [] http://websiteremoved.com/teststat.php?username^=Lukaka 
+ (Invoke-Webrequest http://websiteremoved.com/teststat.p ... [] http://websiteremoved.com/teststat.php?username^=Lukaka 
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [] http://websiteremoved.com/teststat.php?username^=Lukaka 
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException [] http://websiteremoved.com/teststat.php?username^=Lukaka 
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand [] http://websiteremoved.com/teststat.php?username^=Lukaka 

Any ideas as to why it is doing this? Thanks a bunch!

8
  • 2
    You got me wrong last time, I said either quote or escape, not both. Commented Jul 3, 2017 at 19:48
  • Why use cmd.exe at all? Just run the command you want to run directly from the PowerShell prompt. Commented Jul 3, 2017 at 20:23
  • Depends on how much memory you have available. cmd.exe 0.4MB compared to PowerShell 32MB. (80 times more memory required just to have powershell open) Of course if you have 4G free -- who cares... Commented Jul 3, 2017 at 20:47
  • Spinning up a copy of powershell.exe just to run one command is (to put it mildly) quite inefficient. If you need PowerShell, then use it and leave it open. (Then you're not inefficiently spinning up new copies, because it's already open.) Commented Jul 3, 2017 at 23:26
  • @Bill_Stewart Im running this from a batch file. the running of it from cmd.exe was just to test it. Commented Jul 4, 2017 at 14:35

1 Answer 1

0

The way I was doing it was inefficient. I am closing this question. I am working on a different version of a new script here: Add Multiple URLs to this Powershell Script? which is related.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.