title: Call WslRegisterDistribution function from wslapi.dll in PowerShell
body: <p>I am making a PowerShell script to install any Linux userspace from compressed tarballs (.tar.gz). But it always return some number like this <code>2147943746</code>. I am using <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/mt826870(v=vs.85).aspx" rel="nofollow noreferrer"><code>WslRegisterDistribution</code></a> function from <code>wslapi.dll</code>. The syntax in C++ is as follows:</p>
  
  <pre><code>HRESULT WINAPI WslRegisterDistribution(
    _In_ PCWSTR distroName,
    _In_ PCWSTR tarGzFilename
  );
  </code></pre>
  
  <p>My PowerShell script is as follows:</p>
  
  <pre><code>$X=Read-Host "Enter Distro Name"
  $Y=Read-Host "Enter .tar.gz file path"
  Write-Host "Distro name '$X' and file path '$Y'"
  pause
  
  $MethodDefinition=@'
  [DllImport("wslapi.dll", CharSet = CharSet.Unicode)]
  public static extern uint WslRegisterDistribution(string distributionName, string tarGzFilename);
  '@
  
  $WslApi=Add-Type -MemberDefinition $MethodDefinition -Name 'Wslapi' -PassThru
  $WslApi::WslRegisterDistribution("$X", "$Y");
  </code></pre>
  
  <p>I can call that method using C++/C# but I can not do that in PowerShell. In C# I import the function and pass two command arguments in its parameter shown as below:</p>
  
  <pre><code>[DllImport("wslapi.dll", CharSet = CharSet.Unicode)]
  public static extern uint WslRegisterDistribution(string distributionName, string tarGzFilename);
  </code></pre>
  
  <p>Is that possible to import that in PowerShell? How can I corrent the script to do that? See my alternative GitHub project <strong><a href="https://github.com/Biswa96/WSLInstall" rel="nofollow noreferrer">https://github.com/Biswa96/WSLInstall</a></strong>.</p>
  
  <h3>Further readings:</h3>
  
  <ul>
  <li><a href="https://msdn.microsoft.com/en-us/library/windows/desktop/mt826874(v=vs.85).aspx" rel="nofollow noreferrer">WSL Functions</a></li>
  <li><a href="https://blogs.technet.microsoft.com/heyscriptingguy/2013/06/25/use-powershell-to-interact-with-the-windows-api-part-1/" rel="nofollow noreferrer">Use PowerShell to Interact with the Windows API: Part 1</a></li>
  </ul>