Hashtable empty only in use of Powershell Studio 2022

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
Tim Lilienthal
Posts: 3
Last visit: Thu Feb 22, 2024 6:31 am
Has voted: 1 time

Hashtable empty only in use of Powershell Studio 2022

Post by Tim Lilienthal »

Hello guys,

I'd like to compare the OU-Path of an AD-User and create an autoselection of the correct OU in an combobox.
My idea is to write the code as a function where it searchs for all organizationalUnits and put them into a hashtable.
The Script I wrote for it was tested in PowerShell ISE and it worked perfectly, but copying it to the PowerShell Studio drives me crazy.
Everytime when it comes to write data into the hashtable it doesn't work.
When I want to see an output - it's empty.
Code: [Select all] [Expand/Collapse] [Download] (Hashtable_empty.ps1)
  1.    
  2. $OUPfadeStandortearray = $null
  3.     $Script:OUHash = $Null
  4.    
  5.     $OUPfadeStandorte = Get-ADObject -Filter 'ObjectClass -eq "organizationalUnit" -and name -like "*Mitarbeiter*"' | sort name
  6.  
  7.     foreach ($_ in $OUPfadeStandorte)
  8.     {
  9.         $OUPfadeStandorteAbfrage = Get-ADObject -Filter 'ObjectClass -eq "organizationalUnit"' -SearchBase $_
  10.         $OUPfadeStandortearray += $OUPfadeStandorteAbfrage
  11.     }
  12.        
  13.     foreach ($_ in $OUPfadeStandortearray)
  14.     {
  15.         $Name = $_.name | Out-String
  16.         $OU = $_.DistinguishedName | Out-String
  17.  
  18.         ##############this does not work in PowerShell Studio#####################
  19.         $Script:OUHash += @{ $_.name = $_.DistinguishedName }
  20.         ###############################################################
  21.  
  22.     }
  23.     $Script:OUHash
I think it could be my fault to handle the hashtables in PowerShell Studio wrong, but I can't figure out what the problem is...

Thanks for your help! :)
by jvierra » Tue Aug 23, 2022 12:40 am
PSS does not execute code. All code is executed by the PS engine. Perhaps you are not using the same engine.

The use of "$_" as an enumerator variable will not work in all things. It is a reserved variable and cannot be assigned. Change it to a real variable.

I see no reason to create a hash table. The result from the Get-ADObject is an object collection which is better and more flexible than a hash.

There are numerous errors in your code besides using "$_" . The return from Get-AdObject is not an object path or an OU path. You need to spend some time thinking through what you are trying to accomplish. Your code doesn't make any sense

To assign a hash from a variable you would need to use a variable and not a protected pipeline variable. I doubt that this works in ISE as it is illegal PS code.

You cannot use a object property as a string identifier in a hash. This is how to do it.

$var = @{"$($obj.propname)" = $othervar}

You can skip the quotes, but they ensure that it is a string and not an object or a number. Learning how PowerShell uses protected variable especially pipeline variables will help you understand why things like this misbehave.
Go to full post
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Hashtable empty only in use of Powershell Studio 2022

Post by jvierra »

PSS does not execute code. All code is executed by the PS engine. Perhaps you are not using the same engine.

The use of "$_" as an enumerator variable will not work in all things. It is a reserved variable and cannot be assigned. Change it to a real variable.

I see no reason to create a hash table. The result from the Get-ADObject is an object collection which is better and more flexible than a hash.

There are numerous errors in your code besides using "$_" . The return from Get-AdObject is not an object path or an OU path. You need to spend some time thinking through what you are trying to accomplish. Your code doesn't make any sense

To assign a hash from a variable you would need to use a variable and not a protected pipeline variable. I doubt that this works in ISE as it is illegal PS code.

You cannot use a object property as a string identifier in a hash. This is how to do it.

$var = @{"$($obj.propname)" = $othervar}

You can skip the quotes, but they ensure that it is a string and not an object or a number. Learning how PowerShell uses protected variable especially pipeline variables will help you understand why things like this misbehave.
Tim Lilienthal
Posts: 3
Last visit: Thu Feb 22, 2024 6:31 am
Has voted: 1 time

Re: Hashtable empty only in use of Powershell Studio 2022

Post by Tim Lilienthal »

Thank you very much for your response!
I'll change a few things.
I don't want to be smartass, just to prove why I thought it could work like this: (this is what ISE gives back - Same script as on last post)
Name Value
---- -----
Mitarbeiter-TP --------------- OU=Mitarbeiter-TP,OU=*********,OU=Standorte,DC=brb,DC=local
Abt. Buchhaltung --------------- OU=Abt. Buchhaltung,OU=Mitarbeiter,OU=*********,DC=brb,DC=local
Mitarbeiter-KN --------------- OU=Mitarbeiter-KN,OU=*********,OU=Standorte,DC=brb,DC=local

I think I was too busy trying to use hashtable instead of thinking of other solutions.
I have to confess that I've tried so much, that the code you've seen is not that what it should be :D

But thanks!
I'll try to fix that and post my final results here.
Tim Lilienthal
Posts: 3
Last visit: Thu Feb 22, 2024 6:31 am
Has voted: 1 time

Re: Hashtable empty only in use of Powershell Studio 2022

Post by Tim Lilienthal »

I think I undertstood what you meand. I'm pretty new to Scripting.
Just did simple request and 1 liners alot. Just started 1-2 weeks ago to work alot more with whole scripts. So need to learn alot :D

This is my final result:
  1. Get-ADObject -filter ('name -like "*mitarbeiter*" -and ObjectClass -eq "organizationalUnit"') | ForEach-Object {$OU += Get-ADObject -Filter 'ObjectClass -eq "organizationalUnit"' -SearchBase $_}
PS: The loop in the script is because there is a OU named "Mitarbeiter" and holds Sub-OUs that I need.
And I think I'll fill the Combobox with $OUs.name so that even non-IT-guys can read it.
Thanks :) for your help!
This topic is 1 year and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked