Not getting Invoke-WebRequest content

Viewed 31

I'm trying a script with the following:

$content = (Invoke-WebRequest -URI https://pastebin.com/xxxxx/raw).Content
$content2 = (Invoke-WebRequest -URI https://pastebin.com/yyyyy/raw).Content
$show = '$content'
$show2 = '$content2'

But I'm getting the error

Cannot convert argument "content2", with value: "$content2", for "Connect" to type "System.Int32": "Cannot convert value
"$content2" to type "System.Int32". Error: "Input string was not in a correct format.""
At C:\Desktop\script.ps1:10 char:1
+ $client.connect($content,$content2)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Exception calling "GetStream" with "0" argument(s): "The operation is not allowed on non-connected sockets."
At C:\Desktop\script.ps1:11 char:1
+ $stream = $client.GetStream()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

You cannot call a method on a null-valued expression.
At C:\Desktop\script.ps1:24 char:1
+ $stream.Write($encoding.GetBytes($out),0,$out.Length)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

However If I try (Invoke-WebRequest -URI https://pastebin.com/yyyyy/raw).Content or If I try (Invoke-WebRequest -URI https://pastebin.com/xxxxx/raw).Content I'm able to get that content

and if I run

$content = (Invoke-WebRequest -URI https://pastebin.com/xxxxx/raw).Content
$show = '$content'

It returns nothing and I don't know why.

1 Answers

Elaborating on @Theos explanation
when placing variables in quotations you need to use Double-quoted strings. A string enclosed in double quotation marks is an expandable string. Variable names preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing.

Here is the rectified code

$content = (Invoke-WebRequest -URI https://pastebin.com/xxxxx/raw).Content
$content2 = (Invoke-WebRequest -URI https://pastebin.com/yyyyy/raw).Content

$show = "$content"
$show2 = "$content2"
Related