Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PowerShell's Add-Member results in an error

Why does the script below come up with the following error?

"Add-Member : Cannot process command because of one or more missing
mandatory parameters: InputObject.
+ $obj = Add-Member <<<< -MemberType NoteProperty -Name ComputerName -Value $ComputerName
+ CategoryInfo : InvalidArgument: (:) [Add-Member], ParameterBindingException
+ FullyQualifiedErrorId : MissingMandatoryParameter,Microsoft.PowerShell.Commands.AddMemberCommand"

Script

# Receives the computer name and stores the required results in $obj.
Function WorkerNetworkAdaptMacAddress {
    Param($ComputerName)

    $colItems = GWMI -cl "Win32_NetworkAdapterConfiguration" -name "root\CimV2" -comp $ComputerName -filter "IpEnabled = TRUE"
    $obj = New-Object -TypeName PSobject
    ForEach ($objItem in $colItems)
    {
        $obj = Add-Member -MemberType NoteProperty -Name ComputerName -Value $ComputerName
        $obj = Add-Member -MemberType NoteProperty -Name MacAddress -Value $objItem.MacAddress
        $obj = Add-Member -MemberType NoteProperty -Name IPAdress -Value $objitem.IpAddress
    }
    Write-Output $obj
}

# Receives the computer name and passes it to WorkerNetworkAdaptMacAddress.

Function Get-NetworkAdaptMacAddress {
    begin {}
    process{
        WorkerNetworkAdaptMacAddress -computername $_
    }
    end {}
}

# Passes a computer name to get-networkAdaptMacAddress
'tbh00363' | Get-NetworkAdaptMacAddress
like image 314
resolver101 Avatar asked Jan 25 '12 13:01

resolver101


4 Answers

You need to move the PSObject creation into the loop. Otherwise, you'll get errors that the properties already exist on the object.

Secondly, you need to tell Add-Member on which object to operate. You do it either by piping the object to the cmdlet or by specifying it on the InputObject parameter. Finally, return the object back to the pipeline by specifying the PassThru switch on the last Add-Member call:

ForEach ($objItem in $colItems)
{
    $obj = New-Object -TypeName PSobject
    Add-Member -InputObject $obj -MemberType NoteProperty -Name ComputerName -Value $ComputerName
    Add-Member -InputObject $obj -MemberType NoteProperty -Name MacAddress -Value $objItem.MacAddress
    Add-Member -InputObject $obj -MemberType NoteProperty -Name IPAddress -Value $objitem.IpAddress -PassThru
}

Alternatively, you could simplify the process with New-Object's -Property parameter:

Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $ComputerName -Filter "IpEnabled=TRUE" | Foreach-Object {
    New-Object -TypeName PSobject -Property @{
        ComputerName=$ComputerName
        MacAddress=$_.MacAddress
        IPAddress=$_.IpAddress
    }
}

Or by using Select-Object:

Get-WmiObject ... | Select-Object @{n='ComputerName';e={$_.__SERVER}},MacAddress,IpAddress
like image 56
Shay Levy Avatar answered Oct 02 '22 14:10

Shay Levy


First you need to specify the input object to which the property should be added by piping it to the Add-Member cmdlet.
Then, if you want the cmdlet to return the modified object, you should invoke it with the -PassThru argument:

When you use the PassThru parameter, Add-Member returns the newly-extended object. Otherwise, this cmdlet does not generate any output.

Here's a slightly modified version of your script:

$obj = $objItem | Add-Member -MemberType NoteProperty -Name ComputerName -Value $ComputerName -PassThru

However, since in your case you don't really need to save the output object in a new variable, you could also simply say:

$obj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $ComputerName
like image 31
Enrico Campidoglio Avatar answered Oct 02 '22 14:10

Enrico Campidoglio


Try like this:

$objcol = @()
ForEach ($objItem in $colItems)
{
    $obj = New-Object System.Object
    $obj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $ComputerName
    $obj | Add-Member -MemberType NoteProperty -Name MacAddress -Value $objItem.MacAddress
    $obj | Add-Member -MemberType NoteProperty -Name IPAdress -Value $objitem.IpAddress
    $objcol += $obj
}
Write-Output $objcol
like image 32
CB. Avatar answered Oct 02 '22 14:10

CB.


As indicated by Enrico, Shay and Christian, you should specify the object on which Add-Member operates, either by piping the object to Add-Member or by explicitly specifying the object on the InputObject parameter. When adding multiple members using Add-Member I usually add the PassThru switch to avoid repeating the InputObject and to provide a visual cue.

ForEach ($objItem in $colItems) {
  $obj = New-Object PSobject
  $obj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $ComputerName -PassThru ` 
       | Add-Member -MemberType NoteProperty -Name MacAddress -Value $objItem.MacAddress -PassThru `
       | Add-Member -MemberType NoteProperty -Name IPAdress -Value $objitem.IpAddress -PassThru
}
like image 26
jon Z Avatar answered Oct 02 '22 16:10

jon Z