We can prompt window to get credentails of user with the help of `Get-Credentials commandlet and it will captured password as secureString [System.Security.SecureString] ```sh Get-Credential -UserName $env:UserName -Message 'Please enter password' ``` Alternative way to capature password as secureString [System.Security.SecureString] ```sh $pw =Read-Host "Enter Password" -AsSecureString ``` `cmdkey` utility is used to add\delete\list domain,generic,smart card & rsa credentials into windows credentials store - Syntax ```sh To list available credentials: cmdkey /list cmdkey /list:targetname To create domain credentials: cmdkey /add:targetHostOrIp /user:username /pass:password cmdkey /add:targetHostOrIp /user:username cmdkey /add:targetHostOrIp /smartcard To create generic credentials: The /add switch may be replaced by /generic to create generic credentials cmdkey /generic:targetHostOrIp /user:username /pass:password cmdkey /generic:targetHostOrIp /user:username cmdkey /generic:targetHostOrIp /smartcard To delete existing credentials: cmdkey /delete:targetHostOrIp To delete RAS credentials: cmdkey /delete /ras ``` - Example ```sh ## Adding generic credentials into windows credential store for Server1 cmdkey /generic:Server1 /user:$userName /pass :$pwd ## Adding generic credentials with TERMSRV into windows credential store for Server2 cmdkey /generic:TERMSRV/Server1 /user:$userName /pass :$pwd ##Adding domain credentials into windows credential store for Server3 cmdkey /add:Server3 /user:$userName /pass:$pwd ##delete saved credentials from windows credential store for Server1 cmdkey /generic:Server1 ##delete saved credentials from windows credential store for Server2 cmdkey /generic:Server2 ##delete saved credentials from windows credential store for Server3 cmdkey /generic:Server3 ``` ### Here is complete script to connect to list of servers using mstsc terminal with saved credentials using PowerShell. > Note: 1. Server host names should be replaced with actual servers in adding into webBackendActiveServers 2. DR Server host Names should be replaced with actual servers in adding into webBackendDRServers ```sh $webBackendServers=New-Object Collections.Generic.List[String] $webBackendActiveServers=New-Object Collections.Generic.List[String] $webBackendDRServers=New-Object Collections.Generic.List[String] $webBackendActiveServers.AddRange([string[]]@("BE-Server1","BE-Server2","BE-Server3","BE-Server4","BE-Server5","BE-Server6")) $webBackendDRServers.AddRange([string[]]@("BE-DR-Server1","BE-DR-Server2","BE-DR-Server3","BE-DR-Server4","BE-DR-Server5","BE-DR-Server6")) $credentials = Get-Credential -UserName $env:UserName -Message 'Please enter password' $userName =$credentials.UserName $credentials.Password $pwd=$credentials.GetNetworkCredential().Password $webBackendServers.AddRange($webBackendActiveServers) $webBackendServers.AddRange($webBackendDRServers) $webBackendServers | Foreach-Object { ## Adding credentials into windows credential store for hostname cmdkey /generic:$_ /user:$userName /pass :$pwd ##If above command doesn't work then try with below command to add credentials into windows credential store for hostname ##cmdkey /generic:TERMSRV/$_ /user:$userName /pass :$pwd ##If above two command don't work then try with below command to add credentials into windows credential store for hostname ##cmdkey /add:$_ /user:$userName /pass:$pwd mstsc /v:$_ /f } $remove =cmdkey /list | &{Process {if ($_ -like "*Target=*" -and $webBackendServers.Contains($_.Split("=")[1].Trim())){ $_.Split("=")[1].Trim()}}}; $remove| &{Process { ## clearing credentials from windows credential store for hostname cmdkey /delete:$_ ##if use TERMSRV while adding credentials into windows credentials store then uncomment below line of code ##cmdkey /delete:TERMSRV/$_} }; ```
Azure Cloud Solution Architect, Full-Stack Development in .Net Eco system, Senior Manager at Capgemini
Thursday, March 23, 2023
How to connect to List of servers using mstsc terminal with saved credentials using PowerShell
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment