powershell 使用PowerShell映射驱动器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 使用PowerShell映射驱动器相关的知识,希望对你有一定的参考价值。


# Using Test-Path instead of Get-PSDrive and trying to map the drive without and then with credentials makes sure the script works in all circumstances: 
#    also when the drive was already mapped in Windows Explorer or in a PowerShell remoting session

# We map the drive with -Perist to make sure the drive is mapped in the Windows file explorer as well. Without -Persist the drive is only available in PowerShell.
# The -Scope Global parameter is used to make sure the drive is available outside of this script as well

$sharePath = "\\server\share\optional-path"
$mapDrive = "Z"

if(-not (Test-Path "${mapDrive}:")) {
    # try to map the network drive without asking for credentials; this works on a local PowerShell session
    New-PSDrive -Name $mapDrive -Root $sharePath -Persist -Scope Global -PSProvider "FileSystem" -ErrorAction SilentlyContinue | Out-Null
}
if(-not (Test-Path "${mapDrive}:")) {
    # ask for credentials; we need this when the current user does not have rights to use the network share, or when on a PowerShell remoting session
    New-PSDrive -Name $mapDrive -Root $sharePath -Persist -Scope Global -PSProvider "FileSystem" -Credential (whoami) | Out-Null
}

if(Test-Path "${mapDrive}:") {
	# Show how the Z drive is mapped
    Get-PSDrive $mapDrive | Select-Object -Property @('Root', 'DisplayRoot')
}

以上是关于powershell 使用PowerShell映射驱动器的主要内容,如果未能解决你的问题,请参考以下文章

powershell 使用PowerShell映射Windows网络驱动器

Powershell:如何使用不同的用户名/密码映射网络驱动器

powershell 适用于标准版和企业版的SharePoint PowerShell许可证映射

使用 Azure Devops 任务或 PowerShell 创建/更新 Azure WebApp 的路径映射

powershell 映射网络驱动器

Powershell学习之道-文件夹共享及磁盘映射