Using PowerShell's Add-Member results in an er

2019-06-18 06:59发布

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

5条回答
地球回转人心会变
2楼-- · 2019-06-18 07:27

You're assigning the result of Add-Member to a variable, not adding it to a property collection within $obj.

Instead of

$obj = Add-Member -MemberType NoteProperty -Name ComputerName -Value $ComputerName

Try this:

Add-Member -MemberType NoteProperty -Name ComputerName -Value $ComputerName -inputObject $obj
查看更多
萌系小妹纸
3楼-- · 2019-06-18 07:34

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
查看更多
Anthone
4楼-- · 2019-06-18 07:35

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
查看更多
叼着烟拽天下
5楼-- · 2019-06-18 07:36

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
}
查看更多
再贱就再见
6楼-- · 2019-06-18 07:49

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
查看更多
登录 后发表回答