title: Executing Powershell script as different User in Exchange 2007 Powershell
body: <p>My scenario looks like this:</p>
  
  <p>Java opens a Powershell in which Exchange Powershell Command/Scripts should be executed as a different user and the output should be displayed in the Powershell windows that Java opened (so Java can read the output).
  So: <code>Normal Powershell --&gt; Add Exchange functionality --&gt; Execute Script/Command as different user</code></p>
  
  <p>To add Exchange functionality to the normal Powershell I use either
  <code>add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin</code> or start Powershell like this <code>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". 'PathToScript/script1.ps1'"</code></p>
  
  <p>The problem is the execution as a different user:</p>
  
  <ul>
  <li><code>runAs</code> (or other tools like PSEXEC or minirunAs) is not working because it opens a new window so the output is not shown in the powershell window opened by Java (an therefore cant be read by Java) and is not suitable for automation</li>
  <li>I tried 2 different ways to do it with Powershell:</li>
  </ul>
  
  <p>Way 1:</p>
  
  <pre><code>$username = 'domain\user'
  $password = 'Pa$$w0rd'
  $cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
  Invoke-Command -Credential $cred -ComputerName localhost -FilePath PathToScript/script1.ps1
  </code></pre>
  
  <p>But i get the following Error:</p>
  
  <pre><code>An Active Directory error 0x80072020 occurred while searching for domain controllers in domain MYDOMAIN: An operations error occurred.
  + CategoryInfo          : NotSpecified: (0:Int32) [Get-MailboxStatistics], ADTransientException
  </code></pre>
  
  <p>A simple <code>whoami</code> works this way an prints the user specified in $username but according to this link it looks like that this is not possible with Exchange command (but I dont know how reliable the source is): <a href="http://thwack.solarwinds.com/thread/40524" rel="nofollow">http://thwack.solarwinds.com/thread/40524</a></p>
  
  <p>Way 2 (as suggested here <a href="http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/f805cbe0-bca9-401a-a381-a7f5520244d2" rel="nofollow">http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/f805cbe0-bca9-401a-a381-a7f5520244d2</a>):</p>
  
  <pre><code>$computerName = "localhost"
  $username = 'domain\user'
  $password = 'Pa$$w0rd'
  $cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
  $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$computerName/powershell -Credential $cred
  Import-PSSession $session
  </code></pre>
  
  <p>But the problem here is that the URI <code>http://$computerName/powershell</code> does not exist (I get WinRM 502 exception) and I dont know how to get a Powershell Virtual Directory on a Server where just the Exchange 2007 Management Tools are installed.</p>
  
  <p>So my questions are: Is there another way to do this? What am I doing wrong in Way 1 &amp; 2 (more how can I add the Powershell VD with Way 2)? Is it possible at all?</p>
  
  <p>I run Java (7 x64) on a WinSrv2012 with Exchange 2007 Management Tools installed. The Exchange Server runs on version 2007. The script is <code>Get-MailboxStatistics -server ExSrv</code>.</p>
  
  <p>This bothers me for nearly a week now so I highly appreciate any help.</p>