New-Object add-member - cannot add a member - powershell - powershell

I have a problem :(
I have many errors when I add New object with the same name of property which previously Object .
"Add-Member : Cannot add a member with the name "ServerName" because a
member with that name already exists. To overwrite the member anyway,
add the Force par ameter to your command."
it works but with errors;)
Have someone a solution for me ??
Below example code :
$OutputObj = #()
$OutputObj += New-Object -TypeName PSobject
$OutputObj | Add-Member -MemberType NoteProperty -Name ServerName -Value "server1"
$OutputObj | Add-Member -MemberType NoteProperty -Name Status -Value "online"
$OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value "01:20"
$OutputObj += New-Object -TypeName PSobject
$OutputObj | Add-Member -MemberType NoteProperty -Name ServerName -Value "server2"
$OutputObj | Add-Member -MemberType NoteProperty -Name Status -Value "online"
$OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value "01340"

You are adding the member on the array, not on the object. Try the following:
$OutputObj = #()
$NewObj = New-Object -TypeName PSobject
$NewObj | Add-Member -MemberType NoteProperty -Name ServerName -Value "server1"
$NewObj | Add-Member -MemberType NoteProperty -Name Status -Value "online"
$NewObj | Add-Member -MemberType NoteProperty -Name Uptime -Value "01:20"
$OutputObj += $NewObj
$NewObj = New-Object -TypeName PSobject
$NewObj | Add-Member -MemberType NoteProperty -Name ServerName -Value "server2"
$NewObj | Add-Member -MemberType NoteProperty -Name Status -Value "online"
$NewObj | Add-Member -MemberType NoteProperty -Name Uptime -Value "01340"
$OutputObj += $NewObj
Alernatively, I would do the following:
$OutputObj = #()
$OutputObj += New-Object -TypeName PSobject -Property #{
ServerName = "server1"
Status = "Online"
Uptime = "01:20"
}
$OutputObj += New-Object -TypeName PSobject -Property #{
ServerName = "server2"
Status = "Online"
Uptime = "01340"
}
Since Add-Member destroys and generates a new object each time it is used.

Related

Questions about Powershell objects, property names, multiple property values with the same name and dot sourcing issues

