Reputation: 483
I am trying to automate network drive mapping. Because of a Windows issue, my drives don't persist after reboot and I get misleading error messages. To overcome this, I wrote a powershell script.
"Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again."
Actually, these are simply same set of credentials to the same server but different folders. Well, it is Windows/Microsoft, I won't or can't fix them. As a workaround, I have this script, which works on other computers.
Remove-PSDrive -Name "P"
Remove-PSDrive -Name "T"
$User = "DOMAIN\username"
$Credential = Get-Credential -UserName $User -Message "Enter password"
New-PSDrive -Name "P" -PSProvider FileSystem -Root "\\host\Dir_A" -Credential $Credential -Persist
New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\host\Dir_B" -Credential $Credential -Persist
This script works on another PC with Windows 10 Pro but not on Windows 11 Home. When run second time, I get an error that the password is wrong without being prompted for the password. It somehow skips that step. I can't also run it in powershell.
I wonder if I am doing something wrong.
Upvotes: 0
Views: 177
Reputation: 483
This is not a solution but if anyone having similar issues, I suspected that the credentials are cached. But that wasn't the reason. I used the cmdkey.exe
command to cache and clear cached credentials to solve my actual problem.
Basically, fist I delete the cached credential by target name and then I prompt for credentials. Then I cache those credentials again. This way, user does not need to reboot to correct incorrectly given password or for whatever reason needs to reenter the credentials.
cmdkey.exe /delete:<targetname>
e.g. cmdkey.exe /delete:subdomain.domain.com
after prompt
cmdkey.exe /delete:<targetname>
e.g. cmdkey.exe /add:subdomain.domain.com /user:<username> /pass:<password>
You can get password from the $credentials
object with $credentials.GetNetworkCredential().Password
.
This is a temporary solution for me but works for now, although it is not the solution for the above mentioned problem.
Upvotes: 0