无法验证参数 'Username' 的参数。参数为 null 或空。提供一个不为 null 或空的参数

Cannot validate argument on parameter 'Username'. The argument is null or empty. Provide an argument that is not null or empty

我正在尝试使用 Powershell 7-Preview 执行 Powershell 脚本 (7.0) 文件,它遍历所有数据库并在所有数据库中执行 SQL 服务器脚本。

脚本正确获取所有数据库,但是,当执行并行块时,出现此错误(请查看下面的屏幕截图了解详细信息)。

Cannot validate argument on parameter 'Username'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

命令-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
    }
}

错误:

这个问题 完全, but the answer is pretty much the same as .

的重复

问题是 $DatabaseUsername$DatabasePassword 等,foreach-object 脚本块中的变量在不同的范围内,实际上是与主脚本中的变量不同的变量脚本。

您可以使用 Using scope modifier

解决此问题

For any script or command that executes out of session, you need the Using scope modifier to embed variable values from the calling session scope, so that out of session code can access them.

所以代替:

$DatabaseUsername = "myusername"

foreach-object -parallel {
    ...
    "Username" = $DatabaseUsername
    ...
}

尝试

$DatabaseUsername = "myusername"

foreach-object -parallel {
    ...
    "Username" = $using:DatabaseUsername
    ...
}