PSObject with relational values - one to manny - powershell

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.

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.

New-Object add-member - cannot add a member - 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.

Powershell: Get VM Properties

I Created a VM and I want to export its properties in a CSV file.
what I tried does not give me the IPAddress, SwitchName, Macaddress.
$Data = #();
$VMs = Get-VM $VMName;
foreach($VM in $VMs){
$VMCustom = New-Object System.Object;
$VMCustom | Add-Member -Type NoteProperty -Name VMName -Value $VM.VMName;
# Get-VMNetworkAdapter -VMName $VMName | Select -expand IPAddresses
$VMCustom | Add-Member -Type NoteProperty -Name IPAddress -Value $VM.guest.IPAddresses;
$VMCustom | Add-Member -Type NoteProperty -Name SwitchName -Value $VM.MacAddress;
$VMCustom | Add-Member -Type NoteProperty -Name Status -Value $VM.State;
$VMCustom | Add-Member -Type NoteProperty -Name Generation -Value $VM.Generation;
$VMCustom | Add-Member -Type NoteProperty -Name SwitchName -Value $VM.SwitchName;
$Data += $VMCustom;
}
$Data | Export-CSV "C:\VM.csv" -Delimiter ";";
Question: Is the Ipaddress, the IPaddress of the VM or the IPaddress of the Hyper-V?
That would be great if someone could help me out.
Try this:
$Data = #()
$VMs = "Server-001","Server-002","Server-003"
foreach($VM in $VMs)
{
$VMInfo = Get-VM -Name $VM
$VMNetwork = $VMInfo | Get-VMNetworkAdapter
$VMCustom = New-Object System.Object
$VMCustom | Add-Member -Type NoteProperty -Name VMName -Value $VMInfo.VMName
$VMCustom | Add-Member -Type NoteProperty -Name Status -Value $VMInfo.Status
$VMCustom | Add-Member -Type NoteProperty -Name Generation -Value $VMInfo.Generation
$VMCustom | Add-Member -Type NoteProperty -Name IPAddress -Value $VMNetwork.IPAddresses[0]
$VMCustom | Add-Member -Type NoteProperty -Name MacAddress -Value $VMNetwork.MacAddress
$VMCustom | Add-Member -Type NoteProperty -Name SwitchName -Value $VMNetwork.SwitchName
$Data += $VMCustom
}
$Data | Export-CSV "C:\VM.csv" -Delimiter ";" -NoTypeInformation

Powershell : unable to add the members to collections

I face a problem in following test script.
I expect the following result as the output of it.
VM Name vCPU Memory
------- ---- ------
Server1 1
Server2 8
unfortunately, i am receiving the following output. Can someone help me to understand the mistake in the code?
enter code here
VM Name vCPU
------- ----
Server1 1
Server2
$obj_collection = #()
$report_data = New-Object System.Object
$report_data | Add-Member -MemberType NoteProperty -Name "VM Name" -Value "Server1"
$report_data | Add-Member -MemberType NoteProperty -Name "vCPU" -Value "1"
$obj_collection += $report_data
$report_data = New-Object System.Object
$report_data | Add-Member -MemberType NoteProperty -Name "VM Name" -Value "Server2"
$report_data | Add-Member -MemberType NoteProperty -Name "Memory" -Value "8"
$obj_collection += $report_data
$obj_collection
this? You need to add apple with apple because apple with potatoes can give strange behaviour
$obj_collection = #()
$report_data = New-Object System.Object
$report_data | Add-Member -MemberType NoteProperty -Name "VM Name" -Value "Server1"
$report_data | Add-Member -MemberType NoteProperty -Name "vCPU" -Value "1"
$report_data | Add-Member -MemberType NoteProperty -Name "Memory" -Value $null
$obj_collection += $report_data
$report_data = New-Object System.Object
$report_data | Add-Member -MemberType NoteProperty -Name "VM Name" -Value "Server2"
$report_data | Add-Member -MemberType NoteProperty -Name "vCPU" -Value $null
$report_data | Add-Member -MemberType NoteProperty -Name "Memory" -Value "8"
$obj_collection += $report_data
$obj_collection