运行 sql 文件列表的 Powershell 脚本
Powershell script to run list of sql files
我想读取一个文件,其中包含以行分隔的 *.sql 文件名列表(全部位于同一目录)以执行以下操作:
$reader = [System.IO.File]::OpenText("_Scripts.txt")
try {
for(;;) {
$line = $reader.ReadLine()
if ($line -eq $null) { break }
#output
$out = $line.split(".")[0] + ".txt" ;
# -serverinstance u should change the value of it according to use
invoke-sqlcmd -inputfile $line -serverinstance "." | format-table | out-file -filePath $out
$line
}
}
finally {
$reader.Close()
}
我正在尝试使用包含以下命令的批处理文件来执行此脚本文件:
powershell.exe -ExecutionPolicy Bypass -Command "_scripts.ps1"
但我收到如下所示的错误:
谁能帮我修复我的 ps1 脚本?
这对我有用:
$lines = Get-Content C:\Temp\TEST\_Scripts.txt
ForEach ($line in $lines)
{
$out = $line.split(".")[0] + ".txt" ;
Invoke-Sqlcmd -InputFile $line -ServerInstance "localhost" -Database "master" | Format-Table | Out-File -FilePath $out
}
我想读取一个文件,其中包含以行分隔的 *.sql 文件名列表(全部位于同一目录)以执行以下操作:
$reader = [System.IO.File]::OpenText("_Scripts.txt")
try {
for(;;) {
$line = $reader.ReadLine()
if ($line -eq $null) { break }
#output
$out = $line.split(".")[0] + ".txt" ;
# -serverinstance u should change the value of it according to use
invoke-sqlcmd -inputfile $line -serverinstance "." | format-table | out-file -filePath $out
$line
}
}
finally {
$reader.Close()
}
我正在尝试使用包含以下命令的批处理文件来执行此脚本文件:
powershell.exe -ExecutionPolicy Bypass -Command "_scripts.ps1"
但我收到如下所示的错误:
谁能帮我修复我的 ps1 脚本?
这对我有用:
$lines = Get-Content C:\Temp\TEST\_Scripts.txt
ForEach ($line in $lines)
{
$out = $line.split(".")[0] + ".txt" ;
Invoke-Sqlcmd -InputFile $line -ServerInstance "localhost" -Database "master" | Format-Table | Out-File -FilePath $out
}