Hey,
I got a problem, I want to run a command/script as a different user in Exchange Powershell out of the normal Powershell. I got a Java Application which should automatically execute these commands and show the output in an swing application (different topic).
Java can only execute normal Powershell so I execute Exchange (2007) Powershell scripts with this command (Command 1):
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". 'PathToScript'"
But the user who starts the Java Program has no rights to read on the Exchange Server. So I need to somehow execute this script as a different user. I tried to do the following:
$username = 'DOMAIN\userWithExchangeRights' $password = 'P4$$w0rd' $cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
But I'm not sure how to run the command above (Command 1) with these credentials because when I save the command in another script (lets call it script1) and try to execute
Invoke-Command -FilePath script.ps1 -Credential $cred -ComputerName localhost
I always get the error:
[localhost] Connecting to remote server localhost failed with the following error message : Access is denied.+ CategoryInfo : OpenError: (localhost:String) [], PSRemotingTransportException+ FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken
So the basic structure looks like this Java - Powershell - Exchange Powershell - Execute Script as different user. So how to realize this scenario? What I'm doing wrong? For me it doesnt matter when the user credentials change I would just need the output of the script in the first Powershell that is opened from Java.
I tried a lot but nothing worked so I highly appriciate any support. Thank you in advance!!