Why do some of my parameters do not appear?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
weiyen.tan@gmail.com
Posts: 55
Joined: Wed Apr 09, 2014 2:06 am

Why do some of my parameters do not appear?

Post by weiyen.tan@gmail.com »

Hi there,

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)
					
				}
			}
		}
		
	}
}

jvierra
Posts: 14547
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Why do some of my parameters do not appear?

Post by jvierra »

When uploading large script please add the script as an attachment or place it in the code control available on the edit menu. Code pasted raw into the forum cannot be copied and is very hard to read.

Code larger than a small page should be uploaded as a file attachment.

Pasted code should look like this:
  1. function Move-ADUserOU
  2.  
  3. {
  4.  
  5.     [CmdletBinding(DefaultParameterSetName = 'username',
  6.  
  7.                    ConfirmImpact = 'High',
  8.  
  9.                    SupportsShouldProcess = $true)]
  10.  
  11.     param
  12.  
  13.     (
  14.  
  15.         [Parameter(ParameterSetName = 'textfile',
  16.  
  17.                    Mandatory = $false)]
  18.  
  19.         [Parameter(ParameterSetName = 'username')]
  20.  
  21.         [string]$ou,
  22.  
  23.         [Parameter(ParameterSetName = 'textfile',
  24.  
  25.                    Mandatory = $false)]
  26.  
  27.         [Parameter(ParameterSetName = 'username')]
  28.  
  29.         [string]$Description,
  30.  
  31.         [Parameter(ParameterSetName = 'csv',
  32.  
  33.                    Mandatory = $true)]
  34.  
  35.         [Parameter(ParameterSetName = 'textfile',
  36.  
  37.                    Mandatory = $true)]
  38.  
  39.         [string]$Filepath,
  40.  
  41.         [switch]$disabled,
  42.  
  43.         [Parameter(ParameterSetName = 'csv')]
  44.  
  45.         [switch]$csv,
  46.  
  47.         [Parameter(ParameterSetName = 'username',
  48.  
  49.                    ValueFromPipeline = $true)]
  50.  
  51.         [switch]$name,
  52.  
  53.         [Parameter(ParameterSetName = 'textfile')]
  54.  
  55.         [switch]$textfile,
  56.  
  57.         [Parameter(ParameterSetName = 'username')]
  58.  
  59.         $uname
  60.  
  61.     )
  62.  
  63.    
  64.  
  65.     Begin { }
  66.  
  67.     Process
  68.  
  69.     {

jvierra
Posts: 14547
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Why do some of my parameters do not appear?

Post by jvierra »

What is it that you mean by "missing". The parameters are all there in the Param statement.

User avatar
weiyen.tan@gmail.com
Posts: 55
Joined: Wed Apr 09, 2014 2:06 am

Re: Why do some of my parameters do not appear?

Post by weiyen.tan@gmail.com »

Sorry I should say when I execute the cmdlet. If you look up help they don't display. If I look it up intellisense in the ise they don't show. The parameter with switches that is.

jvierra
Posts: 14547
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Why do some of my parameters do not appear?

Post by jvierra »

Can you please fix the posted code so I can download it and I will take a look.

jvierra
Posts: 14547
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Why do some of my parameters do not appear?

Post by jvierra »

Note that usually this is caused by a subtle syntax error. Finding it can be an issue.

jvierra
Posts: 14547
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Why do some of my parameters do not appear?

Post by jvierra »

I can verify that there is one very bad syntax and structural error in the declaration of "Move-AdUserOU"
Layout the parameters in a more consistent way and you will see it. You have a double declaration.

User avatar
weiyen.tan@gmail.com
Posts: 55
Joined: Wed Apr 09, 2014 2:06 am

Re: Why do some of my parameters do not appear?

Post by weiyen.tan@gmail.com »

jvierra wrote:I can verify that there is one very bad syntax and structural error in the declaration of "Move-AdUserOU"
Layout the parameters in a more consistent way and you will see it. You have a double declaration.
Forgive me if my assumptions are wrong, but when you say I have double declarations, it means that I have declared the parameters twice? In the case of my parameter sets? textfile and csv?

If this is the case I have amended them and renamed the files to something else...still no dice....I wiull have to keep looking.

jvierra
Posts: 14547
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Why do some of my parameters do not appear?

Post by jvierra »

Look at the parameter declarations. You have incorrectly specified 'Parameter(,,,)} for the last parameter.

User avatar
weiyen.tan@gmail.com
Posts: 55
Joined: Wed Apr 09, 2014 2:06 am

Re: Why do some of my parameters do not appear?

Post by weiyen.tan@gmail.com »

The last parameter I have is:

Code: Select all

	[Parameter(ParameterSetName = 'username')]
		$uname
	)
	
However I cannot see any ,,, like you mentioned....
As I have been using the function builder in Powershell studio I have been noticing that it has been entering in , in the position field (in the function builder).

Locked