passing webrequestsession object into a start-job operation

Welcome Forums General PowerShell Q&A passing webrequestsession object into a start-job operation

This topic contains 0 replies, has 1 voice, and was last updated by  ABurger 9 months ago.

  • Author
    Posts
  • #93715

    Participant
    Points: 0
    Rank: Member

    I am trying to parallelize the request for open files on a 14-node Isilon cluster by using their REST API, and authenticating with session cookies. I can't seem to get the webrequestsession object to be used properly in the invoke-restmethod cmdlet inside start-job however. Code is as follows....assume auth is handled by another function that returns a script level variable called $module_sessionhash, a hash table of the uri (key) and session cookie(value):

    function GetIsilonOpenFiles {
    [CmdletBinding()]
    param (

    )

    begin {

    }

    process {

    try {

    $job = {
    $ResourceUrl = "/platform/1/protocols/smb/openfiles"
    $BaseUrl = $args[0]
    $Uri = $BaseUrl + $ResourceUrl
    $cookie = $args[1]
    Invoke-RestMethod -Uri $Uri -WebSession $cookie -Method Get -ErrorAction Stop
    }

    $keys = $Module_SessionHash.keys | ForEach-Object {$_}
    $values = $Module_SessionHash.values | ForEach-Object {$_}

    Start-job -ScriptBlock $job -ArgumentList $keys[0],$values[0]
    #}

    while (get-job) {
    Get-Job | Where-Object State -NE 'Running' | ForEach-Object {
    if ($_.State -ne 'Completed') {
    Write-Warning ('Job [{0}] [{1}] ended with state [{2}].' -f $_.id, $_.Name, $_.State)
    }
    else {
    $JobResults = @()
    $JobResults = @(Receive-Job -Job $_)

    if ($JobResults.Count -ne 0) { Write-Output $JobResults}
    }

    remove-job $_
    }
    start-sleep -Seconds 1
    }
    }
    catch {
    Write-Error -ErrorAction Stop -Exception $_.Exception
    }
    }
    end {
    }
    }

    At first I was getting this error:
    Cannot bind parameter 'WebSession'. Cannot convert value "Microsoft.PowerShell.Commands.WebRequestSession" to type "Microsoft.PowerShell.Commands.WebRequestSession". Error: "Cannot
    convert the "Microsoft.PowerShell.Commands.WebRequestSession" value of type "Deserialized.Microsoft.PowerShell.Commands.WebRequestSession" to type

    Fine, there is a PSSerializer class that I can leverage to get the format that start-job wants by adding this in the $job scriptblock:

    $cookieobject = [System.Management.Automation.PSSerializer]::Serialize($cookie)

    then running invoke-restmethod with this new object:

    Invoke-RestMethod -Uri $Uri -WebSession $cookieobject -Method Get -ErrorAction Stop

    But now it isn't happy with the xml format!
    Cannot bind parameter 'WebSession'. Cannot convert the "

    ....................................BLAHBLAHBLAH of xml
    -1

    " value of type "System.String" to type "Microsoft.PowerShell.Commands.WebRequestSession".

    What am I missing? Is it not possible to use these types of objects in start-job? The invoke-restmethod works fine when used outside start-job. Perhaps there is a more elegant way of simultaneously asking for the open files on all nodes? Workflows perhaps?

The topic ‘passing webrequestsession object into a start-job operation’ is closed to new replies.