Guest User

Untitled

a guest
Feb 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. param([Parameter(Mandatory=$true)][string]$hostname)
  2.  
  3. #-----------------------------------------------------------------------
  4. #- Get-SavedCredentials
  5. #-----------------------------------------------------------------------
  6. function Get-SavedCredentials {
  7. $savedCredentials = C:\Windows\System32\cmdkey.exe /list | Select-String 'TERMSRV/' | ForEach-Object {
  8. Write-Output ($_.ToString().Replace('Target: LegacyGeneric:target=','')).Trim()
  9. }
  10. return $savedCredentials
  11. }
  12.  
  13. #-----------------------------------------------------------------------
  14. #- Remove-SavedCredentials
  15. #-----------------------------------------------------------------------
  16. Function Remove-SavedCredentials {
  17. Get-SavedCredentials | ForEach-Object {
  18. $formattedEntry = $_.Replace('Target: Domain:target=','')
  19. C:\Windows\System32\cmdkey.exe /delete:$formattedEntry
  20. }
  21. }
  22.  
  23. #-----------------------------------------------------------------------
  24. #- Add-SavedCredential
  25. #-----------------------------------------------------------------------
  26. Function Add-SavedCredential {
  27. param([string]$hostname,[string]$savedCredentialPath='C:\users\paul\Documents\encCrd.xml')
  28. $ec = Import-CliXML -Path $savedCredentialPath
  29. $username = $ec.Username.ToString().Trim()
  30. $password = $ec.GetNetworkCredential().Password.ToString().Trim()
  31. C:\Windows\System32\cmdkey.exe /generic:TERMSRV/$hostname /user:$username /pass:$password
  32. Remove-Variable ec,username,password
  33. }
  34.  
  35. #-----------------------------------------------------------------------
  36. #- Main
  37. #-----------------------------------------------------------------------
  38.  
  39. Remove-SavedCredentials
  40. Add-SavedCredential -hostname $hostname
Add Comment
Please, Sign In to add comment