I created a number of drive mapping scripts since my job requires accessing a large number of files on different servers, in a separate domain, based on the task at hand. After fat-fingering my password into Get-Credential a few times and locking my account in the other domain, I added code to test my credentials prior to executing the series of New-PSDrive commands. Since then, I have noticed my account getting locked after running the scripts, despite using the correct username and password. Here is the sanitized code I am using:
Write-Host "Press any key to enter <Second Domain> credentials and map <Second Domain> domain drives..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
## Prompt for credentials
$Credential = $host.ui.PromptForCredential("Need <Second Domain> credentials", "Please enter MCG\username and password.", "", "")
$UserName = $Credential.UserName
$Password = $Credential.GetNetworkCredential().password
$CurrentDomain = "LDAP://DC=<Second Domain>,DC=com"
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)
if ($domain.name -eq $null)
{
write-output "Authentication failed - please verify your username and password."
exit #terminate the script.
}
else
{
write-output "Successfully authenticated with domain " $domain.name
}
## Map Drives in <Second Domain> domain using credentials supplied via pop-up
New-PSDrive -Name H -PSProvider FileSystem -Root \\<Server Name>.<Second Domain>.com\C$ -Credential $Credential -Persist
New-PSDrive -Name I -PSProvider FileSystem -Root \\<Server Name>.<Second Domain>.com\C$ -Credential $Credential -Persist
New-PSDrive -Name J -PSProvider FileSystem -Root \\<Server Name>.<Second Domain>.com\E$ -Credential $Credential -Persist
New-PSDrive -Name K -PSProvider FileSystem -Root \\<Server Name>.<Second Domain>.com\F$ -Credential $Credential -Persist
When I watch the logs on the domain controllers, I see a failed logon using credentials for the domain I am in followed by a successful logon using the supplied credentials for each New-PSDrive command executed.
Can somebody help me understand what is going on here and why the account I supplied credentials for is getting locked out?