title: How to reference PowerShell Module within ASP.NET Site
body: <p>I'm trying to figure out how to use the Microsoft Online Services Migration Toolkit PowerShell Commands from within an ASP.NET website (using vb.NET).</p>
  
  <p>I've started off using a guide on how to use PowerShell in ASP.NET - from here: <a href="http://devinfra-us.blogspot.com/2011/02/using-powershell-20-from-aspnet-part-1.html" rel="nofollow">http://devinfra-us.blogspot.com/2011/02/using-powershell-20-from-aspnet-part-1.html</a></p>
  
  <p>I'm trying to work out how to implement the Online Services Migration Toolkit PowerShell cmdlets.</p>
  
  <p>Here is a snippet from my code-behind:</p>
  
  <pre><code>Sub GetUsers()
  
      Dim iss As InitialSessionState = InitialSessionState.CreateDefault()
      iss.ImportPSModule(New String() {"MSOnline"})
      Using myRunSpace As Runspace = RunspaceFactory.CreateRunspace(iss)
          myRunSpace.Open()
  
          ' Execute the Get-CsTrustedApplication cmdlet.
          Using powershell As System.Management.Automation.PowerShell = System.Management.Automation.PowerShell.Create()
  
              powershell.Runspace = myRunSpace
  
              Dim connect As New Command("Get-MSOnlineUser -Enabled")
              Dim secureString As New System.Security.SecureString()
              Dim myPassword As String = "ThePassword"
  
              For Each c As Char In myPassword
                  secureString.AppendChar(c)
              Next
  
              connect.Parameters.Add("Credential", New PSCredential("admin@thedomain.apac.microsoftonline.com", secureString))
              powershell.Commands.AddCommand(connect)
  
              Dim results As Collection(Of PSObject) = Nothing
              Dim errors As Collection(Of ErrorRecord) = Nothing
  
              results = powershell.Invoke()
              errors = powershell.Streams.[Error].ReadAll()
  
              For Each obj As PSObject In results
                  Response.Write(obj.Properties("Identity").Value.ToString())
              Next
  
          End Using
      End Using
  
  End Sub
  </code></pre>
  
  <p>When I try to run the code via the page, I'm getting the following error</p>
  
  <blockquote>
    <p>The term 'Get-MSOnlineUser -Enabled' is not recognized as the name of
    a cmdlet, function, script file, or operable program. Check the
    spelling of the name, or if a path was included, verify that the path
    is correct and try again.</p>
  </blockquote>
  
  <p>So I'm guessing I haven't worked out how to import the Online Services Migration Toolkit PowerShell CmdLets. I'm also not exactly sure if the line:</p>
  
  <pre><code>iss.ImportPSModule(New String() {"MSOnline"})
  </code></pre>
  
  <p>Is exactly correct. Is there a way I can verify the Module name?</p>
  
  <p>I'm also unsure of where and how to reference the .dll files. At the moment I have copied them to my bin folder but I can't add them as references, so how does the ImportPSModule statement know where to find them? Especially when the website is published to the final production server.</p>
  
  <p>One other question, should I be using the x86 or x64 cmdlets? I'm developing on Win7 x64, but not sure if the website builds as x86 or x64? Do I need to find out what architecture the server is?</p>