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"
}
}