Thursday, September 27, 2012

Run IE using different user credentials

To test a web application, quite often we need to log on in different user roles.  Many years ago I created a shortcut to do the work, with help of some third-part tool. But, to keep the environment simple, is there a way to do that directly?

It's not as easy as it looks like.

At first, I tried the DOS command "runas". It doesn't work for IE.
Then, I tried "Task Scheduler". Not easy to let the task program open in current desktop window.
The last one: PowerShell script.

Save the scripts below to a PowerShell script file "RunIEasTestUser1.ps1", then right click the file, select "Run with PowerShell" from the context menu.

========================

$username = "DomainName\TestUserLogin1"
$password = "somepassword" | ConvertTo-SecureString -asPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username,$password)

Start-Process -FilePath "C:\Program Files (x86)\Internet Explorer\iexplore.exe" -Credential $cred -passthru -LoadUserProfile -ArgumentList "http://testserver"

8 comments:

  1. Hi Eric,

    I stumbled upon your page and wanted to do something very similar. However, rather than passively capturing the credentials, I would prefer to be prompted interactively to type in my credentials and also be able to pass it through launching IE like what you did. Would you be able to provide the code that can do this?

    Thanks,

    Donald

    ReplyDelete
  2. Hi Donald,

    You can put the script below into the powershell script file, to replace the hard coded password.

    And, you can write similar code to get user name.

    $password = Read-Host -Prompt "Enter password of user $username:" -AsSecureString

    ReplyDelete
    Replies
    1. Hi Eric,

      Thank you so much for the suggested code! It works. The line you provided did have a small syntax issue. It needed a {} around the username variable. I corrected it with the following:

      $password = Read-Host -Prompt "Enter password of user ${username}:" -AsSecureString

      However, I can't get the username portion to work with the following:

      $username = Read-Host -Prompt "Enter username of user ${username}:" -AsSecureString

      What I ended up using for everything is the following. Not ideal, but it asks for the username within PS, while the password is prompted in a separate window. I would have liked the username and password to both get prompted in the prompt window. If you have time, try it and let me know if you can revise and have it working. I'll check back to your site on Monday.

      $username = Read-Host -Prompt "Enter username of user"
      $password = Read-Host -Prompt "Enter password of user ${username}:" -AsSecureString
      $cred = New-Object System.Management.Automation.PSCredential($username,$password)
      Start-Process -FilePath "C:\Program Files (x86)\Internet Explorer\iexplore.exe" -Credential $cred -passthru -LoadUserProfile -ArgumentList "http://testsite"


      Thanks again for your help.

      Donald

      Delete
  3. Hi Eric,

    Internet explorer is not visible when I use your code to open IE with different user credentials by selecting "Run with PowerShell" from the context menu.

    ReplyDelete
    Replies
    1. could you try "C:\Program Files\Internet Explorer\iexplore.exe"? Do you have local administrator rights?

      Delete
  4. Thanks for your reply Eric.

    This is the code I am using to launch IE with different user credentials and I checked my username is available in the local administrator group.

    $username = "QAMR-ACCOUNTS\tQPMTest01"
    $password = "QAccess124" | ConvertTo-SecureString -asPlainText -Force
    $cred = New-Object System.Management.Automation.PSCredential($username,$password)

    Start-Process -FilePath "C:\Program Files\Internet Explorer\iexplore.exe" -Credential $cred -passthru -LoadUserProfile -ArgumentList "http://google.com/"

    But the command prompt is getting opened and it got disappeared when I run the above Power shell script as you mentioned.

    My requirement is: I need to open IE with different user credentials and navigate to application URL.

    Each user have different rights on the application and I need to perform some testing on that.

    Please help me to resolve this issue and let me know if you are not clear.

    Thanks in advance.

    ReplyDelete
  5. Eric,

    Now Internet explorer is getting opened and visible when I run Powershell script from the command prompt as below and navigated to the URL which I have given.

    PowerShell.exe -File C:\Users\q788289\Desktop\RunIEasTestUser1_1.ps1

    But I don't know how to pass the credentials when pop-up appears in the same IE.

    Note: The same user credentials(which already mentioned in the code to open IE for different user credentials) I have to pass to the pop-up

    Please help me in this regard.

    ReplyDelete