I just finished writing this function that moves users into different OUs.
I managed to get the function to work the way I want at some point but now it is playing up. More specifically some of the parameters that I have defined are not showing. It looks as though the filepath, ou, description, are the only thing that is showing in the final function.
I have been using the function builder to build the parameters and up to now it has been working.
If anyone can provide any assistance as to why the parameters such as CSV,uname, textname has disappeared I would be most grateful.
Thanking you,
Wei-Yen Tan
EDIT: I can see that the parameters that are missing are the parameters that are switches. Is there anyway to get them back?
<# .SYNOPSIS This cmdlet allows you to move users to different Organizational Units. .DESCRIPTION This cmdlet moves Active Directory users to different Organizational Units. There is different ways that this can be done. It can be done using CSV, text files or just by entering text. .PARAMETER ou This is the destination Organizational Unit. .PARAMETER Description When this option is set the function will write the text in the description property of the Active Directory user. .PARAMETER Filepath This parameter defines the file path for CSV files and txt files. .PARAMETER disabled If this parameter is set then it will disable the users as well as move them into the OU. .PARAMETER csv This parameter tells Powershell to import the users from a CSV file. .PARAMETER name This parameter tells Powershell to import from a listed set of names. .PARAMETER textfile This parameter tells Powershell to import from a text file. .PARAMETER uname A description of the uname parameter. .PARAMETER username A description of the username parameter. .PARAMETER text This parameter tells Powershell to import the list of users by a text file. .EXAMPLE Move-ADUserOU -textfile -FilePath "c:\textfiles\users.txt" -ou "OU=Employees,DC=contoso,DC=internal" The -textfile parameter tells powershell that it is going to be processing a textfile. You must provide the destination OU in this case. As the description parameter has been omitted it will not be entering anything in the description field of that Active Directory user. .EXAMPLE Move-ADUserOU -textfile -FilePath c:\textfiles\users.txt -ou "OU=Employees,DC=contoso,DC=internal" -Description "Disabled 15/02/2016" This is the same command as above but because we specified the -Description parameter it will enter in the text Disabled 15/02/2016 into every user in the text file. .EXAMPLE Move-ADUserOU -csv -FilePath c:\textfiles\users.csv -disabled This will go through the CSV file users.csv and then parse the values and set the values appropriately. However please note that the CSV file must have the following fields as specified in the notes section. Because the disabled parameter has been specified the cmdlet will also go ahead and disable the users specified in the CSV file. .NOTES When using the CSV parameter it must be noted that there are specific fields that must be present in the CSV file. They are samaccountname,ou and Description. There can be additional fields but these are the mandatory ones for the file to work. #> function Move-ADUserOU { [CmdletBinding(DefaultParameterSetName = 'username', ConfirmImpact = 'High', SupportsShouldProcess = $true)] param ( [Parameter(ParameterSetName = 'textfile', Mandatory = $false)] [Parameter(ParameterSetName = 'username')] [string]$ou, [Parameter(ParameterSetName = 'textfile', Mandatory = $false)] [Parameter(ParameterSetName = 'username')] [string]$Description, [Parameter(ParameterSetName = 'csv', Mandatory = $true)] [Parameter(ParameterSetName = 'textfile', Mandatory = $true)] [string]$Filepath, [switch]$disabled, [Parameter(ParameterSetName = 'csv')] [switch]$csv, [Parameter(ParameterSetName = 'username', ValueFromPipeline = $true)] [switch]$name, [Parameter(ParameterSetName = 'textfile')] [switch]$textfile, [Parameter(ParameterSetName = 'username')] $uname ) Begin { } Process { if ($pscmdlet.ShouldProcess("Target", "Operation")) { switch ($PsCmdlet.ParameterSetName) { 'csv' { if ($csv) { $users = Import-Csv -Path $Filepath $i = 0 foreach ($user in $users) { $i++ get-ADUser $user.samaccountname | Set-ADUser -Description $user.Description get-aduser $user.samaccountname | Move-ADObject -TargetPath $user.ou if ($disabled) { Disable-ADAccount $user.samaccountname } Write-Progress -Activity "Updating records" -Status "Progress:" -PercentComplete (($i/$users.Count) * 100) } } } 'textfile' { if ($textfile) { $users = Get-Content -Path $Filepath $i = 0 foreach ($user in $users) { $i++ if ($Description) { get-ADUser $user | Set-ADUser -Description $Description } get-aduser $user | Move-ADObject -TargetPath $ou if ($disabled) { Disable-ADAccount $user } Write-Progress -Activity "Updating records" -Status "Progress:" -PercentComplete (($i/$users.Count) * 100) } } } 'username' { if ($name) { $users = $uname $i = 0 foreach ($user in $users) { $i++ if ($Description) { get-ADUser $user | Set-ADUser -Description $Description } get-aduser $user | Move-ADObject -TargetPath $ou if ($disabled) { Disable-ADAccount $user } Write-Progress -Activity "Updating records" -Status "Progress:" -PercentComplete (($i/$users.Count) * 100) } } } } } } } Function Get-NetworkInformation{ <# .Synopsis This cmdlet gets key network information from local and remote computers .DESCRIPTION This cmdlet uses WMI to query the computername specified. it will query the macaddress and all IP addresses of the network card. .PARAMETER computername This is a mandatory parameter as the cmdlet will query it. .EXAMPLE get-networkinfo.ps1 -Computername dc2-ad-p01 This gets the mac address and ip address of one computer. .EXAMPLE get-networkinfo.ps1 -Computername dc2-ad-p01,dc1-file-p02 This gets the macaddress and ip address of two computers. .EXAMPLE get-networkinfo.ps1 -Computername (Get-Content C:\Scripts\Powershell\Customscripts\computers.txt) | select computername, macaddress, @{name = 'ipaddress'; expression = {$_.ipaddress -join ';'}} | export-csv c:\temp\custom.csv This gets the information and then parses it to a csv file. #> [CmdletBinding()] param ( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [string[]]$Computername ) foreach ($comp in $Computername) { $networkadapterconfiguration = Get-WmiObject -class win32_networkadapterconfiguration -ComputerName $Comp -filter 'ipenabled ="true"' $networkadapter = Get-WmiObject -Class win32_networkadapter -ComputerName $Comp $props = @{ 'Computername' = $Comp; 'ipaddress' = $networkadapterconfiguration.ipaddress; 'macaddress' = $networkadapterconfiguration.macaddress } $obj = New-Object -TypeName psobject -Property $props Write-Output $obj } } function Remove-DisabledUserGroup { <# .SYNOPSIS This removes all disabled user in group. .DESCRIPTION This removes all users that are disabled in a group. it checks the users "enabled" poperty and if it is false it will remove it. .PARAMETER ADGroup The group name in Active Directory that the cmdlet is querying against. .EXAMPLE Remove-DisabledUserGroup.ps1 -Group 'CTX_Word' This removes all disabled users in the group CTX_Word. .NOTES Additional information about the file. #> [CmdletBinding()] param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ValueFromRemainingArguments = $true, Position = 1)] [string]$ADGroup ) Begin { Import-Module ActiveDirectory } Process { $Groupmember = get-adgroupmember -identity $ADGroup $Disabledusers = $Groupmember | Select -ExpandProperty samaccountname | Get-Aduser | where { $_.enabled -eq $false } Remove-ADGroupMember -Members $Disableduser -Confirm:$false } End { Remove-Module activedirectory } } <# .SYNOPSIS This cmdlet allows you to move users to different Organizational Units .DESCRIPTION This cmdlet moves Active Directory users to different Organizational Units. There is diffferent ways that this can be done. It can be done using CSV, text files or just by entering text. .PARAMETER ou This is the destination Organizational Unit .PARAMETER name This is the names or array that you want to feed into the cmdlet .PARAMETER Description When this option is set the function will write the text in the description property of the Active Directory user. .PARAMETER Filepath A description of the Filepath parameter. .EXAMPLE Move-ADUserOU -textfile -FilePath "c:\textfiles\users.txt" -ou "OU=Employees,DC=contoso,DC=internal" The -textfile parameter tells powershell that it is going to be processing a textfile. You must provide the destination OU in this case. As the description parameter has been omitted it will not be entering anything in the description field of that Active Directory user. .EXAMPLE Move-ADUserOU -textfile -FilePath c:\textfiles\users.txt -ou "OU=Employees,DC=contoso,DC=internal" -Description "Disabled 15/02/2016" This is the same command as above but because we specified the -Description parameter it will enter in the text Disabled 15/02/2016 into every user in the text file. .EXAMPLE Move-ADUserOU -csv -FilePath c:\textfiles\users.csv This will go through the CSV file users.csv and then parse the values and set the values appropriately. However please note that the CSV file must have the following fields as specified in the notes section. .OUTPUTS switch, switch, switch .NOTES When using the CSV parameter it must be noted that there are specific fields that must be in the CSV file. They are samaccountname,ou and Description. The description field does not need to be filled in. #> function Move-ADUserOU { [CmdletBinding(DefaultParameterSetName = 'username', SupportsShouldProcess = $true)] [OutputType([switch], ParameterSetName = 'username')] [OutputType([switch], ParameterSetName = 'csv')] [OutputType([switch], ParameterSetName = 'textfile')] param ( [Parameter(ParameterSetName = 'textfile', Mandatory = $true, Position = 2)] [string]$ou, [Parameter(ParameterSetName = 'username', Mandatory = $true, ValueFromPipeline = $true, Position = 1)] $name, [Parameter(ParameterSetName = 'textfile', Mandatory = $false)] $Description, [Parameter(ParameterSetName = 'csv', ValueFromPipeline = $true)] [Parameter(ParameterSetName = 'textfile')] [string]$Filepath ) if ($pscmdlet.ShouldProcess("Target", "Operation")) { switch ($PsCmdlet.ParameterSetName) { 'csv' { $users = Import-Csv -Path $Filepath $i = 0 foreach ($user in $users) { $i++ get-ADUser $user.samaccountname | Set-ADUser -Description $user.Description get-aduser $user.samaccountname | Move-ADObject -TargetPath $user.ou Write-Progress -Activity "Updating records" -Status "Progress:" -PercentComplete (($i/$users.Count) * 100) } } 'textfile' { $users = Get-Content -Path $Filepath $i = 0 foreach ($user in $users) { $i++ if ($Description) { get-ADUser $user | Set-ADUser -Description $Description } get-aduser $user.samaccountname | Move-ADObject -TargetPath $ou Write-Progress -Activity "Updating records" -Status "Progress:" -PercentComplete (($i/$users.Count) * 100) } } 'username'{ $users = $name $i = 0 foreach ($user in $users) { $i++ if ($Description) { get-ADUser $user | Set-ADUser -Description $Description } get-aduser $user | Move-ADObject -TargetPath $ou Write-Progress -Activity "Updating records" -Status "Progress:" -PercentComplete (($i/$users.Count) * 100) } } } } }