title: SessionVariable parametrised in Invoke-WebRequest
body: <p>I want to pass a variable to <code>SessionVariable</code> as an argument, so that I can use any name for the <code>SessionObject</code> so that I can test/send to other functions, and not happy it is being hardcoded... i.e. Pass <code>$TCSessionVar</code> instead of a hardcoded <code>TCSessionVar</code> argument.</p>
  
  <pre><code>$Response= Invoke-WebRequest -Uri $TeamCityUrl `
     -SessionVariable $TCSessionVar -Method `
     Get -Headers @{"Authorization"="Basic $Encoded"}
  
  $ReturnObject =New-Object PSCustomObject @{
      SessionVar=[Microsoft.PowerShell.Commands.WebRequestSession]$TCSessionVar
      Response=$Response
  }   
  </code></pre>
  
  <p>The error I get is: </p>
  
  <blockquote>
    <p>Cannot convert the "Microsoft.PowerShell.Commands.WebRequestSession" value of type "System.String" to type "Microsoft.PowerShell.Commands.WebRequestSession".</p>
  </blockquote>
  
  <p>--Note: I can create the WebReuestSessionObject from the response, but then SessionVariable is pointless.. </p>
  
  <pre><code>$TCCookieName="TCSESSIONID"
  $Cookies=$Response.Headers.'set-cookie'
  $TCCookie= $cookies.Split(";")| Where-Object {$_ -match $TCCookieName}
  $CookieName=$TCCookie.Split("=")[0]
  $CookieValue=$TCCookie.Split("=")[1]
  $TCSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
  $cookie = New-Object System.Net.Cookie 
  $cookie.Name = $CookieName
  $cookie.Value = $CookieValue
  $cookie.Domain=([uri]$TeamCityUrl).Host
  $TCSession.Cookies.Add($cookie);
  </code></pre>