how to launch powershell as another user

717 Views Asked by At

Currently if I manually connect to my admin windows account and launch this code, it works fine, but if someone that does not have admin credentials, the "enable-mailbox does not work because it cannot find the database. I did some research and it is because he does not have the permissions. Is there any way to run this code as another user providing the username and password??

PowerShell powershell = PowerShell.Create();
            PSSnapInException ex;
            powershell.Runspace.RunspaceConfiguration.AddPSSnapIn(
                "Microsoft.Exchange.Management.PowerShell.Admin", out ex);
            if (ex != null)
                throw ex;
            powershell.AddScript(". $env:ExchangeInstallPath\\bin\\RemoteExchange.ps1");
            powershell.AddScript("Connect-ExchangeServer -auto");
            powershell.Invoke();
            powershell.Streams.ClearStreams();
            powershell.Commands.Clear();
            powershell.AddCommand("Enable-Mailbox");
            powershell.AddParameter("Identity", "username");
            powershell.AddParameter("Database", @"databasePath");
            var result = powershell.Invoke();
0

There are 0 best solutions below