Created Mixed Custom Objects

So I am haven’t worked with objects to terrible much I have searched around for a way to do this but can’t seem to find anything to terribly useful.
My problem is I want to create a custom object from Get-VM and grab the selected like Name, PowerState and ProvisionedSpaceGB properties. Then use Get-ADUser to grab a few select properties like SamAccountName, DisplayName, and Enabled…

I am trying to create one object that would contain all the infomation and format it like the following:

VirtualMachine PowerState ProvisionedSpace DisplayName SamAccountName AccountEnabled


VM1 PoweredOff 103.03 name1 user1 false

and so on…
I am able to create two separate objects but my trouble is with combining them to create one obj that can easily be exported to a csv file or other format. If any one would like to see what I have so far I will gladly post my code. And if anyone has any ideas one how to perform the above any help is greatly appreciated. Thanks in advance.

Hey there Kaleb,

You can leverage PSCustomObject to do exactly what you’re looking to do. I did a write up a couple of days ago on how to use them.

Let me know if that helps. If not, post up your code and we’ll take a look!

Hey Keleb,

Yes, can you post your code? Would like to see how you are linking the sets of data together. Provided you have some linking element, this should certainly be possible within some type of looping structure.

Will, Your post helped me understand a few things about custom objects and the syntax. So I took a different approach, though I haven’t got to work on it much here is my original code.

It did not attach right sorry.

How does a user relate to a vm?

So what you’ll need to do in this case is find a common piece of information between the two to order it. For example, perhaps the last logged on user for your target machine. Try this:

[cmdletBinding()]
Param([Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String]$VM,

  [Parameter(Mandatory=$true, 
            Position=1,
             ValueFromPipeline=$true,
             ValueFromPipelineByPropertyName=$true)]
            [Microsoft.ActiveDirectory.Management.ADUser[]]$User

)

ForEach($System in $VM){
$VMInfo = Get-VM $System -ErrorAction SilentlyContinue -ErrorVariable objError | Select-Object Name, PowerState, ProvisionedSpaceGB
$LastLoggedUser = Get-ItemProperty -Path ‘HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon’
$GetUserInfo = Get-ADUser -Identity $LastLoggedUser.LastUsedUsername

[PSCustomObject]@{
Name             = $VMInfo.Name
PowerState       = $VMInfo.PowerState
ProvisionedSpace = $VMInfo.ProvisionedSpaceGB
DisplayName    = $GetUserInfo.DisplayName
SamAccountName = $GetUserInfo.SamAccountName
AccountEnabled = $GetUserInfo.Enabled
}

}
Scratch that. I forgot to update my code to pull remote registry. One moment.

Note, I wasn’t able to test the VM portion of it. I don’t currently have access to leverage the Hyper-V cmdlets.

I’ve updated my code with getting remote registry, but it appears to only work on Workstation OS. I’m still looking for something more universal.

[cmdletBinding()]
Param([Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String]$VM,

  [Parameter(Mandatory=$true, 
            Position=1,
             ValueFromPipeline=$true,
             ValueFromPipelineByPropertyName=$true)]
            [Microsoft.ActiveDirectory.Management.ADUser[]]$User

)

ForEach($System in $VM){
$VMInfo = Get-VM $System -ErrorAction SilentlyContinue -ErrorVariable objError | Select-Object Name, PowerState, ProvisionedSpaceGB
$LastLoggedUser = Invoke-Command -ComputerName $VMInfo.Name -Scriptblock {Get-ItemProperty -Path ‘HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon’}
$GetUserInfo = Get-ADUser -Identity $LastLoggedUser.LastUsedUsername

[PSCustomObject]@{
Name             = $VMInfo.Name
PowerState       = $VMInfo.PowerState
ProvisionedSpace = $VMInfo.ProvisionedSpaceGB
DisplayName    = $GetUserInfo.DisplayName
SamAccountName = $GetUserInfo.SamAccountName
AccountEnabled = $GetUserInfo.Enabled
}

}