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

为何路径存在时Move-Item仍抛出无效路径错误?

SQL代理作业PowerShell脚本Move-Item路径无效问题解决

问题情况

SQL代理作业运行PowerShell脚本从Blob容器拉取文件,按扩展名做不同处理:

  • .gz文件能正常解压到M:\sys\in\
  • 处理.csv/.dat文件时,Move-Item报错路径无效,但该路径已经被验证可用

脚本代码

$LoadPath = "M:\sys\in\" 

$blobs = gci $LandTempPath | where { $_.PSIsContainer -eq $false }

foreach ($file in $blobs) {
    $extension = [System.IO.Path]::GetExtension($file.FullName)

    switch -Regex ($extension)
    {
        '^(.dat|.csv)$' {
            $file | Move-Item -Destination $LoadPath -Force
        }
        '^(.gz|.gzip)$' {
            $file | Inflate -DestPath $LoadPath -RemoveSource $true
        }
        '^.txt' {
            if ([System.IO.Path]::GetFileNameWithoutExtension($file) -eq 'PLCHD_FL')
            {
                $file | Remove-Item -Force
            }
            else {
                $file | Move-Item -Destination $LoadPath -Force
            }
        }
        default {
            $file | Move-Item -Destination $QuarantinePath -Force
        }
    }
}

错误提示

Executed as user: NT Service\SQLSERVERAGENT. A job step received an error at line 32 in a PowerShell script.
The corresponding line is '$file | Move-Item -Destination $LoadPath -Force '. Correct the script and reschedule the job.
The error information returned by PowerShell is: 'Invalid Path: 'M:\sys\in'. '. Process Exit Code -1. The step failed.

解决办法

  • 明确指定目标文件完整路径:直接传目录给Move-Item时,PowerShell对映射路径/Blob挂载路径可能解析异常,改成指定具体文件名:

    # 替换原Move-Item代码行
    $targetPath = Join-Path $LoadPath $file.Name
    $file | Move-Item -Destination $targetPath -Force
    
  • 标准化路径格式:路径末尾的斜杠可能引发解析问题,先统一路径格式:

    $LoadPath = [System.IO.Path]::GetFullPath("M:\sys\in\")
    
  • 确认SQL代理账户权限:虽然.gz文件处理成功,但解压操作可能临时改动了目录权限,要确保NT Service\SQLSERVERAGENT账户对M:\sys\in\有读写修改权限。如果M:\是网络映射盘,建议改用UNC路径(比如\\server\share\sys\in\),避免映射加载问题。

  • 检查文件对象类型:如果$LandTempPath是Blob容器的挂载路径,$file可能不是标准本地文件对象,可先把文件复制到本地临时目录再处理,避免特殊对象导致的移动异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 14:20:14