There is code at the bottom of this to help make it easy to reproduce what I'm asking in my questions. My questions are:
$parentObject.Children.Children.Andrew."Birthday Info" --- is there a programmatic way to know to need quotes and add quotes?"
$parentObject.Children.Robert.Children.Peter."Favorite Foods" --- I understand it creates a property name conflict if I try to add "Name" with a different value more than once, how can I add "Name" with a different food value multiple times? Should it be an array that I keep adding to and replacing/updating?
$parentObject."01/01/1960". --- why can't I dot source the child properties
$parentObject.Children.Robert. --- why can't I dot source the child properties, the 3rd level seems to be the limit?
$parentObject.01/01/1960; --- fails, returns a "0"
$parentObject."01/01/1960"; --- returns property names "Location"
and "Years Together"
$parentObject.{01/01/1960}; --- returns
property names "Location" and "Years Together"
$parentObject."01/01/1960". --- does not dot source
$parentObject.{01/01/1960}. --- does not dot source
$parentObject."01/01/1960".Location; --- returns property value
"Hawaii"
$parentObject.{01/01/1960}.Location; --- returns property
value "Hawaii"
$parentObject."01/01/1960"."Years Together"; --- returns property value "72"
$parentObject.{01/01/1960}.{Years Together}; --- returns property value "72"
$parentObject.anniversary - 01/01/1960; --- fails, performs a math
equation
$parentObject."anniversary - 01/01/1960"; --- returns
property values "Hawaii" and "62"
$parentObject.{anniversary - 01/01/1960}; --- returns property values "Hawaii" and "62"
$parentObject."anniversary - 01/01/1960". --- does not dot source
$parentObject.{anniversary - 01/01/1960}. --- does not dot source
Full code:
#Region Instantiate
$foodsObject = New-Object -TypeName psobject;
$birthDayObject = New-Object -TypeName psobject;
$numberOfBirthdaysObject = New-Object -TypeName psobject;
$grandChildObject = New-Object -TypeName psobject;
$childObject = New-Object -TypeName psobject;
$parentObject = New-Object -TypeName psobject;
$anniversaryObject = New-Object -TypeName psobject;
#EndRegion /Instantiate
#Region Peter
$numberOfBirthdaysObject = New-Object -TypeName psobject;
$numberOfBirthdaysObject | Add-Member -MemberType NoteProperty -Name "Count" -Value "22";
$birthDateObject = New-Object -TypeName psobject;
$birthDateObject | Add-Member -MemberType NoteProperty -Name "Date" -Value "01/01/2000";
$birthDateObject | Add-Member -MemberType NoteProperty -Name "Birthday Count" -Value $numberOfBirthdaysObject;
$birthDayObject = New-Object -TypeName psobject;
$birthDayObject | Add-Member -MemberType NoteProperty -Name "Birthday Info" -Value $birthDateObject;
$foodsObject = New-Object -TypeName psobject;
$foodsObject | Add-Member -MemberType NoteProperty -Name "Spaghetti" -Value "Spaghetti";
$foodsObject | Add-Member -MemberType NoteProperty -Name "Pizza" -Value "Pizza";
$foodsObject | Add-Member -MemberType NoteProperty -Name "Ice cream" -Value "Ice cream";
$personalInformationObject = New-Object -TypeName psobject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Birthday" -Value $birthDayObject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Color" -Value "Red";
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Foods" -Value $foodsObject;
$grandChildObject | Add-Member -MemberType NoteProperty -Name "Peter" -Value $personalInformationObject;
#EndRegion /Peter
#Region Andrew
$numberOfBirthdaysObject = New-Object -TypeName psobject;
$numberOfBirthdaysObject | Add-Member -MemberType NoteProperty -Name "Count" -Value "20";
$birthDateObject = New-Object -TypeName psobject;
$birthDateObject | Add-Member -MemberType NoteProperty -Name "Date" -Value "01/01/2002";
$birthDateObject | Add-Member -MemberType NoteProperty -Name "Birthday Count" -Value $numberOfBirthdaysObject;
$birthDayObject = New-Object -TypeName psobject;
$birthDayObject | Add-Member -MemberType NoteProperty -Name "Birthday Info" -Value $birthDateObject;
$foodsObject = New-Object -TypeName psobject;
$foodsObject | Add-Member -MemberType NoteProperty -Name "Big Mac" -Value "Big Mac";
$foodsObject | Add-Member -MemberType NoteProperty -Name "Soup" -Value "Soup";
$foodsObject | Add-Member -MemberType NoteProperty -Name "Cheese cake" -Value "Cheese cake";
$personalInformationObject = New-Object -TypeName psobject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Birthday" -Value $birthDayObject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Color" -Value "Green";
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Foods" -Value $foodsObject;
$grandChildObject | Add-Member -MemberType NoteProperty -Name "Andrew" -Value $personalInformationObject;
#EndRegion /Andrew
#Region Robert
$numberOfBirthdaysObject = New-Object -TypeName psobject;
$numberOfBirthdaysObject | Add-Member -MemberType NoteProperty -Name "Count" -Value "47";
$birthDateObject = New-Object -TypeName psobject;
$birthDateObject | Add-Member -MemberType NoteProperty -Name "Date" -Value "01/01/1975";
$birthDateObject | Add-Member -MemberType NoteProperty -Name "Birthday Count" -Value $numberOfBirthdaysObject;
$birthDayObject = New-Object -TypeName psobject;
$birthDayObject | Add-Member -MemberType NoteProperty -Name "Birthday Info" -Value $birthDateObject;
$foodsObject = New-Object -TypeName psobject;
$foodsObject | Add-Member -MemberType NoteProperty -Name "Burger King" -Value "Burger King";
$foodsObject | Add-Member -MemberType NoteProperty -Name "Cheese Pizza" -Value "Cheese Pizza";
$foodsObject | Add-Member -MemberType NoteProperty -Name "Beer" -Value "Beer";
$personalInformationObject = New-Object -TypeName psobject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Birthday" -Value $birthDayObject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Color" -Value "Maroon";
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Foods" -Value $foodsObject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Children" -Value $grandChildObject;
$childObject | Add-Member -MemberType NoteProperty -Name "Robert" -Value $personalInformationObject;
#EndRegion /Robert
$grandChildObject = New-Object psobject;
#Region Lisa
$numberOfBirthdaysObject = New-Object -TypeName psobject;
$numberOfBirthdaysObject | Add-Member -MemberType NoteProperty -Name "Count" -Value "25";
$birthDateObject = New-Object -TypeName psobject;
$birthDateObject | Add-Member -MemberType NoteProperty -Name "Date" -Value "01/01/1997";
$birthDateObject | Add-Member -MemberType NoteProperty -Name "Birthday Count" -Value $numberOfBirthdaysObject;
$birthDayObject = New-Object -TypeName psobject;
$birthDayObject | Add-Member -MemberType NoteProperty -Name "Birthday Info" -Value $birthDateObject;
$foodsObject = New-Object -TypeName psobject;
$foodsObject | Add-Member -MemberType NoteProperty -Name "Burgers" -Value "Burgers";
$foodsObject | Add-Member -MemberType NoteProperty -Name "Rice" -Value "Rice";
$foodsObject | Add-Member -MemberType NoteProperty -Name "Cherry snowball" -Value "Cherry snowball";
$personalInformationObject = New-Object -TypeName psobject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Birthday" -Value $birthDayObject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Color" -Value "Purple";
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Foods" -Value $foodsObject;
$grandChildObject | Add-Member -MemberType NoteProperty -Name "Lisa" -Value $personalInformationObject;
#EndRegion /Lisa
#Region Thomas
$numberOfBirthdaysObject = New-Object -TypeName psobject;
$numberOfBirthdaysObject | Add-Member -MemberType NoteProperty -Name "Count" -Value "44";
$birthDateObject = New-Object -TypeName psobject;
$birthDateObject | Add-Member -MemberType NoteProperty -Name "Date" -Value "01/01/1978";
$birthDateObject | Add-Member -MemberType NoteProperty -Name "Birthday Count" -Value $numberOfBirthdaysObject;
$birthDayObject = New-Object -TypeName psobject;
$birthDayObject | Add-Member -MemberType NoteProperty -Name "Birthday Info" -Value $birthDateObject;
$foodsObject = New-Object -TypeName psobject;
$foodsObject | Add-Member -MemberType NoteProperty -Name "Chicken" -Value "Chicken";
$foodsObject | Add-Member -MemberType NoteProperty -Name "Pit beef" -Value "Pit beef";
$foodsObject | Add-Member -MemberType NoteProperty -Name "Coke float" -Value "Coke float";
$personalInformationObject = New-Object -TypeName psobject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Birthday" -Value $birthDayObject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Color" -Value "Yellow";
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Foods" -Value $foodsObject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Children" -Value $grandChildObject;
$childObject | Add-Member -MemberType NoteProperty -Name "Thomas" -Value $personalInformationObject;
#EndRegion /Thomas
#Region Jason and Pamela
#Region Jason
$numberOfBirthdaysObject = New-Object -TypeName psobject;
$numberOfBirthdaysObject | Add-Member -MemberType NoteProperty -Name "Count" -Value "80";
$birthDateObject = New-Object -TypeName psobject;
$birthDateObject | Add-Member -MemberType NoteProperty -Name "Date" -Value "01/01/1942";
$birthDateObject | Add-Member -MemberType NoteProperty -Name "Birthday Count" -Value $numberOfBirthdaysObject;
$birthDayObject = New-Object -TypeName psobject;
$birthDayObject | Add-Member -MemberType NoteProperty -Name "Birthday Info" -Value $birthDateObject;
$foodsObject = New-Object -TypeName psobject;
$foodsObject | Add-Member -MemberType NoteProperty -Name "Grits" -Value "Grits";
$foodsObject | Add-Member -MemberType NoteProperty -Name "Burgers" -Value "Burgers";
$foodsObject | Add-Member -MemberType NoteProperty -Name "Malt drink" -Value "Malt drink";
$personalInformationObject = New-Object -TypeName psobject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Birthday" -Value $birthDayObject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Color" -Value "Brown";
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Foods" -Value $foodsObject;
$parentObject | Add-Member -MemberType NoteProperty -Name "Jason" -Value $personalInformationObject;
#EndRegion /Jason
#Region Pamela
$numberOfBirthdaysObject = New-Object -TypeName psobject;
$numberOfBirthdaysObject | Add-Member -MemberType NoteProperty -Name "Count" -Value "78";
$birthDateObject = New-Object -TypeName psobject;
$birthDateObject | Add-Member -MemberType NoteProperty -Name "Date" -Value "01/01/1944";
$birthDateObject | Add-Member -MemberType NoteProperty -Name "Birthday Count" -Value $numberOfBirthdaysObject;
$birthDayObject = New-Object -TypeName psobject;
$birthDayObject | Add-Member -MemberType NoteProperty -Name "Birthday Info" -Value $birthDateObject;
$foodsObject = New-Object -TypeName psobject;
$foodsObject | Add-Member -MemberType NoteProperty -Name "Rice" -Value "Rice";
$foodsObject | Add-Member -MemberType NoteProperty -Name "Cheese" -Value "Cheese";
$foodsObject | Add-Member -MemberType NoteProperty -Name "Popsicle" -Value "Popsicle";
$personalInformationObject = New-Object -TypeName psobject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Birthday" -Value $birthDayObject;
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Color" -Value "Pink";
$personalInformationObject | Add-Member -MemberType NoteProperty -Name "Favorite Foods" -Value $foodsObject;
$parentObject | Add-Member -MemberType NoteProperty -Name "Pamela" -Value $personalInformationObject;
#EndRegion /Pamela
$anniversaryObject | Add-Member -MemberType NoteProperty -Name "Location" -Value "Hawaii";
$anniversaryObject | Add-Member -MemberType NoteProperty -Name "Years Together" -Value "62";
if ($parentObject.Children -eq $null)
{
$parentObject | Add-Member -MemberType NoteProperty -Name "Father" -Value "Jason";
$parentObject | Add-Member -MemberType NoteProperty -Name "Mother" -Value "Pamela";
$parentObject | Add-Member -MemberType NoteProperty -Name "Children" -Value $childObject;
$parentObject | Add-Member -MemberType NoteProperty -Name "01/01/1960" -Value $anniversaryObject;
$parentObject | Add-Member -MemberType NoteProperty -Name "anniversary - 01/01/1960" -Value $anniversaryObject;
}
else
{
$parentObject | Add-Member -MemberType NoteProperty -Name "Father" -Value "Jason";
$parentObject | Add-Member -MemberType NoteProperty -Name "Mother" -Value "Pamela";
$parentObject.Children = $childObject;
$parentObject | Add-Member -MemberType NoteProperty -Name "01/01/1950" -Value $anniversaryObject;
$parentObject | Add-Member -MemberType NoteProperty -Name "anniversary - 01/01/1950" -Value $anniversaryObject;
}
#EndRegion Jason and Pamela
I think your elemental object should be a single person with the desired properties, including refences to child objects. Then define a marriage which references the individual people and stores the date & location.
Best to just avoid spaces in property names
Use tab completion to explore what's available through dot notation
FavFoods is created as an array by splitting a string that uses an arbitrary separator (+)
ScriptProperties are used to keep time-dependent values current
Play with the objects generated by this code:
#'
Name,Birthday,FavColor,FavFoods
Jason,01/01/1942,Brown,Grits+Burgers+Malt Drink
Pamela,01/01/1944,Pink,Rice+Cheese+Popsicle
Robert,01/01/1975,Maroon,Burger King+Cheese Pizza+Beer
Thomas,01/01/1978,Yellow,Chicken+Pit Beef+Coke float
Peter,1/1/2000,Red,Spaghetti+Pizza+Ice cream
Andrew,1/1/2002,Green,Big Mac+Soup+Cheesecake
'# | convertFrom-Csv | ForEach {[PSCustomObject]#{
'Name' = $_.Name
'Birthday' = $_.Birthday
'FavColor' = $_.FavColor
'FavFoods' = $_.FavFoods.Split('+')
'Children' = #{}
} | Add-Member -MemberType ScriptProperty -Name Age -Value { ([DateTime]((Get-Date) - [DateTime]($This.Birthday)).Ticks).Year - 1 } -PassTHru
} | FOrEach { $hash = #{} } {
$hash.Add($_.Name,$_)
} { $hash } | sv People
'Jason' , 'Pamela' | ForEach {
$Parent = $_
'Robert' , 'THomas' | ForEach {
$People[$Parent].Children.Add( $_ , $People[$_] )
}
}
'Peter' , 'Andrew' | ForEach {
$People.Robert.Children.Add( $_ , $People[$_] )
}
$Marriage = [PSCustomObject]#{
Spouses = #( $People['Jason'], $People['Pamela'] )
Date = '1/1/1960'
Location = 'Hawaii'
Children = $People['Jason'].Children.Keys + $People['Pamela'].Children.Keys | select -Unique | ForEach {$hash = #{} } { $hash.Add($_ , $People[$_] ) } { $hash }
}
$Marriage | Add-Member -MemberType ScriptProperty -Name 'YearsTogether' -Value { ([DateTime]((Get-Date) - [DateTime]($This.Date)).Ticks).Year - 1 }
Samples:
PS C:\> $People.GetEnumerator() | ForEach { $_.value }
Name : Jason
Birthday : 01/01/1942
FavColor : Brown
FavFoods : {Grits, Burgers, Malt Drink}
Children : {THomas, Robert}
Age : 80
Name : Robert
Birthday : 01/01/1975
FavColor : Maroon
FavFoods : {Burger King, Cheese Pizza, Beer}
Children : {Peter, Andrew}
Age : 47
Name : Pamela
Birthday : 01/01/1944
FavColor : Pink
FavFoods : {Rice, Cheese, Popsicle}
Children : {THomas, Robert}
Age : 78
Name : Thomas
Birthday : 01/01/1978
FavColor : Yellow
FavFoods : {Chicken, Pit Beef, Coke float}
Children : {}
Age : 44
Name : Peter
Birthday : 1/1/2000
FavColor : Red
FavFoods : {Spaghetti, Pizza, Ice cream}
Children : {}
Age : 22
Name : Andrew
Birthday : 1/1/2002
FavColor : Green
FavFoods : {Big Mac, Soup, Cheesecake}
Children : {}
Age : 20
PS C:\>>$Marriage.Children
Name Value
---- -----
THomas #{Name=Thomas; Birthday=01/01/1978; FavColor=Yellow; F...
Robert #{Name=Robert; Birthday=01/01/1975; FavColor=Maroon; F...
PS C:\>>$Marriage.Children.Robert.Children
Name Value
---- -----
Peter #{Name=Peter; Birthday=1/1/2000; FavColor=Red; FavFood...
Andrew #{Name=Andrew; Birthday=1/1/2002; FavColor=Green; FavF...
PS C:\>>$Marriage.Children.Robert.Children.Values | select Name, FavFoods
Name FavFoods
---- --------
Peter {Spaghetti, Pizza, Ice cream}
Andrew {Big Mac, Soup, Cheesecake}

