running powershell as another user

I am trying to use Powershell to manage a new Exchange 2010 server. My regular credentials will not work for managing the server, I need to run as my admin account.

However I am not sure how to do this and also have powershell run as administrator since I am working on Windows 7.

Any ideas on how to do this? for MMC I have a batch script that uses runas, but I cannot get that to work on my local machine for running powershell.
LVL 12
ryan80Asked:
Who is Participating?
 
QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
In your RUNAS attempt you have mixed up the quotes. You do not need quotes after -command, which simplifies things, and the working command line should look like this:
runas /user:domain\admin_account "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command . 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto"

Open in new window

You should be able to omit the path to powershell.exe, to make the command more lean.
0
 
jeiben812Commented:
The '-Credential DOMAINNAME\Username' option works for most, if not all, PowerShell cmdlets.  You'll probably also want to look into the 'Get-Credential' cmdlet.  It's very helpful for integrating runas credentials into scripts.
0
 
ryan80Author Commented:
is there a way to elevate a particular instance of Powershell to the desired admin account?
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
morpheiosCommented:
Syntax
      RUNAS [/profile] [/env] [/netonly] /user:user Program

Key
   /profile   Option to load the user's profile (registry)
   /env       Use current environment instead of user's.
   /netonly   Use the credentials specified only for remote connections.
   /user      Username in form USER@DOMAIN or DOMAIN\USER
              (USER@DOMAIN is not compatible with /netonly)
   Program    The command to execute SELECT powershell.exe
Enter the password when prompted.

When you start a program with RunAs /netonly, the program will execute on your local computer as the user you are currently logged on as, but any connections to other computers on the network will be made using the user account specified.

Without /netonly everything will run under the user account specified.
0
 
ryan80Author Commented:
how do I run as admin as well as run as another user? I am having issues with select when not run as admin.
0
 
ryan80Author Commented:
I am trying to create a batch script that will allow me to launch the Powershell. here is the script

runas /user:domain\admin_account "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command '. 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1; Connect-ExchangeServer -auto''"

I get an error of unexpected token at char:15
0
 
ryan80Author Commented:
I cannot find any way so far to do this. If i launch cmd from runas and then powershell from there, it doesnt run as an administrator and things dont work correctly.

The only way that I have gotten things to run correctly is by logging onto a machine directly as my admin account and then running powershell as admin. There has to be a better way.
0
 
jeiben812Commented:
Have you considered using the Get-Credential cmdlet?  This will allow you to store the runas credentials as a variable in the script.

$Credential = Get-Credential

Open in new window


You can then specify the credentials for each cmdlet where the admin credentials are required by using the -Credential option.

Further details and example code from Technet here:
http://technet.microsoft.com/en-us/library/cc764264.aspx
0
 
ryan80Author Commented:
I guess that i could do this, just that it would be nice to not have to enter the extra command every time.

The easiest thing to do for me to run this, is log onto a server with the tools and run it from there. Just seems like there would be an easier way to do this.
0
 
jeiben812Commented:
You can still do it that way.  If you put the "$Credential = Get-Credential" at the beginning of the script, then use the "-PSCredential $Credential" option for each command you would only need to enter the credentials once.  Effectively, this would be the same as a runas for the entire script.

Also, there are ways to store the credentials in the script but that means storing the password in plain text.  I strongly suggest not doing so.

--Jeff
0
 
ryan80Author Commented:
I am not just running scripts though. I am working with cmdlets to add, remove, update folders, mailboxes.......

So it is not just a matter of running a script, but instead running many commands. Everytime would be tedious. Additionally I can even run the cmdlets, because unless the Powershell is run as an administrator, they are not even recognized as an option.
0
 
ryan80Author Commented:
Thank you for catching that. This was what I was looking for.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.