Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

oop - trouble changing powershell function to class with Invoke-WebRequest

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

question from:https://stackoverflow.com/questions/65852232/trouble-changing-powershell-function-to-class-with-invoke-webrequest

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...