Powershell object doesn't add member

For some reason when I wrote this Powershell object, the last member does not get displayed in my result:
$resultObject = New-Object -TypeName psobject
$resultObject | Add-Member -MemberType NoteProperty -Name Date -Value $date
$resultObject | Add-Member -MemberType NoteProperty -Name TotalPCs -Value $ad
$resultObject | Add-Member -MemberType NoteProperty -Name UnmanagedTotal -Value $total
$resultObject | Add-Member -MemberType NoteProperty -Name TotalEMEA -Value $adEMEA
$resultObject | Add-Member -MemberType NoteProperty -Name UnmanagedEMEA -Value $totalEMEA
$resultObject | Add-Member -MemberType NoteProperty -Name TotalAFRICA -Value $adAFRICA
$resultObject | Add-Member -MemberType NoteProperty -Name UnmanagedAFRICA -Value $totalAFRICA
$resultObject | Add-Member -MemberType NoteProperty -Name TotalAMERICAS -Value $adAMERICAS
$resultObject | Add-Member -MemberType NoteProperty -Name UnmanagedAMERICAS -Value $totalAMERICAS
$resultObject | Add-Member -MemberType NoteProperty -Name TotalAPAC -Value $adAPAC
$resultObject | Add-Member -MemberType NoteProperty -Name UnmanagedAPAC -Value $totalAPAC
$resultObject | ft -AutoSize | out-file -FilePath "C:\BAC\WSUSScripts\KPI\ManagedComputers.txt" -Append
I have no clue why since every info is retrieved the same way and for the other member it all works. The last member just falls off.

