Powershell 无法验证参数';用户名';。参数为null或为空。提供一个不为null或空的参数

Powershell 无法验证参数';用户名';。参数为null或为空。提供一个不为null或空的参数,powershell,powershell-7.0,Powershell,Powershell 7.0,我正在尝试使用Powershell 7-Preview执行Powershell脚本(7.0)文件,该文件将遍历所有数据库并在所有数据库中执行SQL Server脚本 脚本正确地获取了所有数据库,但是,当并行块执行时,我得到了这个错误(请查找下面的屏幕截图了解详细信息) 无法验证参数“Username”上的参数。参数为null或为空。请提供一个不为null或空的参数,然后重试该命令 命令-C:\GitHub\TempApp\CompanyTemplate\bin\debug\DeploymentS

我正在尝试使用Powershell 7-Preview执行Powershell脚本(7.0)文件,该文件将遍历所有数据库并在所有数据库中执行SQL Server脚本

脚本正确地获取了所有数据库,但是,当并行块执行时,我得到了这个错误(请查找下面的屏幕截图了解详细信息)

无法验证参数“Username”上的参数。参数为null或为空。请提供一个不为null或空的参数,然后重试该命令

命令-C:\GitHub\TempApp\CompanyTemplate\bin\debug\DeploymentScripts\PowerShell\DeployCompanyDatabases.ps1

Param
(
    [string]$SystemWorkingDir,
    [string]$DatabaseUsername,
    [string]$DatabasePassword,
    [string]$DacpacLocation,
    [string]$OngoingChangesScriptLocation,
    [string]$PreDeploymentBugFixScriptLocation,
    [string]$DropExternalTablesScriptLocation
)

    $SystemWorkingDir = "C:\GitHub\TempAPP\CompanyTemplate\bin\Debug" 
    $DatabaseUsername = "tempDB"
    $DatabasePassword = "tempPassword" 
    $DacpacLocation = "CompanyTemplate.dacpac" 
    $OngoingChangesScriptLocation = "DeploymentScripts\OnGoingDataChanges.sql" 
    $PreDeploymentBugFixScriptLocation = "DeploymentScripts\PreDeployment.sql"
    $DropExternalTablesScriptLocation = "DeploymentScripts\DropExternalTables.sql"
    [string]$ServerInstance = "tempDB"

$GetDatabasesParams = @{
    "Database" = "master"
    "ServerInstance" =  "tempDB"
    "Username" = "$DatabaseUsername"
    "Password" = "$DatabasePassword"
    "Query" = "select [name] from sys.databases where [name] like 'Company%'"
    "ConnectionTimeout" = 9999
}

echo "Retrieving company database names"
[string[]]$DatabaseNames = Invoke-Sqlcmd @GetDatabasesParams | select -expand name

[int]$ErrorCount = 0
$DatabaseNames | ForEach-Object -Parallel {
    try 
    {
        $OngoingChangesScriptParams = @{
            "Database" = "$_"
            "ServerInstance" = "$ServerInstance"
            "Username" = "$DatabaseUsername"
            "Password" = "$DatabasePassword"
            "InputFile" ="$SystemWorkingDir\$OngoingChangesScriptLocation" 
            "OutputSqlErrors" = 1
            "QueryTimeout" = 9999
            "ConnectionTimeout" = 9999
        }

        Invoke-Sqlcmd @OngoingChangesScriptParams       
    }
    catch
    {
        $ErrorCount++
        echo "Internal Error. The remaining databases will still be processed."
        echo $_.Exception|Format-List -force
    }
}
错误:

这个问题并不是完全重复的,但答案与相同

问题在于
$DatabaseUsername
$DatabasePassword
foreach对象
脚本块中的变量位于不同的范围内,实际上与主脚本中的变量不同

您可以使用

对于在会话外执行的任何脚本或命令,都需要使用作用域修饰符来嵌入调用会话作用域中的变量值,以便会话外代码可以访问它们

因此,不是:

$DatabaseUsername=“myusername”
foreach对象-并行{
...
“Username”=$DatabaseUsername
...
}
试一试

$DatabaseUsername=“myusername”
foreach对象-并行{
...
“Username”=$using:DatabaseUsername
...
}

请,.尝试使用:DatabaseUsername“Username”=$using,看看是否有效。这可能是一个范围问题…@mclayton,它正在发挥作用。谢谢您能告诉我如何连接两个变量,即$using:SystemWorkingDir+using:$ongoingchangescriptlocation吗?@mclayton请将您的回复作为答案发布,以便我将其标记为已接受answer@vonPryz我只是想知道发布错误图片是否违反政策?