简体   繁体   English

参数的变量类型问题? - 电源外壳

[英]Variable Type Issue For Parameter? - Powershell

I am trying to run the below code to search for inactive user accounts in an OU.我正在尝试运行以下代码来搜索 OU 中的非活动用户帐户。 It seems like the type of variable I'm using may not be able to used with parameters.似乎我使用的变量类型可能无法与参数一起使用。 Does that seem correct and if so what type of variable should I be using?这看起来正确吗?如果正确,我应该使用什么类型的变量?

$scope = "-UsersOnly" 

$accounts = Search-ADAccount -SearchBase "OU=Users,OU=testLab02,DC=test,DC=local" -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
        }
    }

I receive the below error:我收到以下错误:

在此处输入图片说明

Search-ADAccount : A positional parameter cannot be found that accepts argument '-UsersOnly'. Search-ADAccount :找不到接受参数“-UsersOnly”的位置参数。 At C:\\Users\\Administrator\\Documents\\Disable-ADAccounts.ps1:63 char:21 + ... $accounts = Search-ADAccount -SearchBase $OU.DistinguishedName -Accou ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Search-ADAccount], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SearchADAccount在 C:\\Users\\Administrator\\Documents\\Disable-ADAccounts.ps1:63 char:21 + ... $accounts = Search-ADAccount -SearchBase $OU.DistinguishedName -Accou ... + ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Search-ADAccount], ParameterBindingException +fullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SearchADAccount

However, if I run the command manually without variables it works as expected:但是,如果我在没有变量的情况下手动运行命令,它会按预期工作:

Search-ADAccount -SearchBase "OU=Users,OU=testLab02,DC=test,DC=local" -AccountInactive -TimeSpan ([timespan]7D) -UsersOnly

在此处输入图片说明

$scope = "-UsersOnly"

You can't pass a (switch) parameter stored in a variable that way - it'll invariably be treated as a (positional) argument , which explains the error you saw;您不能以这种方式传递存储在变量中的 (switch)参数- 它总是会被视为 (positional) argument ,这解释了您看到的错误; with directly passed arguments, only unquoted, literal tokens such as -UsersOnly are recognized as parameter names.对于直接传递的参数,只有不带引号的文字标记(例如-UsersOnly被识别为参数名称。

You can use splatting to pass parameters via variables, which in your case means:您可以使用splatting通过变量传递参数,在您的情况下,这意味着:

# Define a hash table of parameter values.
# This hash table encodes switch parameter -UsersOnly
$scope = @{ UsersOnly = $true } # [switch] parameters are represented as Booleans

# Note the use of sigil @ instead of $ to achieve splatting
Search-ADAccount @scope -SearchBase "OU=Users,OU=testLab02,DC=test,DC=local" -AccountInactive -TimeSpan ([timespan]7D) 
  • $scope is defined as a hash table ( @{ ... } ) whose entries represent parameter name-value pairs . $scope被定义为一个hash table@{ ... } ),其条目表示参数名称-值对

    • Here, only one parameter name-value pair is defined:这里只定义了一个参数名-值对:
      • Parameter name -UsersOnly (the entry key must be defined without the - sigil) ...参数名称-UsersOnly (必须在没有- -UsersOnly情况下定义输入键)...
      • ... with value $true , which for a [switch] (flag) parameter is equivalent to passing the parameter; ... 值为$true ,对于[switch] (flag) 参数相当于传递参数; $false is typically [1] equivalent to omitting it. $false通常[1]相当于省略它。
  • To pass the parameter values represented by hashtable $scope to a command via splatting , sigil @ instead of $ must be used, ie, @scope in this case.通过由散列表所表示的参数值$scope经由到命令splatting ,印记@代替$必须使用的,即, @scope在这种情况下。


[1] A command can technically distinguish between a switch being omitted and it being passed with value $false , and on occasion that results in different behavior, notably with the common -Confirm parameter, where -Confirm:$false overrides the $ConfirmPreference preference variable. [1] 命令可以从技术上区分被省略的开关和它以$false值传递,有时会导致不同的行为,特别是使用通用的-Confirm参数,其中-Confirm:$false覆盖$ConfirmPreference首选项多变的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM