gpt4 book ai didi

powershell - 将共享映射到 powershell 中未分配的驱动器号

转载 作者:行者123 更新时间:2023-12-04 01:36:10 24 4
gpt4 key购买 nike

我正在使用 powershell 映射我的网络驱动器,如下所示。

$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("r:", "$Folder","FALSE","Domain\UserName", "Password")

但在这里我发送驱动器号为 r: 。如果 r: 驱动器已被使用,它将失败。

有没有办法将驱动器映射到未分配的驱动器号。 (例如,如果 R: 已分配但 S: 未分配,则它应自动映射到 S: 驱动器)

最佳答案

首先,您需要获得下一个可用的驱动器盘符。这是来自 Archive of PowerShell.com 的示例:

function Get-NextFreeDrive {
68..90 | ForEach-Object { "$([char]$_):" } |
Where-Object { 'h:', 'k:', 'z:' -notcontains $_ } |
Where-Object {
(new-object System.IO.DriveInfo $_).DriveType -eq 'noRootdirectory'
}
}

来自 PowerShell.com:

It starts by enumerating ASCII codes for letters D: through Z:. The pipeline will then convert those ASCII codes into drive letters. Next, check out how the pipeline uses an exclusion list with drives you do not want to use for mapping (optional). In this example, drive letters h:, k:, and z: are never used. Finally, the pipeline uses a .NET DriveInfo object to check whether the drive is in use or not.

$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive((Get-NextFreeDrive)[0], "$Folder","FALSE","Domain\UserName", "Password")

关于powershell - 将共享映射到 powershell 中未分配的驱动器号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8799643/

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