?
Solved

running powershell as another user

Posted on 2011-10-25
12
Medium Priority
?
2,495 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:ryan80
12 Comments
 
LVL 2

Expert Comment

by:jeiben812
ID: 37027351
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
 
LVL 12

Author Comment

by:ryan80
ID: 37027731
is there a way to elevate a particular instance of Powershell to the desired admin account?
0
 
LVL 6

Expert Comment

by:morpheios
ID: 37027758
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Author Comment

by:ryan80
ID: 37027799
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
 
LVL 12

Author Comment

by:ryan80
ID: 37027867
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
 
LVL 12

Author Comment

by:ryan80
ID: 37033179
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
 
LVL 2

Expert Comment

by:jeiben812
ID: 37033497
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
 
LVL 12

Author Comment

by:ryan80
ID: 37056988
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
 
LVL 2

Expert Comment

by:jeiben812
ID: 37057721
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
 
LVL 12

Author Comment

by:ryan80
ID: 37062639
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
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 37063150
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
 
LVL 12

Author Closing Comment

by:ryan80
ID: 37063210
Thank you for catching that. This was what I was looking for.
0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Loops Section Overview

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question