在Powershell中循环遍历SQL文件

时间:2018-06-22 15:56:58

标签: sql-server powershell tfs

我试图遍历文件夹中的每个sql文件并在数据库上运行脚本。这是我目前拥有的。

#Provide SQLServerName
$SQLServer ="xxx\xxx"
#Provide Database Name
$DatabaseName ="xxx"
#Database User
$User ="xx"
#Database Password
$Password ="xxx"
#Scripts Folder Path
$FolderPath ="\\xxxs\xxx\xxx\xxx\"

#Loop through the .sql files and run them
foreach ($filename in get-childitem -path $FolderPath -filter "*.sql")
{
    invoke-sqlcmd –ServerInstance $SQLServer -Database $DatabaseName -Username $User -Password $Password -InputFile $filename.fullname
    #Print file name which is executed
    $filename
}

运行时,出现以下错误

At C:\Agent\_work\8\s\Deploy\dbRun.ps1:15 char:21
+ invoke-sqlcmd â?"ServerInstance $SQLServer -Database $DatabaseName
-Username ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
The string is missing the terminator: ".
At C:\Agent\_work\8\s\Deploy\dbRun.ps1:14 char:1
+ {
+ ~
Missing closing '}' in statement block.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

我无法弄清楚导致此错误的原因。我看到了结束}

0 个答案:

没有答案