Using PowerShell's Add-Member results in an error

柔情痞子 提交于 2019-12-10 13:28:30

问题


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

回答1:


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



回答2:


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



回答3:


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



回答4:


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
}



回答5:


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


来源:https://stackoverflow.com/questions/9003167/using-powershells-add-member-results-in-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!