Authentication and Credentials



The PowerShell cmdlets included in NetCmdlets allow you to perform authentication using either plain text (with the -User and -Password string parameters) or with a PSCredential object (through the -Credential parameter).

Here's an example with the Get-LDAP cmdlet.

  1. Bind to the directory server using plain text parameters, like this:

    PS> Connect-LDAP -Server myserver -BindDN mydomain\admin -Password admin

  2. Or, bind using a PSCredential object, like this:

    PS> $myCred = Get-Credential
    PS> Connect-LDAP -Server myserver -Cred $mycred

    Note: It is also possible to bind with credentials even if you're not using Active Directory (ie, if you're binding with Novell or OpenLDAP).

Here's another example using the Get-FTP cmdlet.

  1. Connecting to an FTP server supplying a plain text user and password:

    PS> Get-FTP -Server myserver -User myusername -Password mypassword

  2. Or, connecting to an FTP server using a PSCredential object:

    PS> $myCred = Get-Credential
    PS> Get-FTP -Server myserver -Cred $mycred

Both of the above examples that use the -Credential parameter assume that a PSCredential object was already created and stored in the $mycred variable. The process of manually creating the PSCredential object can be avoided by saving your password in a securestring file and creating the new PSCredential object on the fly, using those file contents, like so:

PS> Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File C:\mysecstr.txt

PS> $pass = Get-Content C:\mysecstr.txt | ConvertTo-SecureString

PS> $mycred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "myusername",$pass