Add-Member error

Welcome Forums General PowerShell Q&A Add-Member error

Viewing 1 reply thread
  • Author
    Posts
    • #25565
      Inactive
      Topics: 29
      Replies: 32
      Points: 0
      Rank: Member

      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
      Member
      Topics: 9
      Replies: 2322
      Points: 0
      Rank: Member

      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 }
      }
      
Viewing 1 reply thread
  • The topic ‘Add-Member error’ is closed to new replies.