当存在另一个参数时,在 PowerShell 中需要参数 [英] Requiring parameters in PowerShell when another parameter is present
问题描述
到目前为止我所读到的一切都告诉我这应该有效.这旨在成为自动转出 AD 帐户的脚本.我希望它如何工作,如果设置了 -TransferHomeDrive 开关,那么它将需要旧服务器和新服务器的名称.
Everything I have read so far tells me that this should work. This is intended to be a script for automating the transfer out of AD accounts. How I would like it to work is if the -TransferHomeDrive switch is set, then it will require names for the old server and new server.
我相信我应该能够使用参数集来做到这一点,但是如果我只有一个参数集,加上默认参数,它总是会提示我输入新旧服务器.这是我的 param
声明:
I believe I should be able to do this with a parameter set, however if I just have the single parameter set, plus the default parameters, it will always prompt me for an old and new server. Here is my param
statement:
Param(
[Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[string]$Username,
[switch]$RetainGroups,
[Parameter(ParameterSetName='TransferHomeDrive', Mandatory=$False)]
[switch]$TransferHomeDrive,
[Parameter(ParameterSetName='TransferHomeDrive', Mandatory=$True)]
[string]$OldServer,
[Parameter(ParameterSetName='TransferHomeDrive', Mandatory=$True)]
[string]$NewServer
)
所以,如果它工作正常,你可以运行
So, if it were working, you could run
Move-AccountOut -Username JohnSmith -RetainGroups
它不会安排家庭驱动器转移.但是,如果您要运行
And it won't schedule a home drive transfer. However, if you were to run
Move-AccountOut -Username JohnSmith -RetainGroups -TransferHomeDrive
它会提示 OldServer
和 NewServer
.
我发现了类似的问题,包括这些问题,但仍然无法解决:
I've found similar questions, including these, but it's still not working:
我相信如果我创建第二个参数集会起作用,其中包括所有参数,包括 Username
和 RetainGroups
,但似乎根据其他帖子我应该能够让它按照我编写的方式工作,其中 Username
和 RetainGroups
是全局"参数,而 OldServer
和 NewServer
仅在设置 TransferHomeDrive
时才需要
I believe that it would work if I created a second parameter set, that included ALL parameters, including Username
and RetainGroups
, but it seems like according to the other posts that I should be able to have it work the way that I have it written, where Username
and RetainGroups
are "global" parameters, and OldServer
and NewServer
are only required when TransferHomeDrive
is set
推荐答案
在你的代码中你只有一个参数集TransferHomeDrive
,你没有其他default参数集.您应该让 PowerShell 知道存在另一个命名参数集,方法是在 CmdletBinding
或 Parameter
属性中显式声明它.
In your code you have only one parameter set TransferHomeDrive
, you does not have other default parameter set. You should let PowerShell know, that another named parameter set exists, by explicitly declare it in CmdletBinding
or Parameter
attribute.
[CmdletBinding(DefaultParameterSetName='NoTransferHomeDrive')]
Param(
[Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[string]$Username,
[switch]$RetainGroups,
[Parameter(ParameterSetName='TransferHomeDrive', Mandatory=$False)]
[switch]$TransferHomeDrive,
[Parameter(ParameterSetName='TransferHomeDrive', Mandatory=$True)]
[string]$OldServer,
[Parameter(ParameterSetName='TransferHomeDrive', Mandatory=$True)]
[string]$NewServer
)
这篇关于当存在另一个参数时,在 PowerShell 中需要参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!