Note that there are some explanatory texts on larger screens.

plurals
  1. POImpersonation throws FileNotFoundException with WindowsIdentity in Powershell
    primarykey
    data
    text
    <p>I am encountering a somewhat weird error with performing impersonation in PowerShell and C#. Executing the folowing code does not show any errors. </p> <pre><code>PSObject result = null; using (PowerShell powershell = PowerShell.Create()) { RunspaceConfiguration config = RunspaceConfiguration.Create(); powershell.Runspace = RunspaceFactory.CreateRunspace(config); powershell.Runspace.Open(); powershell.AddScript(String.Format(CmdletMap[PSVocab.OsBootTime], this.ComputerName)); result = powershell.Invoke().First(); powershell.Runspace.Close(); } return DateTime.Parse(result.ToString()); </code></pre> <p>where the PS script for <code>CmdletMap[PSVocab.OsBootTime]</code> is simply: </p> <pre><code>$info = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer ; $info.ConvertToDateTime($info.LastBootUpTime) </code></pre> <p>The above C# code works fine locally. However, once I had this same block with Windows impersonation like so:</p> <pre><code>WindowsIdentity ImpersonatedIdentity = new WindowsIdentity(ImpersonateUserName); WindowsImpersonationContext impersonatedContext = ImpersonatedIdentity.Impersonate(); try { PSObject result = null; using (PowerShell powershell = PowerShell.Create()) { RunspaceConfiguration config = RunspaceConfiguration.Create(); powershell.Runspace = RunspaceFactory.CreateRunspace(config); powershell.Runspace.Open(); powershell.AddScript(String.Format(CmdletMap[PSVocab.OsBootTime], this.ComputerName)); result = powershell.Invoke().First(); powershell.Runspace.Close(); } return DateTime.Parse(result.ToString()); } catch (Exception ex) { // do logging here } </code></pre> <p>I get the following exception: </p> <pre><code>FileNotFoundException: C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll </code></pre> <p>and debugging shows that it fails at <code>RunspaceConfiguration.Create()</code>. Not sure why though.</p> <p>The DLL though is already registered in the GAC though as well as being referenced in the project itself. Also confirmed that the paths and version are correct. </p> <p>References taken from:</p> <ul> <li><a href="http://blogs.msdn.com/b/powershell/archive/2007/09/10/impersonation-and-hosting-powershell.aspx" rel="nofollow noreferrer">Impersonation and Hosting PowerShell</a></li> <li><a href="https://stackoverflow.com/questions/2656553/asp-net-powershell-impersonation">ASP.NET PowerShell Impersonation</a></li> </ul> <p>Can someone give an insight to this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload