title: How can I simulate clicking a link in order to execute aspx server side code, using Powershell 2.0?
body: <p>This is somewhat SharePoint related, but I think the essence of the question is not actually SharePoint specific.</p>
  
  <p>Here's the situation.  I'm using a third party SharePoint add-on.  When this add-on is installed, it provides a link in the SharePoint Library Settings page for record libraries.  If you click on the link, it executes some code and does some configuration to the library (installs custom content types, etc.).</p>
  
  <p>The link itself executes some Javascript:</p>
  
  <pre><code>function dialogCallback()
  {
      SP.UI.Notify.addNotification('Content types installed and settings configured.');                                                        
      SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
  }
  function openInstallContentTypesDialog()
  { 
      var options = SP.UI.$create_DialogOptions();
      options.url = 'http://my.sharepoint.site/_layouts/Custom/Feature/ExecuteConfigurationCode.aspx?SiteUrl=http://my.sharepoint.site&amp;ListId={6767676a-0101-b4dc-a67a-67a67a67a67a}';
      options.dialogReturnValueCallback = Function.createDelegate(null, dialogCallback);
      SP.UI.ModalDialog.showModalDialog(options);
  }                
  openInstallContentTypesDialog();
  </code></pre>
  
  <p>I need to configure ~50 libraries, so I would rather not have to go through the UI into each library's settings page and click that link.  (Especially since I will have to do it more than once, as I set up the farm on multiple dev/test/etc environments.)  I'd rather write a Powershell script, cycle through all the libraries, and somehow simulate clicking that link and calling the <code>ExecuteConfigurationCode.aspx</code> page.  I know I can get the information needed for the query string (site url and list ID) through Powershell, but I can't figure out how to get the aspx page's code to fire.</p>
  
  <p>I did some digging around, and found some posts that say to use a <code>System.Net.WebClient</code> object and call <code>DownloadString()</code> on it.  Something like:</p>
  
  <pre><code>$web = get-spweb http://my.sharepoint.site
  $list = $web.Lists["My Record Library"]
  $requestUrl = $web.Url+"/_layouts/Custom/Feature/ExecuteConfigurationCode.aspx"
  $querystring = new-object system.collections.specialized.namevaluecollection
  $querystring.Add("SiteUrl", $web.Url)
  $querystring.Add("ListId", "{"+$list.Id.Tostring()+"}")
  $webclient = new-object system.net.webclient
  $webclient.Credentials = new-object system.net.networkcredential("Site Collection Admin", "password")
  $webclient.QueryString = $querystring
  $response = $webclient.downloadstring($requestUrl)
  </code></pre>
  
  <p>And, although I do get all of the HTML that would comprise the page, the code behind the aspx page is not executed.</p>
  
  <p>I have also tried the <code>UploadString()</code> method to see if it needs to be a POST.  That had the same result -- all the HTML of the page was returned to me, but the server side code of the aspx page was not executed.</p>
  
  <p>So... how can I simulate clicking that link through Powershell?  (Also keep in mind that since it is SharePoint, I'm using the SharePoint 2010 Management Shell, which is Powershell 2.0.  So I can't use the <code>Invoke-WebRequest</code> cmdlet.)</p>