无法将 "System.String" 类型的“/Action:”值转换为 "System.Management.Automation.ScriptBlock" 类型
Cannot convert the "/Action:" value of type "System.String" to type "System.Management.Automation.ScriptBlock"
我在尝试执行低于 power-shell (7.0) 的脚本时遇到此错误。
此脚本使用 dacpac 文件和 SqlPackage.exe.
更新所有数据库
[string]$SqlPackagePath = "C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\Common7\IDE\Extensions\Microsoft\SQLDB\DAC0\SqlPackage.exe"
[int]$ErrorCount = 0
$DatabaseNames | ForEach-Object -Parallel {
echo "$_ - Updating database using DACPAC."
dir -Path "C:\Program Files (x86)\Microsoft Visual Studio*" -Filter "SqlPackage.exe" -Recurse -ErrorAction SilentlyContinue | %{$_.FullName} {$using:SqlPackagePath /Action:Publish /tu:$using:DatabaseUsername /tp:$using:DatabasePassword /tsn:$using:ServerInstance /tdn:"$_" /sf:using:$DacpacLocation /p:BlockOnPossibleDataLoss=False}
}
我是 powershell 脚本的新手。看起来问题似乎与传递参数的方式有关,所以我只是将 {} 放在参数周围,因为 $using:SqlPackagePath 参数出现另一个错误,但这似乎不起作用
无法将类型“System.String”的“somepath”值转换为类型
“System.Management.Automation.ScriptBlock”。
您不能通过将参数嵌套在脚本块文字中来“转义”参数。
听起来你想做:
[string]$SqlPackagePath = "C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\Common7\IDE\Extensions\Microsoft\SQLDB\DAC0\SqlPackage.exe"
[int]$ErrorCount = 0
$DatabaseNames | ForEach-Object -Parallel {
echo "$_ - Updating database using DACPAC."
# locate SqlPackage, otherwise default to `$SqlPackagePath` from calling scope
$sqlPackageExe = dir -Path "C:\Program Files (x86)\Microsoft Visual Studio*" -Filter "SqlPackage.exe" -Recurse -ErrorAction SilentlyContinue |Select -First 1
if(-not $sqlPackageExe){
$sqlPackageExe = $using:SqlPackagePath
}
# use the `&` invocation operator to execute SqlPackage
& $sqlPackageExe /Action:Publish /tu:$using:DatabaseUsername /tp:$using:DatabasePassword /tsn:$using:ServerInstance /tdn:"$_" /sf:$using:DacpacLocation /p:BlockOnPossibleDataLoss=False
}
我在尝试执行低于 power-shell (7.0) 的脚本时遇到此错误。 此脚本使用 dacpac 文件和 SqlPackage.exe.
更新所有数据库[string]$SqlPackagePath = "C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\Common7\IDE\Extensions\Microsoft\SQLDB\DAC0\SqlPackage.exe"
[int]$ErrorCount = 0
$DatabaseNames | ForEach-Object -Parallel {
echo "$_ - Updating database using DACPAC."
dir -Path "C:\Program Files (x86)\Microsoft Visual Studio*" -Filter "SqlPackage.exe" -Recurse -ErrorAction SilentlyContinue | %{$_.FullName} {$using:SqlPackagePath /Action:Publish /tu:$using:DatabaseUsername /tp:$using:DatabasePassword /tsn:$using:ServerInstance /tdn:"$_" /sf:using:$DacpacLocation /p:BlockOnPossibleDataLoss=False}
}
我是 powershell 脚本的新手。看起来问题似乎与传递参数的方式有关,所以我只是将 {} 放在参数周围,因为 $using:SqlPackagePath 参数出现另一个错误,但这似乎不起作用
无法将类型“System.String”的“somepath”值转换为类型 “System.Management.Automation.ScriptBlock”。
您不能通过将参数嵌套在脚本块文字中来“转义”参数。
听起来你想做:
[string]$SqlPackagePath = "C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\Common7\IDE\Extensions\Microsoft\SQLDB\DAC0\SqlPackage.exe"
[int]$ErrorCount = 0
$DatabaseNames | ForEach-Object -Parallel {
echo "$_ - Updating database using DACPAC."
# locate SqlPackage, otherwise default to `$SqlPackagePath` from calling scope
$sqlPackageExe = dir -Path "C:\Program Files (x86)\Microsoft Visual Studio*" -Filter "SqlPackage.exe" -Recurse -ErrorAction SilentlyContinue |Select -First 1
if(-not $sqlPackageExe){
$sqlPackageExe = $using:SqlPackagePath
}
# use the `&` invocation operator to execute SqlPackage
& $sqlPackageExe /Action:Publish /tu:$using:DatabaseUsername /tp:$using:DatabasePassword /tsn:$using:ServerInstance /tdn:"$_" /sf:$using:DacpacLocation /p:BlockOnPossibleDataLoss=False
}