远程使用PowerShell安装MSU文件时遇语法错误求助
问题
通过Invoke-Command远程执行PowerShell脚本,下载并安装Win8.1AndW2K12R2-KB3191564-x64.msu文件时,触发「文件名、目录名或卷标语法不正确」错误,该错误仅在远程执行时出现。
执行脚本
Write-Host "Downloading file..." if((Test-Path -Path "C:\Temp\Win8.1AndW2K12R2-KB3191564-x64.msu") -eq $true) { Remove-Item -Path "C:\Temp\Win8.1AndW2K12R2-KB3191564-x64.msu" } Invoke-WebRequest -Uri "https://go.microsoft.com/fwlink/?linkid=839516" -OutFile "C:\Temp\Win8.1AndW2K12R2-KB3191564-x64.msu" if((Test-Path -Path "C:\Temp\Win8.1AndW2K12R2-KB3191564-x64.msu") -eq $true) { Write-Host "Installing file..." wusa /PackagePath:C:\Temp\Win8.1AndW2K12R2-KB3191564-x64.msu /quiet /norestart } else { Write-Host "File not downloaded" }
错误信息
The filename, directory name, or volume label syntax is incorrect. At line:1 char:9 + $results = Invoke-Command -Computername $Computer -Credential ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (The filename, d...x is incorrect.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError
排查思路与解决方法
检查远程机器的C:\Temp目录是否存在
远程执行时脚本不会自动创建目录,若C:\Temp不存在,Invoke-WebRequest写入文件会触发路径错误。在脚本开头添加目录创建逻辑:if (-not (Test-Path -Path "C:\Temp")) { New-Item -Path "C:\Temp" -ItemType Directory -Force | Out-Null }给wusa的路径添加引号
原生EXEwusa对路径解析可能存在兼容性问题,将/PackagePath的路径用双引号包裹:wusa "/PackagePath:C:\Temp\Win8.1AndW2K12R2-KB3191564-x64.msu" /quiet /norestart或者改用PowerShell的
Start-Process调用,避免原生EXE的路径解析问题:Start-Process -FilePath "wusa.exe" -ArgumentList "/PackagePath:C:\Temp\Win8.1AndW2K12R2-KB3191564-x64.msu", "/quiet", "/norestart" -Wait验证远程会话的权限与路径映射
远程PowerShell会话可能存在权限不足或路径映射异常,先在远程会话中执行Test-Path "C:\"和Get-Acl "C:\",确认执行脚本的账号拥有C:\Temp的读写权限,以及运行wusa的权限。检查Invoke-Command的脚本传递方式
若通过-ScriptBlock传递脚本,可能因字符串转义导致路径被篡改。建议将脚本保存为本地PS1文件,通过-FilePath参数传递:Invoke-Command -ComputerName $Computer -Credential $Cred -FilePath ".\Install-KB.ps1"
内容的提问来源于stack exchange,提问作者Pie
相关产品推荐
相关产品推荐

