该脚本正确安装了驱动器,但重新启动计算机后驱动器未保留:
\nfunction RemapDrive {\n param( \n $DriveLetter,\n $FullPath,\n $Credential \n )\n \n Write-Host "Trying to remove $DriveLetter in case it already exists ..." \n # $DriveLetter must be concatenated with ":" for the command to work\n net use "${DriveLetter}:" /del\n \n ## $DriveLetter cannot contain ":"\n $psDrive = New-PSDrive -Name "$DriveLetter" -PSProvider "FileSystem" -Root "$FullPath" -Credential $Credential -Scope "Global" -Persist\n \n Write-Host "$DriveLetter was successfully added !" \n}\n\nfunction BuildCredential {\n param (\n $Username,\n $Password\n )\n $pass = ConvertTo-SecureString $Password -AsPlainText -Force\n $credential = New-Object System.Management.Automation.PSCredential ($Username, $pass)\n return $credential\n}\n\n$credential = (BuildCredential -Username "xxxxxx" -Password "yyyyyy")[-1]\n\nRemapDrive -DriveLetter "X" -FullPath "\\\\my-server\\x" -Credential $credential\n\nRun Code Online (Sandbox Code Playgroud)\n我发现了什么:
\n\n\n\xe2\x80\x9c当您在本地范围内设置命令时(即,没有点源),Persist 参数不会在运行命令的范围之外保留 PSDrive 的创建。如果您在脚本内运行 New-PSDrive,并且希望新驱动器无限期地保留,则必须对该脚本进行点源化。为了获得最佳结果,要强制保留新驱动器,除了将 Persist 添加到命令中之外,还应指定 Global 作为 Scope 参数的值。\xe2\x80\x9d
\n
我尝试使用“. .\\my-script.ps1”执行脚本(对脚本进行点源?),但结果是相同的。
\n尝试使用“网络使用”和注册表来尝试添加网络驱动器也将我带入了死胡同。
\n眼镜:
\nWindows 10 家庭版
\nPowershell版本:
\nMajor Minor Build Revision\n----- ----- ----- --------\n5 1 18362 1171\nRun Code Online (Sandbox Code Playgroud)\n
基本上,New-PSDrive没有/SAVECRED来自 的参数net use,并且不会以运行脚本的用户以外的用户身份持久映射驱动器。
有三种方法可以处理这个问题:
New-PSDrive -Name "$DriveLetter" -PSProvider "FileSystem" -Root "$FullPath" -Scope 'Global' -Persist无凭据标志。这假设您的文件共享允许 kerberos 登录,因此在某些边缘情况下可能不起作用。net use,并包含用户名、密码/persistent:yes和/savecred。这可以在 powershell 中完成,没有任何问题。HKCU\Network\[drive letter]\ConnectionType = 1HKCU\Network\[drive letter]\DeferFlags= 4| 归档时间: |
|
| 查看次数: |
3460 次 |
| 最近记录: |