Author Posts

August 10, 2015 at 7:15 am

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
	Param (
		# Name should be a valid computer name.
		[Parameter(Mandatory = $true,
				   ValueFromPipeline = $true,
				   ValueFromPipelineByPropertyName = $true,
				   ValueFromRemainingArguments = $false)]
		[Alias('HostName', 'Identity', 'DNSHostName')]
		[Parameter(Mandatory = $true,
				   ValueFromPipeline = $false)]
		[Parameter(Mandatory = $true,
				   ValueFromPipeline = $false)]
			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?


August 10, 2015 at 7:25 am

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.

August 10, 2015 at 7:48 am

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!

August 10, 2015 at 7:49 am

Yup. Exactly.

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