I was working on a script trying to output two different custom objects; one after another and was having problems, so to simplify, I stripped all the code down to just the minimum:
$beep = new-object -TypeName PSObject
$beep | Add-Member -MemberType NoteProperty -Name "Entry1" -Value "beep1"
$beep | Add-Member -MemberType NoteProperty -Name "Entry2" -Value "beep1"
$beep
$boop = new-object -TypeName PSObject
$boop | Add-Member -MemberType NoteProperty -Name "Entry1" -Value "boop1"
$boop | Add-Member -MemberType NoteProperty -Name "Entry2" -Value "boop1"
$boop
When I run this, it appears the objects are combined together. When I do a get-member, it seems to just show one object. Why?
In the code I am actually trying to finish, one object is a custom object like above, but the other it a selected.system.int32. When I try to output one after the other only the first one outputs. If I flip the order; same thing, the first object gets output. What am I doing wrong / don't understand?
@JamesQ - I guess what is confusing me is when I do:
$beep = new-object -TypeName PSObject
$beep | Add-Member -MemberType NoteProperty -Name "Entry1" -Value "beep1"
$beep | Add-Member -MemberType NoteProperty -Name "Entry2" -Value "beep1"
$beep | get-member
$boop = new-object -TypeName PSObject
$boop | Add-Member -MemberType NoteProperty -Name "Entry1" -Value "boop1"
$boop | Add-Member -MemberType NoteProperty -Name "Entry2" -Value "boop1"
$boop | get-member
I get:
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Entry1 NoteProperty System.String Entry1=beep1
Entry2 NoteProperty System.String Entry2=beep1
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Entry1 NoteProperty System.String Entry1=boop1
Entry2 NoteProperty System.String Entry2=boop1
I was trying to keep the question simple, but I think I've made it too ambiguous so...
I am trying to reuse some code:
function Get-NetworkConfig($computerName) {
Get-WmiObject Win32_NetworkAdapter -Filter 'NetConnectionStatus=2' |
ForEach-Object {
$result = 1 | Select-Object Name, IP, MAC
$result.Name = $_.Name
$result.MAC = $_.MacAddress
$config = $_.GetRelated('Win32_NetworkAdapterConfiguration')
$result.IP = $config | Select-Object -expand IPAddress
$result
}
}
then in the same script something like:
function Get-ComputerInfo($computerName) {
$operatingSystem = Get-WMIObject -computername $computerName win32_operatingsystem
$computerInfo = new-object -TypeName PSObject
$computerInfo | Add-Member -MemberType NoteProperty -Name "Computer Name" -Value ("$computerName")
$computerInfo | Add-Member -MemberType NoteProperty -Name "Operating System" -Value ("$operatingSystem.Caption")
$computerInfo
}
$computerNames = $args
foreach ($computerName in $computerNames) {
Get-ComputerInfo($computerName)
Get-NetworkConfig($computerName)
}
Only the output from get-computerinfo is displayed, then a bunch of blank lines where get-networkconfig would have output.
If I reverse the order; only the first function gives output, the second only spits blank lines.
Why can't I just call two functions in sequence like this?