InputObject is null - Powershell DataTable

I'm attempting to output an object array (of event logs) to a DataTable with the expectation to pipe into SQL.
The basics are:
Get some forwarded events
Process them to pull some required info out
Output to a data table
function Get-Type
{
param($type)
$types = #(
'System.Boolean',
'System.Byte[]',
'System.Byte',
'System.Char',
'System.Datetime',
'System.Decimal',
'System.Double',
'System.Guid',
'System.Int16',
'System.Int32',
'System.Int64',
'System.Single',
'System.UInt16',
'System.UInt32',
'System.UInt64')
if ( $types -contains $type ) {
Write-Output "$type"
}
else {
Write-Output 'System.String'
}
} #Get-Type
function Out-DataTable
{
[CmdletBinding()]
param([Parameter(Position=0, Mandatory=$true, ValueFromPipeline = $true)] [PSObject[]]$InputObject)
Begin
{
$dt = new-object Data.datatable
$First = $true
}
Process
{
Write-Output "test"
foreach ($object in $InputObject)
{
$DR = $DT.NewRow()
foreach($property in $object.PsObject.get_properties())
{
if ($first)
{
$Col = new-object Data.DataColumn
$Col.ColumnName = $property.Name.ToString()
if ($property.value)
{
if ($property.value -isnot [System.DBNull]) {
$Col.DataType = [System.Type]::GetType("$(Get-Type $property.TypeNameOfValue)")
}
}
$DT.Columns.Add($Col)
}
if ($property.Gettype().IsArray) {
$DR.Item($property.Name) =$property.value | ConvertTo-XML -AS String -NoTypeInformation -Depth 1
}
else {
$DR.Item($property.Name) = $property.value
}
}
$DT.Rows.Add($DR)
$First = $false
}
}
End
{
Write-Output #(,($dt))
}
} #Out-DataTable
$allEvents = Get-WinEvent -LogName ForwardedEvents | Where-Object{$_.Id -ne 111}
$outEvents = #()
$dt = $null
foreach ($curEvent in $allEvents){
$curObj = $null
switch ($curEvent.ID) {
4624 {
$curObj = New-Object -TypeName PSObject
Add-Member -InputObject $curObj -MemberType NoteProperty -Name TimeCreated -Value ([datetime]$curEvent.TimeCreated)
Add-Member -InputObject $curObj -MemberType NoteProperty -Name Action -Value $curEvent.TaskDisplayName
Add-Member -InputObject $curObj -MemberType NoteProperty -Name MachineName -Value $curEvent.MachineName
Add-Member -InputObject $curObj -MemberType NoteProperty -Name UserName -Value ((($curEvent.Message).Split([Environment]::NewLine)[36]).split(":")[1]).Trim()
Add-Member -InputObject $curObj -MemberType NoteProperty -Name LoginID -Value ((($curEvent.Message).Split([Environment]::NewLine)[40]).split(":")[1]).Trim()
Add-Member -InputObject $curObj -MemberType NoteProperty -Name SourceIP -Value ((($curEvent.Message).Split([Environment]::NewLine)[64]).split(":")[1]).Trim()
Add-Member -InputObject $curObj -MemberType NoteProperty -Name ID -Value $curEvent.Id
Add-Member -InputObject $curObj -MemberType NoteProperty -Name RecordID -Value $curEvent.RecordID
}
4647 {
$curObj = New-Object -TypeName PSObject
Add-Member -InputObject $curObj -MemberType NoteProperty -Name TimeCreated -Value ([datetime]$curEvent.TimeCreated)
Add-Member -InputObject $curObj -MemberType NoteProperty -Name Action -Value $curEvent.TaskDisplayName
Add-Member -InputObject $curObj -MemberType NoteProperty -Name MachineName -Value $curEvent.MachineName
Add-Member -InputObject $curObj -MemberType NoteProperty -Name UserName -Value ((($curEvent.Message).Split([Environment]::NewLine)[8]).split(":")[1]).Trim()
Add-Member -InputObject $curObj -MemberType NoteProperty -Name LoginID -Value ((($curEvent.Message).Split([Environment]::NewLine)[12]).split(":")[1]).Trim()
Add-Member -InputObject $curObj -MemberType NoteProperty -Name SourceIP -Value "Not Available"
Add-Member -InputObject $curObj -MemberType NoteProperty -Name ID -Value $curEvent.Id
Add-Member -InputObject $curObj -MemberType NoteProperty -Name RecordID -Value $curEvent.RecordID
}
4778 {
$curObj = New-Object -TypeName PSObject
Add-Member -InputObject $curObj -MemberType NoteProperty -Name TimeCreated -Value ([datetime]$curEvent.TimeCreated)
Add-Member -InputObject $curObj -MemberType NoteProperty -Name Action -Value "Reconnect"
Add-Member -InputObject $curObj -MemberType NoteProperty -Name MachineName -Value $curEvent.MachineName
Add-Member -InputObject $curObj -MemberType NoteProperty -Name UserName -Value ((($curEvent.Message).Split([Environment]::NewLine)[6]).split(":")[1]).Trim()
Add-Member -InputObject $curObj -MemberType NoteProperty -Name LoginID -Value ((($curEvent.Message).Split([Environment]::NewLine)[10]).split(":")[1]).Trim()
Add-Member -InputObject $curObj -MemberType NoteProperty -Name SourceIP -Value ((($curEvent.Message).Split([Environment]::NewLine)[24]).split(":")[1]).Trim()
Add-Member -InputObject $curObj -MemberType NoteProperty -Name ID -Value $curEvent.Id
Add-Member -InputObject $curObj -MemberType NoteProperty -Name RecordID -Value $curEvent.RecordID
}
4800 {
$curObj = New-Object -TypeName PSObject
Add-Member -InputObject $curObj -MemberType NoteProperty -Name TimeCreated -Value ([datetime]$curEvent.TimeCreated)
Add-Member -InputObject $curObj -MemberType NoteProperty -Name Action -Value "Locked"
Add-Member -InputObject $curObj -MemberType NoteProperty -Name MachineName -Value $curEvent.MachineName
Add-Member -InputObject $curObj -MemberType NoteProperty -Name UserName -Value ((($curEvent.Message).Split([Environment]::NewLine)[8]).split(":")[1]).Trim()
Add-Member -InputObject $curObj -MemberType NoteProperty -Name LoginID -Value ((($curEvent.Message).Split([Environment]::NewLine)[12]).split(":")[1]).Trim()
Add-Member -InputObject $curObj -MemberType NoteProperty -Name SourceIP -Value "Not Available"
Add-Member -InputObject $curObj -MemberType NoteProperty -Name ID -Value $curEvent.Id
Add-Member -InputObject $curObj -MemberType NoteProperty -Name RecordID -Value $curEvent.RecordID
}
Default { }
}
$outEvents += $curObj
}
$outEvents
$dt = Out-DataTable -InputObject $outEvents
When I run this, the last output of $outEvents lists all of the event objects with the correct details however trying to pipt it into Out-DataTable returns:
Out-DataTable : Cannot bind argument to parameter 'InputObject' because it is null.
At \\server\scripts\Repository\Write-UserLoginEvent\Write-UserLoginEvent.ps1:140 char:38
+ $dt = Out-DataTable -InputObject $outEvents
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Out-DataTable], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Out-DataTable
I've tried all different methods of formatting the data etc and I can't seem to get this function to accept my custom object array.
If I use a built in function like 'Get-Process' the function works correctly and returns the DataTable so I'm thinking it is something specific to the object I'm returning.
EDIT: Before I posted this, I also moved the DataTable segment into the loop, to attempt to print each event object as an individual DataTable. Hoping to identify where the issue was. It failed immediately on the first object.
So the issue was that I had an un-captured event type that had slipped through.
Because this event was not being formatted correctly there was a single event in the output that would not fit into the DataTable structure, causing the NULL error.
To identify the issue I put an Out-DataTable step inside the loop to process each object individually and in the output I could see that all objects succeeded except one. Tracking down that one I could see that I hadn't catered for that Event ID (8001).

