gpt4 book ai didi

powershell - New-PSDrive 的 "-Persist"标志不起作用 : drives removed on reboot

转载 作者:行者123 更新时间:2023-12-02 18:56:41 24 4
gpt4 key购买 nike

脚本正确安装驱动器,但重新启动计算机后驱动器未保留:

function RemapDrive {
param(
$DriveLetter,
$FullPath,
$Credential
)

Write-Host "Trying to remove $DriveLetter in case it already exists ..."
# $DriveLetter must be concatenated with ":" for the command to work
net use "${DriveLetter}:" /del

## $DriveLetter cannot contain ":"
$psDrive = New-PSDrive -Name "$DriveLetter" -PSProvider "FileSystem" -Root "$FullPath" -Credential $Credential -Scope "Global" -Persist

Write-Host "$DriveLetter was successfully added !"
}

function BuildCredential {
param (
$Username,
$Password
)
$pass = ConvertTo-SecureString $Password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($Username, $pass)
return $credential
}

$credential = (BuildCredential -Username "xxxxxx" -Password "yyyyyy")[-1]

RemapDrive -DriveLetter "X" -FullPath "\\my-server\x" -Credential $credential

我发现了什么:

“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 parameter in addition to adding Persist to your command.”

我尝试使用“. .\my-script.ps1”执行脚本(对脚本进行点源?),但结果是相同的。

使用“net use”和注册表来尝试添加网络驱动器也让我陷入了死胡同。


规范:

Windows 10 家庭版

Powershell 版本:

Major  Minor  Build  Revision
----- ----- ----- --------
5 1 18362 1171

最佳答案

基本上,New-PSDrive 没有来自 net use/SAVECRED 参数,并且不会以用户身份持久映射驱动器除了运行脚本的人之外。

有三种方法可以解决这个问题:

  1. [推荐]修复文件共享权限,而不是使用单独的用户名/密码,然后使用 New-PSDrive -Name "$DriveLetter"-PSProvider "FileSystem"-Root "$ FullPath"-Scope 'Global' -Persist 没有凭证标志。这假设您的文件共享允许 Kerberos 登录,因此在某些边缘情况下可能不起作用。
  2. 使用net use,并包含用户名、密码、/persistent:yes/savecred。这可以在 powershell 中完成,没有任何问题。
  3. 设置启动时必须运行的 powershell 脚本。
  4. 设置脚本以使用凭证管理器 - 请参阅答案 here
  • 安装 CredentialManager powershell 模块
  • 设置HKCU\Network\[驱动器盘符]\ConnectionType = 1
  • 设置HKCU\Network\[驱动器号]\DeferFlags= 4

关于powershell - New-PSDrive 的 "-Persist"标志不起作用 : drives removed on reboot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66105668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com