Problems passing parameters into Get-ADComputer/Get-ADUser

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 1 year, 11 months ago.

  • Author
    Posts
  • #28353
    Profile photo of Karson Van Meeteren
    Karson Van Meeteren
    Participant

    I'm specifying mandatory input parameters as part of a function I'm writing, but I'm wondering how the native AD cmdlets like that string input.

    Function Get-IAAMLincPassException
    {
    	[CmdletBinding()]
    	[OutputType([System.Object])]
    	Param (
    		# Name should be a valid computer name.
    		[Parameter(Mandatory = $true,
    				   ValueFromPipeline = $true,
    				   ValueFromPipelineByPropertyName = $true,
    				   ValueFromRemainingArguments = $false)]
    		[Alias('HostName', 'Identity', 'DNSHostName')]
    		[string[]]$computerName,
    		[Parameter(Mandatory = $true,
    				   ValueFromPipeline = $false)]
    		[string[]]$lastName,
    		[Parameter(Mandatory = $true,
    				   ValueFromPipeline = $false)]
    		[string[]]$firstName
    	)
    	Begin
    	{
    	}
    	Process
    	{
    			if ($computerInfo = Get-ADComputer $computerName -Properties * -ErrorAction Stop)
    			{
    				if ($userInfo = Get-ADUser -Filter {Surname -eq $lastName -and GivenName -eq $firstName}  )
    				{
    

    This script excerpt bonks out with

    Get-ADComputer : Cannot convert 'System.String[]' to the type 'Microsoft.ActiveDirectory.Management.ADComputer'
    required by parameter 'Identity'. Specified method is not supported.
    

    I'll keep googling, but perhaps someone can chime in?

    Thanks!

  • #28354
    Profile photo of Don Jones
    Don Jones
    Keymaster

    It's because you're accepting a string[], which is a string array. -Identity will accept a single string, not an array. So if you're getting more than one, you need to enumerate them and only query for one at a time.

  • #28366
    Profile photo of Karson Van Meeteren
    Karson Van Meeteren
    Participant

    Makes sense why specifying the parameter as Read-Host with only one input works. Sounds like a foreach ($computer in $computerName) {scriptblock} will work so I only enumerate one object at a time?

    Thank you, Don!

  • #28367
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Yup. Exactly.

    It'd work with pipeline input as-is, because PROCESS is an implicit ForEach.

You must be logged in to reply to this topic.