Powershell New-PSDrive Persist Between Login

余生长醉 提交于 2021-01-27 17:26:42

问题


I am using New-PSDrive to map network drives. My issue is that despite using the -Persist and -Scope Global parameters the drives no longer show up at the next Windows login for the user. The drives require credentials to access which I pass into the New-PSDrive command. I can see that the drives still exist after a logout and login by using net use at the command line, but the drives are not available. I believe what might be occurring is that Windows does not store the credentials and therefore cannot reconnect, but the drives do not show in Windows Explorer at all after logout and login. I am looking for a way to persist the drives as if I had created them through the "Map Network Drive" option in Windows File Explorer and checked the "Remember my credentials" option after selecting to use different credentials. Is this possible in Powershell? If not is there another Windows scripting language that would support this functionality? Here is my code:

#iterate through array of connection information and map drives
foreach ($connection in $connections)
{
    #split out array into variables
    $driveletter,$key,$account,$location = $connection

    $acctKey = ConvertTo-SecureString -String $key -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PSCredential -ArgumentList $account, $acctKey
    New-PSDrive -Name $driveletter -PSProvider FileSystem -Root $location -Scope Global -Credential $credential -Persist -ErrorAction SilentlyContinue
}

回答1:


My guess is that instead of New-PSDrive you'll need to use net use with the /PERSISTENT:YES and the /SAVECRED parameters in order to (a) create a persistent drive mapping that (b) also remembers the specified credentials (that's the aspect that New-PSDrive seems to be lacking).

Based on the code in your question, try something like:

net use $driveLetter $location $key /USER:$account /PERSISTENT:YES /SAVECRED



回答2:


I have a similar issue with a customer.

Going through the documentation i found the following:

When you scope the command locally, that is, without dot-sourcing, the Persist parameter does not persist the creation of a PSDrive beyond the scope in which you run the command. If you run New-PSDrive inside a script, and you want the new drive to persist indefinitely, you must dot-source the script. For best results, to force a new drive to persist, specify Global as the value of the Scope parameterin addition to adding Persist to your command.




回答3:


I was running across the same problem with a drive mapping script I had created. The credentials would not get saved and the drives disappeared from the File Explorer, but showed as unavailable in net use. It doesn't seem like PowerShell has a built-in method for accessing the Credential Manager. I should also mention that the drives I am mapping are in a different domain than the host machine and accessed via VPN.

For Credential Management, I pushed out this module to all of the workstations that needed to run this script: https://www.powershellgallery.com/packages/CredentialManager/2.0 That gives you the ability to save credentials to the Windows Credential Manager from within PowerShell.

I also created a custom WPF form that stills gets credentials securely (not relevant to the answer but I just needed more flexibility with the GUI).

This allowed me to get and save the credentials securely, however upon a reboot or logoff logon, the drives still disappeared from the File Explorer and showed as unavailable in net use.

Then I started thinking, what happens when you map a network drive via net use with /persist and /savecred or via File Explorer and tick the remember password box? So I checked the registry.

HKCU\Network\[drive letter]

There are 2 incorrect values that control how the File Explorer uses and shows mapped drives.

ConnectionType and DeferFlags.

I noticed that when mapping via New-PSDrive these 2 values were not being set correctly. So in my script after successfully mapping the drive and successfully saving credentials to the Credential Manager, I set ConnectionType to 1 and DeferFlags to 4.

Now after a reboot, the drives are persisted in the File Explorer and upon connecting to VPN and accessing the drives, the connections are restored with no need to enter a password. Hope this helps someone.




回答4:


Updating the Registry keys works for persists file mount.

I used the below lines to update the registry keys.

$registryPath="HKCU:\Network\R" New-ItemProperty -Path $registryPath -Name ConnectionType -Value 1 -PropertyType DWORD -Force | Out-Null New-ItemProperty -Path $registryPath -Name DeferFlags -Value 4 -PropertyType DWORD -Force | Out-Null



来源:https://stackoverflow.com/questions/49201332/powershell-new-psdrive-persist-between-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!