You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell映射网络驱动器读取安装日志报错,请求排查问题

问题:PowerShell映射网络驱动器失败导致日志读取异常

我写了一个PowerShell脚本,逻辑是映射网络驱动器读取安装日志,验证安装状态后取消映射,但最近运行报错,不知道哪里遗漏了。

错误信息

New-PSDrive : The local device name has a remembered connection to another network resource At C:\Windows\TEMP\jenkins3036451124142125854.ps1:31 char:1 
+ New-PSDrive -Name M -PSProvider FileSystem -Root \"${LogFilePath}\" -Cr ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo : InvalidOperation: (M:PSDriveInfo) [New-PSDrive], Win32Exception 
    + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Com mands.NewPSDriveCommand Set-Location : Cannot find drive. A drive with the name 'M' does not exist. At line:1 char:1
     
+ Set-Location $MyInvocation.MyCommand.Name 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo : ObjectNotFound: (M:String) [Set-Location], Drive NotFoundException 
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetL ocationCommand Get-ChildItem : Cannot find drive. A drive with the name 'M' does not exist. At C:\Windows\TEMP\jenkins3036451124142125854.ps1:35 char:20 
    
+ ${LatestLogFile} = Get-ChildItem M:\ | Sort CreationTime -Descending ... 
+ ~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (M:String) [Get-ChildItem], Driv eNotFoundException 
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetC hildItemCommand ---------- latest log file is ----------- Get-Content : Cannot find drive. A drive with the name 'M' does not exist. At C:\Windows\TEMP\jenkins3036451124142125854.ps1:39 char:22 

+ $Installer_LogFile = Get-Content -LiteralPath M:\${LatestLogFile} 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo : ObjectNotFound: (M:String) [Get-Content], DriveN otFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetC ontentCommand

原脚本代码

# Logic to read the log file and verify
${LogFilePath} = “\\inv-r4-bvt-21\c$\FhirInstalls\$Source\installer\logs”
$User = \"cor\inbuildu\"
$PWord = ConvertTo-SecureString -String \"******\" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
New-PSDrive –Name M –PSProvider FileSystem –Root \"${LogFilePath}\" -Credential $Credential –Persist -Scope Global

M:
${LatestLogFile} = Get-ChildItem M:\ | Sort CreationTime -Descending | Select -First 1
Write-Output \"---------- latest log file is -----------\"
Write-Output ${LatestLogFile}

$Installer_LogFile = Get-Content -LiteralPath M:\${LatestLogFile}
$Install_Complete_Message = $Installer_LogFile | %{$_ -match \"STEP: Install of FHIR is complete!\"}
$Install_Complete_Message_Components = $Installer_LogFile | %{$_ -match \"STEP: Install of selected component\(s\) is complete!\"}

if (($Install_Complete_Message -contains $true) -or ($Install_Complete_Message_Components -contains $true))
{
    Write-Host \"Fhir API installed succesfully!\"
} else {
    Write-Host \"Failed to install Fhir API!!\"
    $LastExitCode = 1
    exit $LastExitCode 
}
C:
Remove-PSDrive M

if ($RemoteSession){remove-pssession $RemoteSession}

解决方案

核心问题分析

错误提示The local device name has a remembered connection to another network resource是关键:M盘已经和其他网络资源存在缓存连接,导致无法重复映射;此外脚本还存在路径引号错误、直接切换驱动器的不稳定性、缺乏错误处理等问题。

修复要点

  • 清理残留映射:映射前先移除M盘的现有连接,避免冲突
  • 替换全角引号:将${LogFilePath}的中文全角引号改为英文半角引号,避免路径解析失败
  • 移除不必要的持久化映射:去掉-Persist参数,避免创建重启后仍存在的驱动器映射
  • 添加错误捕获:对映射、文件读取等关键步骤添加错误处理,确保脚本异常时能正确退出并清理资源
  • 优化路径访问:避免直接用M:切换驱动器,改用Join-Path拼接路径,提升稳定性

修改后的完整脚本

# 读取日志并验证安装状态逻辑
$LogFilePath = "\\inv-r4-bvt-21\c$\FhirInstalls\$Source\installer\logs"
$User = "cor\inbuildu"
$PWord = ConvertTo-SecureString -String "******" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord

# 清理已有M盘映射(兼容两种方式确保清理干净)
Remove-PSDrive -Name M -Force -ErrorAction SilentlyContinue
net use M: /delete /y 2>&1 | Out-Null

# 映射网络驱动器,添加错误捕获
try {
    New-PSDrive –Name M –PSProvider FileSystem –Root $LogFilePath -Credential $Credential –Scope Global -ErrorAction Stop
}
catch {
    Write-Host "映射网络驱动器M:失败:$_"
    exit 1
}

# 获取最新日志文件
$LatestLogFile = Get-ChildItem -Path M:\ | Sort-Object CreationTime -Descending | Select-Object -First 1
Write-Output "---------- 最新日志文件是 -----------"
Write-Output $LatestLogFile.Name

# 读取日志内容,添加错误捕获
try {
    $Installer_LogFile = Get-Content -LiteralPath (Join-Path M:\ $LatestLogFile.Name) -ErrorAction Stop
}
catch {
    Write-Host "读取日志文件失败:$_"
    Remove-PSDrive M -Force
    exit 1
}

# 验证安装完成状态
$Install_Complete_Message = $Installer_LogFile | Where-Object { $_ -match "STEP: Install of FHIR is complete!" }
$Install_Complete_Message_Components = $Installer_LogFile | Where-Object { $_ -match "STEP: Install of selected component\(s\) is complete!" }

if ($Install_Complete_Message -or $Install_Complete_Message_Components) {
    Write-Host "Fhir API安装成功!"
} else {
    Write-Host "Fhir API安装失败!"
    $LastExitCode = 1
    exit $LastExitCode 
}

# 清理驱动器映射
Remove-PSDrive M -Force

# 清理远程会话(如果存在)
if ($RemoteSession) { Remove-PSSession $RemoteSession }

内容的提问来源于stack exchange,提问作者g90uagk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:54:25