我有一个到 Azure 存储共享的连接字符串,如下所示:
DefaultEndpointsProtocol=https;
AccountName=myaccount;
AccountKey=xxx_LongEncryptedString_xxx;
EndpointSuffix=core.windows.net
此外,我有一个共享名,例如“
MyShare
”。在 C# 中,我这样连接:
ShareClient share = new ShareClient(connectionString, "MyShare");
问题:我想使用 Powershell 或 cmd 将此共享连接到 Windows (10 / 11 Pro) 中的驱动器。我该怎么做?
编辑: 我无法登录 Azure 门户。我只有如上所示的信息。
如何将 Azure 共享连接到 Windows 驱动器号
您可以使用以下脚本通过 Powershell 连接字符串将 Azure 文件共享连接到您的 Windows。
脚本:
$connectionString = "DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxxxx;EndpointSuffix=core.windows.net"
$shareName = "share1" # Replace with your Azure File Share name
$driveLetter = "Z" # Desired drive letter
$connectionStringDict = @{}
$connectionString.Split(';') | ForEach-Object {
$key, $value = $_ -split '=', 2
$connectionStringDict[$key] = $value
}
$accountName = $connectionStringDict["AccountName"]
$accountKey = $connectionStringDict["AccountKey"]
$endpointSuffix = $connectionStringDict["EndpointSuffix"]
$fileSharePath = "\\$accountName.file.$endpointSuffix\$shareName"
# Test connectivity to port 445
Write-Host "Testing connectivity to $accountName.file.$endpointSuffix on port 445..."
$connectTestResult = Test-NetConnection -ComputerName "$accountName.file.$endpointSuffix" -Port 445
if ($connectTestResult.TcpTestSucceeded) {
Write-Host "Connection successful! Proceeding with drive mapping..."
# Save credentials for persistence
Write-Host "Saving credentials..."
cmd.exe /C "cmdkey /add:`"$accountName.file.$endpointSuffix`" /user:`"localhost\$accountName`" /pass:`"$accountKey`""
# Map the drive
Write-Host "Mapping drive $driveLetter to $fileSharePath..."
New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root $fileSharePath -Persist
Write-Host "Drive mapped successfully! You can access it as ${driveLetter}:"
} else {
Write-Error -Message "Unable to reach $accountName.file.$endpointSuffix via port 445. Check your network configuration or consider using VPN or other tunneling options."
}
输出:
Testing connectivity to venkat326123.file.core.windows.net on port 445...
Connection successful! Proceeding with drive mapping...
Saving credentials...
CMDKEY: Credential added successfully.
Mapping drive Z to \\venkat326123.file.core.windows.net\share1...
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
Z 1.05 102398.95 FileSystem \\venkat326123.file.core.windows...
Drive mapped successfully! You can access it as Z: