0

I was hoping I could get some help. Im currently converting some of my functions into a custom class method. Things have been going ok while I follow along with this tutorial series.

how to make a class

invoke-webrequest sample

The issue I am facing is with the

-SessionVariable mySession

and then referencing it in my second request.

-WebSession $mySession

Here is my class ps1 file


Class myClass
{
    [String]$url = "http://httpbin.org/json"
    [String]$username = "username"
    [String]$password = "password"

    getWebSite()
    {

        $result = Invoke-WebRequest $this.url -SessionVariable mySession

        $result.RawContent | out-file "website.txt"

        $result = Invoke-WebRequest -WebSession $mySession

        

    }

}

$myRecord = new-object myClass
$myRecord.getWebSite()
+         $result = Invoke-WebRequest -WebSession $mySession
+                                                 ~~~~~~~~~~
Variable is not assigned in the method.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : VariableNotLocal

I have tried all kinds like adding mySession variable at the top in the class along with URL. Declaring $mySession at the top of the method

I can see it being set in my debugger(vs code) as an auto var but have no clue how I can access it.

If I use a standard function this works as expected, lifting and shifting the code into a class has me tearing my hair out.

If this isn't a good approach I am open to alternatives.

Thanks for any help :)

vscode debugger

3
  • 2
    $mySession = $null at the beginning of getWebSite() method should work. Commented Jan 22, 2021 at 21:40
  • Thank you very much. I see my error now. I had $mySession as a class property when trying to get it to work on my own. The computer was complaining saying that I had to use $this.mySession removing it from the top and just having your suggestion worked :) I spent far too long on this >:( Commented Jan 23, 2021 at 12:31
  • See also stackoverflow.com/a/78867789/6822192 Commented Aug 13, 2024 at 18:20

1 Answer 1

1

See @zett42 comment.

The issue I was having in testing was that I had mySession as a class property so when I was trying to reference it in the method it was complaining that I had to use $this.mySession.

This is correct error but ends in the wrong result as $this.mySession is null.

Removing it from the class properties and making the declaration in the method as zett42 points out works. My example now looks like the following if anyone else is interested.

Class myClass
{
    [String]$url = "http://httpbin.org/json"
    [String]$username = "username"
    [String]$password = "password"

    getWebSite()
    {
        $mySession = $null

        $result = Invoke-WebRequest $this.url -SessionVariable mySession

        $result.RawContent | out-file "website.txt"

        $result = Invoke-WebRequest -WebSession $mySession

        

    }

}

$myRecord = new-object myClass
$myRecord.getWebSite()
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.