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中嵌套动态参数_Powershell - Fatal编程技术网

在PowerShell中嵌套动态参数

在PowerShell中嵌套动态参数,powershell,Powershell,我正在开发一个函数,该函数将在SQL数据库中插入一行。它基本上是一个简单的更改日志,可以帮助我跟踪在各种SQL实例上更改的内容。作为其中的一部分,我希望有以下参数: 时间戳 服务器 实例 改变 我已经弄清楚了时间戳、更改和服务器,但是这个实例给了我一些麻烦。Server参数是动态的,因为它从我的清单中提取SQL Server列表。然后,我希望在另一个动态参数中使用该参数的值,该参数提取该服务器上的实例列表(也来自我的资源清册)。以下是关于动态部分的内容: DynamicParam {

我正在开发一个函数,该函数将在SQL数据库中插入一行。它基本上是一个简单的更改日志,可以帮助我跟踪在各种SQL实例上更改的内容。作为其中的一部分,我希望有以下参数:

  • 时间戳
  • 服务器
  • 实例
  • 改变
我已经弄清楚了时间戳、更改和服务器,但是这个实例给了我一些麻烦。Server参数是动态的,因为它从我的清单中提取SQL Server列表。然后,我希望在另一个动态参数中使用该参数的值,该参数提取该服务器上的实例列表(也来自我的资源清册)。以下是关于动态部分的内容:

DynamicParam {
    if (!(Get-Module sqlps)){ Pop-Location; Import-Module sqlps -DisableNameChecking; Push-Location }
    $inventoryinstance = 'ServerName'
    $newparams         = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

    $server_query      = 'SELECT [Name] FROM [ServerInventory].[dbo].[Servers] WHERE [TypeID] = 1 ORDER BY [Name]'
    $servers           = Invoke-Sqlcmd -serverinstance $inventoryinstance -query $server_query -connectiontimeout 5
    # Populate array
    $serverlist = @()
    foreach ($servername in $servers.Name) { 
        $serverlist += $servername
    }

    $attributes = New-Object System.Management.Automation.ParameterAttribute
    $attributes.ParameterSetName = "__AllParameterSets"
    $attributes.Position         = 1
    $attributes.Mandatory        = $true
    $attributes.HelpMessage      = "The server the change was made on"

    # Server list parameter setup
    if ($serverlist){ $servervalidationset = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $serverlist }
    $serverattributes = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
    $serverattributes.Add($attributes)
    if ($serverlist){ $serverattributes.Add($servervalidationset) }
    $serverob = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("Server", [String], $serverattributes)
    $newparams.Add("Server", $serverob)

    $instance_query  = "SELECT [Name] FROM [ServerInventory].[dbo].[SQLInstances] WHERE [ServerID] = (SELECT [ServerID] FROM [ServerInventory].[dbo].[Servers] WHERE [Name] = '$($PSBoundParameters.Server)')"
    $instances       = Invoke-Sqlcmd -serverinstance $inventoryinstance -query $instance_query -connectiontimeout 5
    # Populate array
    $instancelist = @()
    foreach ($instancename in $instances.Name) { 
        $instancelist += $instancename
    }

    $attributes = New-Object System.Management.Automation.ParameterAttribute
    $attributes.ParameterSetName = "__AllParameterSets"
    $attributes.Position         = 2
    $attributes.Mandatory        = $false
    $attributes.HelpMessage      = "The instance the change was made on, do not specify for server-level changes"

    # Server list parameter setup
    if ($instancelist){ $instancevalidationset = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $instancelist }
    $instanceattributes = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
    $instanceattributes.Add($attributes)
    if ($instancelist){ $instanceattributes.Add($instancevalidationset) }
    $instanceob = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("Instance", [String], $instanceattributes)
    $newparams.Add("Instance", $instanceob)            

    return $newparams
}
除了实例变量的值没有自动完成之外,所有的事情似乎都在运行。是否可以使用一个动态参数的值生成另一个