Add-Member error

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 2 years, 2 months ago.

  • Author
    Posts
  • #25565
    Profile photo of
    Anonymous

    Hi,
    I want to add multiple values to a property (but values are not array) in a PSObject. For example the below code.

    $ComputerName = 'Computer1','Computer2','Computer3'
    $Obj = New-Object -TypeName PSObject
    ForEach($C in $ComputerName)
    {
        $Obj | Add-Member -MemberType NoteProperty -Name 'ComputerName' -Value $C
    }
    $Obj
    

    The below is the output I am expecting

    ComputerName
    ———————-
    Computer1
    Computer2
    Computer3

    ****
    When I run the above code, I am getting the below error.

    Add-Member : Cannot add a member with the name "ComputerName" because a member with that name already exists. If you want to overwrite the member anyway, use the Force parameter to overwrite it.
    At line:3 char:22
    +     $Obj | Add-Member < <<<  -MemberType NoteProperty -Name "ComputerName" -Value $C
        + CategoryInfo          : InvalidOperation: (@{ComputerName=Computer1}:PSObject) [Add-Member], InvalidOperationException
        + FullyQualifiedErrorId : MemberAlreadyExists,Microsoft.PowerShell.Commands.AddMemberCommand
    
    Add-Member : Cannot add a member with the name "ComputerName" because a member with that name already exists. If you want to overwrite the member anyway, use the Force parameter to overwrite it.
    At line:3 char:22
    +     $Obj | Add-Member <<<<  -MemberType NoteProperty -Name "ComputerName" -Value $C
        + CategoryInfo          : InvalidOperation: (@{ComputerName=Computer1}:PSObject) [Add-Member], InvalidOperationException
        + FullyQualifiedErrorId : MemberAlreadyExists,Microsoft.PowerShell.Commands.AddMemberCommand
    
    Function LogPathValidation
    {
    	If(Test-Path $PSScriptRoot)
    	{
    		If(Test-Path $($PSScriptRoot+"\Logs)"))
    		{
    			Write-Host "Green" -fore green
    			If(Test-Path $($PSScriptRoot+"\Logs\Temp"))
    			{
    				Write-Host "Green" -fore green
    			}
    			Else{ New-Item -Path $($PSScriptRoot+"\Logs") -Name "Temp" -ItemType Directory -ErrorAction "SilentlyContinue"}
    		}
    		Else
    		{
    			New-Item -Path $($PSScriptRoot) -Name "Logs" -ItemType Directory -ErrorAction "SilentlyContinue"
    			New-Item -Path $($PSScriptRoot+"\Logs") -Name "Temp" -ItemType Directory -ErrorAction "SilentlyContinue"
    		}
    	}
    	Else
    	{
    		Write-Host "It should never run"
    	}
    }
    
  • #25568
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    In the output you've described, that's what you'd get if you had three different objects, each one with a ComputerName property, rather than one object with a ComputerName property containing three values. That code might look something like this:

    $ComputerName = 'Computer1','Computer2','Computer3'
    ForEach($C in $ComputerName)
    {
        New-Object psobject -Property @{ ComputerName = $C }
    }
    

You must be logged in to reply to this topic.