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:
- Test role base access control
- 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
Pingback: Creating a OcsPowerShell PSSession under a different set of credential | MSDN Blogs