PowerShell function to runas a different user

Script grab or sets password to and from file. Using this method this allows to emulate the runas command and you are able to run commands in PowerShell with -credential without having to type a password. This allows for automation of tasks were multiple accounts are required.

3.7 Star
13,100 times
Add to favorites
Active Directory
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question

  • Using the same pwd file for different users
    4 Posts | Last post January 10, 2014
    • Hi.  I created a pwd file but when another user tries to use it, it returns the following error: a Key not valid for use in specified state.
      How do i allow other users to use the same pwd file or do they all need their own pwd files.  Thanks.
    • Hello Jek,
      Indeed this is what keeps the files secure, the encryption is based on user accounts. It is possible to change this, for example by using the machine encryption key on the server or computer. This would allow all local administrators to use the same set of key files. If this is something that you would need, I could write something up for you.
    • Hello Jaap,
      Like jek, i want to use the same pwd file for many users.
      have you write somethink for Jek, and my i have the solution.
    • Another solution to this problem might be to share a password through "LastPass"  this way you can also remove anyone immediately if their Job function changes or they are terminated. 
  • Now that the password is in -credential, then what?
    2 Posts | Last post June 07, 2012
    • -credential (C:\Script\PowerShellRunAs.ps1 -get contoso\svc_remoterestart \\fileserver\share\file.pwd)
      So I create the file.pwd, and then run the above function to put the value in -credentail. This is an admin account.
      In the simplest form if I run Get-Process as a limited user, how do I run Get-Process with the value in -credential? Do I use Invoke-Item, RUNAS? 
    • Hello Dale, indeed this script is easier to use in combination with Cmdlets that actually support the -Credential parameter. For example when using this in combination with Get-WmiObject the command would look like this:
      Get-WmiObject -Class Win32_Bios -ComputerName Server01 -Credential (C:\Script\PowerShellRunAs.ps1 -get contoso\svc_remoterestart \\fileserver\share\file.pwd)
      Since Get-Process does not support another alternative is required. If we have a look at Invoke-Command you could execute it with the following line of code:
      Invoke-Command -ComputerName localhost -Credential (.\PowerShellRunAs.ps1 -Get contoso\svc_remotestart \\fileserver\share\file.pwd) -ScriptBlock {Get-Process}
  • Security
    2 Posts | Last post March 26, 2012
    • How secure ist this solution?
      Is it possible to brute force the password?
    • Please note that this was not designed with security in mind, my usage scenario for this is to allow my PowerShell installation scripts to elevate its privileges to setup certain services and perform operations that require elevation or a different account. The password files are cleaned up at the end of the installation.
      Brute force is not required, as anyone could load the password file and use it to execute commands with password permissions. They would need to know the username associated with the password file, so I would recommend obfuscating the file names to it is not obvious which account the file is associated with. Secondly I would put strict NTFS permissions on the folder that holds the files and encryption if possible. And never use administrative accounts.
      If you want security you should not store your password, this is more a convenience script in cases where security is not essential.