gpt4 book ai didi

powershell - 无法使用指定的命名参数解析参数集

转载 作者:行者123 更新时间:2023-12-03 00:41:46 38 4
gpt4 key购买 nike

在下面的脚本中,应该需要 -live 或 -test 参数。但是,该脚本无需任何开关即可运行。如果我使用这些参数之一,我会收到以下错误。为什么它不需要 -live 或 -test 以及为什么如果我使用其中之一就会失败?

C:\Users\Administrator\Documents\Disable-ADAccounts.ps1 : Parameter set cannot be resolved using the specified named parameters. At line:1 char:1 + . .\Disable-ADAccounts.ps1 -days 7 -all -live + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Disable-ADAccounts.ps1], ParameterBindingException + FullyQualifiedErrorId : AmbiguousParameterSet,Disable-ADAccounts.ps1

    [CmdletBinding()]
param (
[Parameter(Mandatory = $true, ParameterSetName = 'UsersOnly')]
[Switch]
$usersOnly,

[Parameter(Mandatory = $true, ParameterSetName = 'ComputersOnly')]
[Switch]
$computersOnly,

[Parameter(Mandatory = $true, ParameterSetName = 'All')]
[Switch]
$all,

[Parameter(Mandatory=$true)]
[string]
$days,

[switch]
$console,

[Parameter(Mandatory = $true, ParameterSetName = 'Test')]
[switch]
$test,

[Parameter(Mandatory = $true, ParameterSetName = 'Live')]
[switch]
$live
)

Process {
$DC = Get-ADDomainController
$OUs = Get-ADOrganizationalUnit -Filter * # Uncomment this line to search ALL OUs. Comment Next Variable.
#$OUs = Get-ADOrganizationalUnit -Filter 'Name -eq "test"' # Use this line to test on a single OU
$TimeStamp = get-date -format D
$description = "Disabled on " + $TimeStamp
$noDisableComputer = Get-ADGroupMember -Identity DoNotDisableComputers -Recursive | Select -ExpandProperty Name
$noDisableUser = Get-ADGroupMember -Identity DoNotDisableUsers -Recursive | Select -ExpandProperty Name
$noDisable = $noDisableComputer+$noDisableUser
$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToString('yyMMdd_hhmmss')
$tempDir = [System.Environment]::GetEnvironmentVariable('TEMP','Machine')
$logFile = $tempDir + "\DisabledAccounts_$CurrentDate.csv"

# If -test switch is used enable -WhatIf parameter.
If($test) { $whatIf = @{ WhatIf = $true } }
ElseIf($live) { $whatIf = @{ WhatIf = $false } }

# Set parameter to UsersOnly, ComputersOnly, or do not use a perameter
If($usersOnly) { $scope = @{ UsersOnly = $true } }
ElseIf($computersOnly) { $scope = @{ ComputersOnly =$true } }
ElseIf($all) { $scope = @{} }

# Disable User and/or Computer Objects inactive for XX days.

# Iterate through Organizational Units
foreach ($OU in $OUs) {

# Search for User and/or Computer Objects inactive for XX days. Disable object if not in DoNotDisable Security Groups
$days = $days + "D"
$accounts = Search-ADAccount -SearchBase $OU.DistinguishedName -SearchScope OneLevel -AccountInactive -TimeSpan ([timespan]7D) @scope
foreach($account in $accounts){
If ($noDisable -notcontains $account.Name) {
Write-Host $account
# #Disable-ADAccount -Identity $account.DistinguishedName -Verbose $whatIf | Export-Csv $logFile
}
}
}
}

# Move Disabled Users to Disabled Users OU & Add Timestamp to Description
#Search-ADAccount –AccountDisabled –ComputersOnly –SearchBase $OU.DistinguishedName | Foreach-object {
#Set-ADComputer $_ -Description $description -Verbose -WhatIf
#Move-ADObject $_ –TargetPath “OU=Disabled Computers, DC=COH,DC=net” -Verbose -WhatIf
#}
#}

最佳答案

我认为您对参数集感到困惑。您所做的是创建五个不同的参数集,并且这五个参数集中只有一个可以随时处于事件状态。如果我正确地理解了您的意图,您需要的是执行以下任一操作的能力:

 \Disable-ADAccounts.ps1 -days 7 -all -live
\Disable-ADAccounts.ps1 -days 7 -UsersOnly -live
\Disable-ADAccounts.ps1 -days 7 -ComputersOnly -live
\Disable-ADAccounts.ps1 -days 7 -all -test
\Disable-ADAccounts.ps1 -days 7 -UsersOnly -test
\Disable-ADAccounts.ps1 -days 7 -ComputersOnly -test

如果是这种情况,您需要六个不同的参数组,而不是五个。您的每个参数都应该是所需组的成员,因此如下所示:

[Parameter(Mandatory = $true, ParameterSetName = 'UsersOnlyLive')]
[Parameter(Mandatory = $true, ParameterSetName = 'UsersOnlyTest')]
[Switch]
$usersOnly,

[Parameter(Mandatory = $true, ParameterSetName = 'ComputersOnlyLive')]
[Parameter(Mandatory = $true, ParameterSetName = 'ComputersOnlyTest')]
[Switch]
$computersOnly,

[Parameter(Mandatory = $true, ParameterSetName = 'AllLive')]
[Parameter(Mandatory = $true, ParameterSetName = 'AllTest')]
[Switch]
$all,

[Parameter(Mandatory=$true)]
[string]
$days,

[switch]
$console,

[Parameter(Mandatory = $true, ParameterSetName = 'AllTest')]
[Parameter(Mandatory = $true, ParameterSetName = 'UsersOnlyTest')]
[Parameter(Mandatory = $true, ParameterSetName = 'ComputersOnlyTest')]
[switch]
$test,

[Parameter(Mandatory = $true, ParameterSetName = 'AllLive')]
[Parameter(Mandatory = $true, ParameterSetName = 'UsersOnlyLive')]
[Parameter(Mandatory = $true, ParameterSetName = 'ComputersOnlyLive')]
[switch]
$live

参见this Microsoft article有关参数集的更多信息。

我希望这会有所帮助。

关于powershell - 无法使用指定的命名参数解析参数集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60305844/

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