1

I am trying to create a one-line powershell script that just requests an url. The script is working fine when I run it as a ps1 file:

File "test.ps1":

$webclient=New-Object "System.Net.WebClient"
$data=$webclient.DownloadString("https://google.com")

I run this script in PS console like this:

PS C:\test.ps1 -ExecutionPolicy unrestricted

This runs without any problem, but when I try to schedule this script and make it a one-line according to these recommendations i.e. replace "" with '' and separate commands with ; so the result will be:

one-line:

powershell -ExecutionPolicy unrestricted -Command "$webclient=New-Object 'System.Net.WebClient'; $data=$webclient.DownloadString('https://google.com');"

Then I got the following problem:

Error:

The term '=New-Object' is not recognized as the name of a cmdlet, function, script file, or operable program

I tried another script that also works fine as ps1 file, but not working as one-liner:

$request = [System.Net.WebRequest]::Create("https://google.com")
$request.Method = "GET"
[System.Net.WebResponse]$response = $request.GetResponse()
echo $response

one-line:

powershell -ExecutionPolicy unrestricted -Command "$request = [System.Net.WebRequest]::Create('https://google.com'); $request.Method = 'GET'; [System.Net.WebResponse]$response = $request.GetResponse(); echo $response"

Error:

Invalid assignment expression. The left hand side of an assignment operator needs to be something that can be assigned to like a variable or a property. At line:1 char:102

According to get-host command I have powershell v 2.0. What is the problem with one-line scripts above?

Community
  • 1
  • 1
Andrey Prokhorov
  • 890
  • 2
  • 13
  • 25
  • `powershell ... -Command "&{$webclient = ...}"` – Ansgar Wiechers Jan 13 '17 at 12:50
  • @AnsgarWiechers Thanks, but unfortunately call operator (&) did not help me in this case, I get the same errors :/ – Andrey Prokhorov Jan 13 '17 at 13:07
  • That's because you're running the commandline from PowerShell instead of running it as a scheduled task like you said. If you need to run it from an interactive console use CMD to emulate an environmnent that's more similar to what you have with a scheduled task. – Ansgar Wiechers Jan 13 '17 at 13:15
  • Yes, you are right! It was not working in powershell but do works in cmd. I thought it does not matter where to test the script. Thank you so much! Copy your first comment as answer if you want me to mark this question as solved. – Andrey Prokhorov Jan 13 '17 at 13:19

1 Answers1

2

Put the statements you want to run in a scriptblock and run that scriptblock via the call operator:

powershell.exe -Command "&{$webclient = ...}"

Note that pasting this commandline into a PowerShell console will produce a misleading error, because PowerShell (the one into which you paste the commandline) expands the (undefined) variables in the string to null values, which are then auto-converted to empty strings. If you want to test a commandline like this, run it from CMD, not PowerShell.

It might also be a good idea to have the scriptblock exit with a status code, e.g.

&{...; exit [int](-not $?)}

or

&{...; $status=$response.StatusCode.value__; if ($status -eq 200) {exit 0} else {exit $status}}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Good advice and explanation, but do you know why the OP's attempt didn't work, why the use of a script block is necessary? It seems to me that the OP's problems only stemmed from running the commands in PS, where the premature variable expansion you describe occurred. – mklement0 Jan 13 '17 at 14:20
  • 1
    @mklement0 The error was indeed caused by pasting the commandline with the double-quoted string into PowerShell. I think using `&{...}` was required if you wanted to run more than one statement with PowerShell v2, but I can't vouch for that. If it was, it doesn't seem to be required with more recent versions anymore, but I don't know when exactly that was changed, and I prefer using the scriptblock wrapper anyway, just to be on the safe side. – Ansgar Wiechers Jan 13 '17 at 17:45
  • For those of you who got SSL error when running the script above - check the answer http://stackoverflow.com/a/9918045/2021224, just adding a line "[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}" helped me to reach the goal. – Andrey Prokhorov Jan 15 '17 at 16:00