gpt4 book ai didi

powershell - 在PowerShell中使用动态参数值

转载 作者:行者123 更新时间:2023-12-02 23:10:43 24 4
gpt4 key购买 nike

我正在尝试在PowerShell中使用动态参数,但是在运行脚本后,该参数的值似乎不存在。

[CmdletBinding()]
Param (
[Parameter(
Mandatory=$true,
Position=0,
HelpMessage = "The entity ID for the Version"
)]
[string]$TPVersionID,

[Parameter(
Mandatory=$true,
Position=1,
HelpMessage = ""
)]
[string]$VersionNumber,

[Parameter(
Mandatory=$true,
Position=2,
HelpMessage = "This is a boolean value; enter any value to make it True, leave it blank to make it False."
)]
[bool]$PullVersionDoc
)


function Get-VersionParam{
[CmdletBinding()]
Param ([string]$TPVersionID, [string]$VersionNumber, [bool]$PullVersionDoc?)
DynamicParam {
if ($PullVersionDoc) {
write-host("HEY!")

$attributes = new-object System.Management.Automation.ParameterAttribute
$attributes.Position = 3
$attributes.Mandatory = $true
$attributeCollection = new-object `
-Type System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($attributes)

$dynParam1 = new-object `
-Type System.Management.Automation.RuntimeDefinedParameter('VersionDocumentID', [Int32], $attributeCollection)

$paramDictionary = new-object `
-Type System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add('VersionDocumentID', $dynParam1)
return $paramDictionary
}
}
}


Get-VersionParam


#Write-Host "Dynamic Parameter PullVersionDoc? = " $PullVersionDoc
Write-Host $PSBoundParameters

我希望脚本询问[VersionDocumentID],如果[PullVersionDoc]的 bool(boolean) 值是TRUE,以便稍后在脚本中使用,但是当我写出[$ PSBoundParameters]时,该参数不存在。我如何获得值(value)以便可以使用它?

最佳答案

这是我尝试使用动态参数来获取Configuration Manager日志的方法。

功劳归功于blog post here

用法:Get-CCMLogs -ComputerNames -Remote -RemoteLogName <Tab to complete lognames>
本地用法:Get-CCMLogs -ComputerNames -LocalLogName <Tab to complete lognames>
如果输入了开关-Remote,则动态参数将返回一个远程日志名称;如果未输入开关-Remote,则动态参数将返回一个本地日志名称。

Function Get-CCMLogs {

[CmdletBinding()]

Param(
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage="Give me a list of computer names!")]
[Alias('Hostname','cn')]
[string[]]$ComputerNames = $env:COMPUTERNAME,

[switch]$Remote
)

DynamicParam{

If ($Remote) {

$ParameterName = 'RemoteLogName'

$RunTimeDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]

$ParamAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParamAttribute.Mandatory = $true
$ParamAttribute.Position = 1

$AttributeCollection.Add($ParamAttribute)

$ValidateItems = Get-ChildItem -Path "\\$ComputerNames\C$\Windows\CCM\Logs" | Where {$_ -notmatch '\d+'} | Select -ExpandProperty FullName
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($ValidateItems)

$AttributeCollection.Add($ValidateSetAttribute)

$RunTimeParam = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)

$RunTimeDictionary.Add($ParameterName, $RunTimeParam)

Return $RunTimeDictionary
}
else {
$ParameterName = 'LocalLogName'

$RunTimeDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]

$ParamAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParamAttribute.Mandatory = $true
$ParamAttribute.Position = 1

$AttributeCollection.Add($ParamAttribute)

$ValidateItems = Get-ChildItem -Path C:\Windows\CCM\Logs | Select -ExpandProperty FullName | Where {$_ -notmatch '\d+'}
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($ValidateItems)

$AttributeCollection.Add($ValidateSetAttribute)

$RunTimeParam = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)

$RunTimeDictionary.Add($ParameterName, $RunTimeParam)

Return $RunTimeDictionary
}
}

Begin{
$LogName = $PSBoundParameters[$ParameterName]
}

Process{
cmtrace.exe $LogName
}
}

关于powershell - 在PowerShell中使用动态参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42318419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com