Creating a Powershell session under a different set of credentials

By | May 4, 2014

Below code can be handy if you need to create a powershell session under a different credential than the one you are logged into.
This allow to support multiple scenarios:

  1. Test role base access control
  2. Increase privilege for important operation

[powershell]
# Avoid displaying the UI prompt when creating credential object
Set-ItemProperty ‘HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds’ ConsolePrompting $true
# The user name
$userName = "<em>username</em>"
# The domain
$domain = "<em>domain</em>"
# Complete user\domain string
$UserDomain = $domain + "\" + $username
# The password (secure string)
$secure_string_pwd = convertto-securestring "<em>password</em>" -asplaintext -force
# Create the powershell credential object
$cred = new-object management.automation.pscredential $UserDomain,$secure_string_pwd
# Change the title of the powershell console
(get-host).ui.rawui.windowtitle = $userName
# Create the Powershell session (in this example I create a session to OcsPowerShell)
$session = New-PSSession -ConnectionUri "https://<em>domain</em>/OcsPowerShell" -Credential $cred
# Import the session
Import-PSSession $session
[/powershell]
Hope this works well for you too. Ping me otherwise πŸ™‚

Ahmet

 
0 Kudos
Don't
move!

One thought on “Creating a Powershell session under a different set of credentials

  1. Pingback: Creating a OcsPowerShell PSSession under a different set of credential | MSDN Blogs

Thoughts?