I have a script that I’m trying to use AD computer objects to populate a computername parameter. The script is called servicecheck.ps1 and it’s designed to see if a service is present, not present, or if computers are unreachable.
[cmdletbinding()]
param (
[parameter(mandatory=$true)][string]$servicename,
[parameter(mandatory=$true,valuefrompipelinebypropertyname=$true)]
[alias('Name')][string[]]$computername
)
begin {}
process{
foreach ($single in $computername) {
Try {
invoke-command -ComputerName $single -ScriptBlock {param($rservicename) get-service `
-name $rservicename} -ArgumentList $servicename -ErrorAction stop | out-null
$success += “$single`r`n”
} catch {
if ($.exception -like “cannot find any service”) {$serviceerror += “$single`r`n”}
if ($.exception -like “connecting to remote”) {$pserror += “$single`r`n”}
}
}
}
end {
if ($success -eq $null) {$success = “None`r`n”}
if ($serviceerror -eq $null) {$serviceerror = “None`r`n”}
if ($pserror -eq $null) {$pserror = “None`r`n”}
write-output "Computers with $servicename present"
write-output "$success`r"
write-output "Computers without $servicename present"
write-output "$serviceerror`r"
write-output "Computers that couldn't be contacted"
write-output "$pserror"
}
When I try to run the command using the following line: Get-ADComputer -Filter * | .\servicecheck.ps1 -servicename bits
I get the following error for each computer that is fetched from get-adcomputer.
E:\servicecheck.ps1 : Cannot bind argument to parameter ‘computername’ because it is an empty array.
At line:1 char:28
- Get-ADComputer -Filter * | .\servicecheck.ps1 -servicename bits
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidData: (CN=LON-DC1,OU=D…C=Adatum,DC=com:PSObject) [servicecheck.ps1], ParameterB
indingValidationException - FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyArrayNotAllowed,servicecheck.ps1
- CategoryInfo : InvalidData: (CN=LON-DC1,OU=D…C=Adatum,DC=com:PSObject) [servicecheck.ps1], ParameterB
When I run the script like this: Get-ADComputer -Filter * | select @{n=‘name’;e={$_.name}} | .\servicecheck.ps1 -servicename bits
It works fine. I don’t want to have to resort to using a calculated property so am wondering why the name property in the adcomputer object isn’t matching up bypropertyname with the computername property I’ve setup with the alias of ‘name’
Thanks in advance for any insight anyone might have