title: Posting HTTP form without using 'Invoke-WebRequest'
body: <p>I have a working PowerShell 3.0 script that takes advantage of <code>Invoke-WebRequest</code> to post a form and save the respective raw HTML output.  Unfortunately, the script doesn't work on workstations with only PowerShell 2.0.</p>
  
  <p>I was hoping someone could please help convert the below script to work with PowerShell 2.0 (without having to install PS modules, packages, or upgrade PowerShell).  It looks like I need to use .NET webclient, but I'm not experienced enough to do this yet.  A working example (based on my script) would be extremely appreciated!</p>
  
  <p>PS:  I'm using <code>sls -pattern</code> to filter out content in the raw HTML output.  If there's a more reliable way to identify certain items in the raw output, I wouldn't mind seeing an example.</p>
  
  <pre class="lang-psh prettyprint-override"><code>$UserAgent = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET CLR 1.1.4322; InfoPath.3; MS-RTC LM 8; .NET4.0E)'
  $r=Invoke-WebRequest -Uri 'http://web.ourwebserver.com/test.aspx' -UseDefaultCredentials -SessionVariable WebSession
  $form = $r.Forms[0]
  
  $fields = Invoke-WebRequest -Uri 'http://web.ourwebserver.com/test.aspx' -WebSession $WebSession | select -ExpandProperty inputfields | select name, value
  
  $viewstate = $fields | ?{$_.name -match "VIEWSTATE"} | select -ExpandProperty value
  $eventvalidation = $fields | ?{$_.name -match "EVENTVALIDATION"} | select -ExpandProperty value
  
  $form.Fields["__EVENTVALIDATION"] = $eventvalidation
  $form.Fields["ctl00`$MainContent`$phone"] = "454-454-2345"
  $form.Fields["ctl00`$MainContent`$Submit"] = "Submit"
  
  $response = Invoke-WebRequest -Uri 'http://web.ourwebserver.com/test.aspx' -WebSession $WebSession -Method POST -Body $form.Fields -ContentType 'application/x-www-form-urlencoded'
  $result = $response.rawcontent
  </code></pre>
  
  <p><strong>EDIT:  Below, is my first attempt at getting the script to work.  Anyway, it's obviously not doing the same thing as my WORKING Invoke-WebRequest script above</strong></p>
  
  <pre class="lang-psh prettyprint-override"><code>$URL = "http://web.ourwebserver.com/test.aspx"
  $wc = new-object net.WebClient
  $wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
  $wc.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET CLR 1.1.4322; InfoPath.3; MS-RTC LM 8; .NET4.0E)")
  $wc.UseDefaultCredentials = $true
  
  ### EventValidation/ViewState Extraction Code ###
  $probe = $wc.downloadData($url)
  $s = [text.encoding]::ascii.getString($probe)
  $start = $s.indexOf('id="__VIEWSTATE"', 0) + 24
  $end = $s.indexOf('"', $start)
  $viewstate = $s.substring($start, $end-$start)
  $start = $s.indexOf('id="__EVENTVALIDATION"', 0) + 30
  $end = $s.indexOf('"', $start)
  $eventvalidation = $s.substring($start, $end-$start)
  ###
  
  $NVC = New-Object System.Collections.Specialized.NameValueCollection
  $NVC.Add("__EVENTVALIDATION", $eventvalidation)
  $NVC.Add("ctl00`$MainContent`$phone", "454-454-2345")
  $NVC.Add("ctl00`$MainContent`$Submit", "Submit")
  $wc.QueryString = $NVC
  
  $Result = $WC.UploadValues($URL,"POST", $NVC)
  
  [System.Text.Encoding]::UTF8.GetString($Result)
  
  $WC.Dispose();
  </code></pre>