PSObject with relational values - one to manny

want to populate the following PSObject so it can have a one:one or one:many relationship between IPs and hostnames.
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value ""
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value ""
This gives me the correct headings but when I try add more than one value, it will error one trying to add the repeat values:
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "1.1.1.1"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server1"
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "2.2.2.2"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server2"
Why is this?
Can do the one to many bit fairly easily:
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "1.1.1.1"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server1", "server2", "server3"
Either of these is the data set to extra from:
1.1.1.1 server1
2.2.2.2 server2
3.3.3.3 server3
4.5.6.7 server4 server5 server6 server7
8.8.8.8 server8
9.9.9.9 server9
1.1.1.1
server1
2.2.2.2
server2
3.3.3.3
server3
4.5.6.7
server4
server5
server6
server7
8.8.8.8
server8
9.9.9.9
server9
you want to create a list of objects like:
$ArrayList = New-Object System.Collections.ArrayList
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "1.1.1.1"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server1"
$ArrayList.Add($hostobj)
$hostobj = New-Object PSOBject
$hostobj | Add-Member -MemberType NoteProperty -Name "ip_address" -Value "2.2.2.2"
$hostobj | Add-Member -MemberType NoteProperty -Name "hostnames" -Value "server2"
$ArrayList.Add($hostobj)
so that $ArrayList contains all the host objects. and each object has one value for ip_address and hostnames.
If this does not help you please specify what you expect/what exactly you are trying to do/work with.

Is there a way to use a function to determine Add-Member value?

$thisplan = New-Object PSObject
Add-Member -InputObject $thisplan -MemberType NoteProperty -Name PlanID -Value $planid
Add-Member -InputObject $thisplan -MemberType NoteProperty -Name PlanName -Value $plan.name
Add-Member -InputObject $thisplan -MemberType NoteProperty -Name SessionPassword -Value $sessionpass
$schedule = $plan.backupConfiguration.advanceSchedule.periodSchedule
#DAY
add-Member -InputObject $thisplan -MemberType NoteProperty -Name DayRetention -Value $schedule.daySchedule.retentionCount
if ($schedule.daySchedule.retentionCount -gt 0)
{
$dstart = udptime($schedule.daySchedule.dayTime)
}
else
{
$dstart = ""
}
Add-Member -InputObject $thisplan -MemberType NoteProperty -Name DayStart -Value $dstart
########################
is there a way to make a function from the #day section?
This is a small part from my script.