简体   繁体   中英

Convert object of type PsObject to HtmlWebResponseObject

I am trying to get around invoke-webrequest 's propensity to hang in memory and kill my entire script. So far I have written a script block using get-job which calls this from a foreach :

start-job -scriptblock {invoke-webrequest $using:varSiteVariable} -name jobTitle | out-null

I wait 10 seconds and then use receive-job to capture the output from the most recent job into a variable, which I then want to parse as a PowerShell HtmlWebResponseObject in the same manner I would if I were using invoke-webrequest directly. The logic behind this is that I will then throw script execution and return to square one if there is nothing to parse, as invoke-webrequest has clearly crashed again.

However, when I pull the data from jobTitle into a variable, it is always of type PsObject , meaning it lacks the crucial ParsedHtml method which I'm using to perform all of the further parsing of the HTML code; that method appears to belong specifically to objects of type HtmlWebResponseObject . There does not appear to be a way that I have found to force-cast the object into this type, nor any way to convert one into the other after the type.

I cannot simply define the variable from within the job and then refer to it outside of the job, as the two commands happen in different contexts and share no working space. I cannot write the data to a file as I am unable to import it back as the right data-type for the processing I need to perform.

Does anyone know how I can convert my PsObject data into HtmlWebResponseObject data?

I ended up fixing this with the help of this article: https://gallery.technet.microsoft.com/Powershell-Tip-Parsing-49eb8810

I couldn't re-cast the data as HtmlWebResponseObject, but I was able to make a new COM Object of type HTMLFile and write the data from the variable grabbed from my job into that. The script needed to be slightly re-written but the all-important methods I was using to parse the data work as before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM