空路径名不合法 - DTEXEC with Powershell

问题描述 投票:0回答:1

我尝试从本地计算机在远程服务器上使用 Powershell 运行 DTSX,但文件路径变量未到达 DTSX 执行中,因此我收到错误“空路径名不合法。”

这是我运行的命令的示例:

$server = "server name"
$credential = Get-Credential

$command = @'
"G:\Program Files\Microsoft SQL Server\160\DTS\Binn\DTExec.exe" /F "G:\insert-data.dtsx" ^
/SET \Package.Variables[User::file_data].Value;"\\servername\data.csv" ^
/SET \Package.Variables[User::file_log].Value;"G:\insert_data_log.txt" ^
/SET \Package.Variables[User::db_destination_server].Value;"servername" ^
/SET \Package.Variables[User::db_destination_name].Value;"dbname" ^
/SET \Package.Variables[User::db_destination_user].Value;"username" ^
/SET \Package.Variables[User::db_destination_password].Value;"pass" ^
/SET \Package.Variables[User::db_destination_table].Value;"tablename" ^
/SET \Package.Variables[User::days].Value;1
'@

Invoke-Command -ComputerName $server -Credential $credential -ScriptBlock {
    param($cmd)
    try {
        Start-Process -FilePath "cmd.exe" -ArgumentList "/c `"$cmd`"" -NoNewWindow -Wait -RedirectStandardOutput "C:\Temp\command_output.txt" -RedirectStandardError "C:\Temp\command_error.txt"
    } catch {
        $_ | Out-File -FilePath "C:\Temp\remote_command_error.txt"
    }
} -ArgumentList $command

file_log 变量由我的“Start LOG”任务使用,在执行进程时为空。这是我收到的错误:

Microsoft (R) SQL Server Execute Package Utility
Version 16.0.4085.2 for 64-bit
Copyright (C) 2022 Microsoft. All rights reserved.

Started:  12:58:39
Progress: 2024-12-18 12:58:40.81
   Source: Data Flow Task 
   Validating: 0% complete
End Progress
Progress: 2024-12-18 12:58:40.95
   Source: Data Flow Task 
   Validating: 100% complete
End Progress
Error: 2024-12-18 12:58:41.50
   Code: 0x00000001
   Source: Start LOG 
   Description: Empty path name is not legal.
End Error
Warning: 2024-12-18 12:58:41.50
   Code: 0x80019002
   Source: insert_data_mambu 
   Description: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED.  The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
End Warning
DTExec: The package execution returned DTSER_FAILURE (1).
Started:  12:58:39
Finished: 12:58:41
Elapsed:  1.984 seconds

我尝试将文件的路径放在双引号和单引号中,但没有任何效果。 任何建议都会受到欢迎!

powershell ssis remote-server dts dtexec
1个回答
0
投票

问题是通过

cmd.exe
参数调用
/C
的 CLI 不支持传递 multiline 代码 - 第一行之后的所有内容都会被悄悄忽略。

如果您想保留多行源代码定义以提高可读性/可维护性,您可以在使用前以编程方式将其转换为单行。

也就是说,将以下语句放在

try
语句之前:

# Transform the multiline source code to a single-line form.
$cmd = $cmd -replace '\^\r?\n'
  • 上面使用基于正则表达式的
    -replace
    运算符从多行源代码中删除所有行延续。
© www.soinside.com 2019 - 2024. All rights reserved.