传递路径参数时从 ruby 调用 Sqlcmd 时出错 运行
Error running Invoke-Sqlcmd from ruby when passing path argument
def executeSql(sqlFile)
#exec("powershell $env:gitroot");
puts "executing file: #{sqlFile}"
command="powershell Invoke-Sqlcmd -InputFile #{sqlFile} -ServerInstance \"localhost\" -Database \"dbName\" -Verbose"
command.gsub! '/','\'
puts "command: #{command}"
if($firstRun)
puts 'inside first run'
importCommand = `powershell Import-Module \"sqlps\" -DisableNameChecking`
printf importCommand
$firstRun=false
end
puts 'made it past first run'
printf `#{command}`
puts 'command execution complete'
end
错误
Invoke-Sqlcmd : A positional parameter cannot be found that accepts argument 'Static'.
当我 运行 在 ruby 之外的 powershell 中使用相同的命令时,它会正常执行。我做错了什么?
更新
我发现用硬编码路径 C:\test\test.sql
替换 #{sqlFile}
可以解决问题。我试过 sqlFile.gsub! '/', '\\'
并没有明显区别(仍然是标准 \
windows 路径)。
我也试过了
command="powershell Invoke-Sqlcmd -InputFile #{Shellwords.escape(sqlFile)} -ServerInstance \"localhost\" -Database \"dbName\" -Verbose"
创建
Invoke-Sqlcmd -InputFile C:\Git\project\sql\db\01\ -\ Static\ Data\file.name.here.sql -ServerInstance "localhost" -Database "dbName" -Verbose
如果不是文件路径中的 01\ -\ Static\
部分,我认为这会起作用。
更新 2
我找到了有用的东西并将其发布为答案。 @tadman 建议对 system
使用多个参数。我试过了,但对我不起作用。我确定我做错了什么:
testLocation=File.join("C:/test 1", "test.sql")
executeSql(testLocation)
def executeSql(sqlFile)
system("powershell", "Invoke-Sqlcmd", "-InputFile", "#{sqlFile}", "-ServerInstance", "localhost", "-Database", "dbName", "-Verbose")
end
诀窍是用反引号转义空格以使 powershell 自行运行。
def executeSql(sqlFile)
sqlFile.gsub! ' ','` '#escape whitespace in path
command="powershell Invoke-Sqlcmd -InputFile #{sqlFile} -ServerInstance \"localhost\" -Database \"dbName\" -Verbose"
puts "command: #{command}"
if($firstRun)
system("powershell Import-Module sqlps -DisableNameChecking;exit")
$firstRun=false
end
system("#{command}")
end
def executeSql(sqlFile)
#exec("powershell $env:gitroot");
puts "executing file: #{sqlFile}"
command="powershell Invoke-Sqlcmd -InputFile #{sqlFile} -ServerInstance \"localhost\" -Database \"dbName\" -Verbose"
command.gsub! '/','\'
puts "command: #{command}"
if($firstRun)
puts 'inside first run'
importCommand = `powershell Import-Module \"sqlps\" -DisableNameChecking`
printf importCommand
$firstRun=false
end
puts 'made it past first run'
printf `#{command}`
puts 'command execution complete'
end
错误
Invoke-Sqlcmd : A positional parameter cannot be found that accepts argument 'Static'.
当我 运行 在 ruby 之外的 powershell 中使用相同的命令时,它会正常执行。我做错了什么?
更新
我发现用硬编码路径 C:\test\test.sql
替换 #{sqlFile}
可以解决问题。我试过 sqlFile.gsub! '/', '\\'
并没有明显区别(仍然是标准 \
windows 路径)。
我也试过了
command="powershell Invoke-Sqlcmd -InputFile #{Shellwords.escape(sqlFile)} -ServerInstance \"localhost\" -Database \"dbName\" -Verbose"
创建
Invoke-Sqlcmd -InputFile C:\Git\project\sql\db\01\ -\ Static\ Data\file.name.here.sql -ServerInstance "localhost" -Database "dbName" -Verbose
如果不是文件路径中的 01\ -\ Static\
部分,我认为这会起作用。
更新 2
我找到了有用的东西并将其发布为答案。 @tadman 建议对 system
使用多个参数。我试过了,但对我不起作用。我确定我做错了什么:
testLocation=File.join("C:/test 1", "test.sql")
executeSql(testLocation)
def executeSql(sqlFile)
system("powershell", "Invoke-Sqlcmd", "-InputFile", "#{sqlFile}", "-ServerInstance", "localhost", "-Database", "dbName", "-Verbose")
end
诀窍是用反引号转义空格以使 powershell 自行运行。
def executeSql(sqlFile)
sqlFile.gsub! ' ','` '#escape whitespace in path
command="powershell Invoke-Sqlcmd -InputFile #{sqlFile} -ServerInstance \"localhost\" -Database \"dbName\" -Verbose"
puts "command: #{command}"
if($firstRun)
system("powershell Import-Module sqlps -DisableNameChecking;exit")
$firstRun=false
end
system("#{command}")
end