Tuesday, 29 March 2011

New-Credential.ps1

  1. <# 
  2. .SYNOPSIS 
  3.    A function to create a credential object from script. 
  4. .DESCRIPTION 
  5.    Enables you to create a credential objects from stored details. 
  6. .NOTES 
  7.     File Name  : New-Credential.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://pshscripts.blogspot.com/2011/03/new-credentialps1.html 
  13. .PARAMETER UserId 
  14.    The userid in the form of "domain\user" 
  15. .PARAMETER Password 
  16.    The password for this user 
  17. .EXAMPLE 
  18.    New-Credential contoso\administrator  Pa$$w0rd 
  19. #> 
  20.  
  21. function New-Credential { 
  22. param ( 
  23. [string] $Userid, 
  24. [string] $Pwd 
  25. # Create the credential 
  26. $spwd = ConvertTo-SecureString -AsPlainText $pwd -Force  
  27. $cred = New-Object System.Management.Automation.PSCredential $userid,$spwd 
  28. # Now return it to the caller 
  29. return $cred 
  30.  
  31. # Call the function to demostrate example 
  32. New-Credential "contoso\administrator" "Pa$$w0rd" 
Technorati Tags: ,,

2 comments:

Fer Sánchez said...

It really works!! ...thank u so much!!

Thomas said...

You are most welcome. I find this a fantastic trick to use with testing and for me in the classroom. Glad you a) found it and b) found it useful!