Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 无法转换“"/行动:“;类型“的值”;System.String“;输入“;“系统.管理.自动化.脚本块”;_Powershell_Powershell 7.0 - Fatal编程技术网

Powershell 无法转换“"/行动:“;类型“的值”;System.String“;输入“;“系统.管理.自动化.脚本块”;

Powershell 无法转换“"/行动:“;类型“的值”;System.String“;输入“;“系统.管理.自动化.脚本块”;,powershell,powershell-7.0,Powershell,Powershell 7.0,我在尝试执行低于powershell(7.0)脚本时遇到此错误。 此脚本使用dacpac文件和SqlPackage.exe更新所有数据库 [string]$SqlPackagePath = "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\Microsoft\SQLDB\DAC\150\SqlPackage.exe" [int]$ErrorCount =

我在尝试执行低于powershell(7.0)脚本时遇到此错误。 此脚本使用dacpac文件和SqlPackage.exe更新所有数据库

[string]$SqlPackagePath = "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\Microsoft\SQLDB\DAC\150\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 Studio\2019\Enterprise\Common7\IDE\Extensions\Microsoft\SQLDB\DAC\150\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        
}

现在还不清楚您试图在这里做什么,
$SqlPackagePath
是您试图执行的可执行文件的名称吗?而
SqlPackage.exe
的完整路径是参数之一?是的,使用“dir”命令,此脚本将在指定路径内查找SqlPackage.exe。如果未找到,则SqlPackage.exe的完整路径将通过$SqlPackagePath变量传递。然后使用该exe在for循环中使用dacpac文件更新所有数据库。我尝试了此解决方案,但现在出现以下错误->out lineoutput:类型为“Microsoft.PowerShell.Commands.Internal.Format.FormatStartData”的对象无效或顺序不正确。这可能是由用户指定的“格式列表”命令引起的,该命令与默认格式冲突。您是否对使用
$using:
引用的任何变量使用了
格式-*
命令(或
ft
fl
)?否,此语句导致返回包含3行的表的问题。我只需要第一条记录,即exe的完整路径$sqlPackageExe=dir-Path“C:\Program Files(x86)\Microsoft Visual Studio*”-Filter“SqlPackage.exe”-Recurse-ErrorAction SilentlyContinue。如何获取第一个条目或路径?通过管道输入
|选择-First 1
,更新答案