Adding a .Net object to a PSObject

by dunagh at 2013-01-10 16:28:20

Is it possible, and if so how does one, add a .Net object to to a PSObject. Specifically, I am trying to add a [Microsoft.Windows.Win32.RegistryKey] to a custome PSObject. I am finding that by doing so, it is converting it to as string because I am incorrectly adding it as a NoteProperty. The rundown of what I am trying to do is this:

$usersObject = @()
$users = @("Sam Jones", "Frank Barber", "Sally Mae")

$computer = "SystemX"

foreach($user in $users)
{
$userObject = New-Object PSObject
$userObject | Add-Member NoteProperty "UserNameName" $user
$userObject | Add-Member NoteProperty "LoginID" $user.Replace(" ", "")
$userObject | Add-Member NoteProperty "SID" "S-1-22-15-123412351-14513451-234562345"
$userObject | Add-Member NoteProperty "UserReg" [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', $computer)
$usersObject += $userObject
}

$computerObject = New-Object PSObject
$computerObject | Add-Member NoteProperty "ComputerName" "SystemX"
$computerObject | Add-Member NoteProperty "Users" $usersObject

Write-Output $computerObject

… and I want $userObject.UserReg to contain a valid Microsoft.Win32.RegistryKey object.

I am also afraid I am adding the $usersObject incorrectly to the $computerObject. Any help is appricated.

Thanks.
by nohandle at 2013-01-11 02:56:52
If you add the correct parentheses in the Add-Member NoteProperty "UserReg" clause it seems to work and produce what you need. (I used another static function from the same class to make the exaple work without using remote registry edit> on the second thought I could have used localhost.:slight_smile: )
$usersObject = @()
$users = @("Sam Jones", "Frank Barber", "Sally Mae")

$computer = "SystemX"

foreach($user in $users)
{
$userObject = New-Object PSObject
$userObject | Add-Member NoteProperty "UserNameName" $user
$userObject | Add-Member NoteProperty "LoginID" $user.Replace(" ", "")
$userObject | Add-Member NoteProperty "SID" "S-1-22-15-123412351-14513451-234562345"
$userObject | Add-Member NoteProperty "UserReg" ([Microsoft.Win32.RegistryKey]]]::Default))
$usersObject += $userObject
}

$user = $usersObject[0]
$user.UserReg.GetType().FullName
$user | Get-Member userreg

Microsoft.Win32.RegistryKey


TypeName: System.Management.Automation.PSCustomObject

Name MemberType Definition
---- ---------- ----------
UserReg NoteProperty Microsoft.Win32.RegistryKey UserReg=HKEY_CURRENT_USER


I also tried to just create an empty property, assign it a custom object and change to object to see if the changes propagate correctly and they did.
$base = New-Object -TypeName PSObject -Property @{reference = ''}
$property = New-Object -TypeName PsObject -Property @{name = 'john'}

#add the object to the reference property
$base.reference = $property
$base.reference.name

#change the original object
$property.name = 'Sarah'

#review the changes and type
$base.reference.name
$base.reference | gm
by dunagh at 2013-01-16 16:50:43
I had realized I was trying to unnecessarily cast the registry object, which I assign somewhere else in my script, as a Microsoft.Win32.RegistryKey object. When trying to cast the object while assigning it to the PSObject property, it saw it as a string. Thanks for the tip on putting parenthecies around an object though. I will keep that in mind if I need it in the future.