Reputation:
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.
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
It would prompt for OldServer
and NewServer
.
I've found similar questions, including these, but it's still not working:
PowerShell mandatory parameter depend on other parameter
Having a optional parameter that requires another parameter to be present
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
Upvotes: 3
Views: 4007
Reputation: 22132
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
)
Upvotes: 2