Powershell,映射网络驱动器(如果不存在)



我刚开始学习powershell和脚本,现在还处于起步阶段。我想征求你的意见和建议。

使用Powershell,我想为我的用户提供一个脚本,他们可以根据需要手动连接网络驱动器。驱动器字母总是预设的:T、U、V、W、X。在连接之前,我想检查驱动器是否已经连接或正在使用。如有必要,绘制地图。你将如何解决这个问题?

您将使用带有-Persist选项的New PSDrive。我们还使用Get-PSDrive和一些逻辑来确定它们是否正在使用并映射到正确的位置。

$Name = "T"
$Path = "\Server01MyShare"
$MappedDrive = (Get-PSDrive -Name $Name -ErrorAction SilentlyContinue)
#Check if drive is already mapped
if($MappedDrive)
{
#Drive is mapped. Check to see if it mapped to the correct path
if($MappedDrive.DisplayRoot -ne $Path)
{
# Drive Mapped to the incorrect path. Remove and readd:
Remove-PSDrive -Name $Name
New-PSDrive -Name $Name -Root $Path -Persist -PSProvider "FileSystem"
}
}
else
{
#Drive is not mapped
New-PSDrive -Name $Name -Root $Path -Persist -PSProvider "FileSystem"
}

相关内容

最新更新