title: Calling WNetAddConnection2 from PowerShell
body: <p>I am trying to directly call some Win32 API's from a PowerShell script. I managed to make WNetAddConnection work with the code below:</p>
  
  <pre><code>$cp = New-Object Microsoft.CSharp.CSharpCodeProvider
  $cpar = New-Object System.CodeDom.Compiler.CompilerParameters
  
  $Code = @"
  using System;
  using System.Runtime.InteropServices;
  namespace Win32Api
  {
      public class Net
      {
          [DllImport("mpr.dll", EntryPoint = "WNetAddConnection")]
          public static extern uint Map(string lpRemoteName, string lpPassword, string lpLocalName);
          [DllImport("mpr.dll", EntryPoint = "WNetCancelConnection")]
        public static extern uint Delete(string lpName, byte fForce);     
      }
  }
  "@
  
  $cp.CompileAssemblyFromSource($cpar, $code)
  [Win32Api.Net]::Map("\\REMKOLAPTOP\C$", $null, "W:")
  </code></pre>
  
  <p>Now I would like to do the same <a href="http://msdn.microsoft.com/en-us/library/aa385413%28VS.85%29.aspx" rel="nofollow noreferrer">WNetAddConnection2</a>, although I found the proper <a href="http://www.pinvoke.net/default.aspx/mpr.wnetaddconnection2" rel="nofollow noreferrer">declarations for C#</a> I am unsure how to declare this for usage in PowerShell. So my questions is: what is the proper "translation" and an example of calling the API would be great.</p>
  
  <p>Please note that I do not want to use non api methods like the one below:</p>
  
  <pre><code>$net = $(New-Object -Com WScript.Network)
  $net.MapNetworkDrive("u:", "\\computer\share")
  </code></pre>