0

Can we defer a variable initialization untill it is needed ?

What I would like to do is predefine some variables in my profile that will contain a list of AD computer:

let's say I want:

$OU1_workstation to be fill with computers found in OU=workstations,OU=OU1,dc=domain,dc=com

$OU2_workstation fill with computers found in
OU=workstations,OU=OU2,dc=domain,dc=com and so on...

I use the following script to do it but it takes 30sec to compute, so currently I can't put that in my profile...

Get-ADOrganizationalUnit -SearchScope onelevel -Filter "*" -Properties "name","distinguishedname" |%{
    set-Variable -Name "$($_.name)_workstation" -value (Get-ADComputer -Searchbase "OU=workstations,$($_.Distinguishedname)" -Filter * )
}    

What options are available in powershell ?

Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
  • 2
    `$Lazy=[Lazy[PSObject]]::new([Func[PSObject]]{dir})` – user4003407 Nov 17 '15 at 07:39
  • lol I think OP wants to optimize his command to make it faster – sodawillow Nov 17 '15 at 07:43
  • 1
    @sodawillow this is not about optimization, I think this is close to what I want : http://stackoverflow.com/a/14503339/381149 – Loïc MICHEL Nov 17 '15 at 08:50
  • _it takes 30sec to compute, so I can't put it in my profile_ I thought you wanted your script to run faster in order to put it in your profile. I don't think you need more than a (classic) variable to hold the results of your command, like `$myFavouriteComputers`. This variable will then be available each time you use PowerShell. – sodawillow Nov 17 '15 at 08:54
  • @sodawillow we are talking about hundreds of OUs and thousands of computername, no need to query all these objects unless they are effectively use. – Loïc MICHEL Nov 17 '15 at 09:00
  • _Can we defer a variable initialization untill it is needed ?_ I missed this, sorry – sodawillow Nov 17 '15 at 09:03
  • have you try using job? `start-job`, `get-job | receive-job` – CB. Nov 17 '15 at 11:47
  • @CB. no this is not the point, but I think your response here : http://stackoverflow.com/questions/14482274/how-to-create-a-dynamic-variable-in-powershell-sucha-as-date-time-etc/14503339#14503339 is a good candidate (I didn't have the time to test yet) – Loïc MICHEL Nov 17 '15 at 12:24
  • @Kayasax mmhh.. Using job your profile can continue w/o waiting 30 seconds. With a dynamic variable you have to wait each time you call it. – CB. Nov 17 '15 at 12:43
  • @CB. mmhh also :) ! using dynamic variable the code will not be run for all OUs, but likely for just one, so it should not be too long... I really have to try both suggestions ... – Loïc MICHEL Nov 17 '15 at 12:51
  • I'm happy to listen to you.. You and Paris have all my support ..for what it's worth! – CB. Nov 17 '15 at 12:59
  • @CB. thank you very much – Loïc MICHEL Nov 17 '15 at 13:01

1 Answers1

0

Finally, based on @Richard's reply of a previous question of mine, I've chosen the following path to achieve some sort of lazy loading : using a scriptproperty of a PSCustomObject. So I can put this in my profile

#requires -module activedirectory
$server=New-Object PSCustomObject
Get-ADOrganizationalUnit -SearchScope onelevel -Filter "*" -Properties "name","distinguishedname" | 
?{
    $_.name -notmatch 'Administrateurs|Administration|Comptes de deploiement|Contacts|Domain Controllers|Groupes|Serveurs|Services' 
} |
%{
    $OU=$_.name
    $s=[scriptblock]::Create("Get-ADComputer -SearchBase ""OU=servers,OU=$OU,DC=domain,DC=com"" -Filter 'name -notlike "" *old""' |select -expand name")
    $server| Add-Member -MemberType ScriptProperty -name $OU -value $s -Force
}

then when needed I can call $server.OU1 to get all server under this OU, $server.OU2 etc...

Community
  • 1
  • 1
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
  • IMHO, it is not really lazy loading. It evaluate `Get-ADComputer ...` command each time you ask for it. It is just different syntax to call function. – user4003407 Dec 04 '15 at 15:46
  • You are right, that's why I said a sort of lazy loading. But this is good for my daily needs :) and allow to define things without execute it. To avoid executing the code each time I can create another var : `$OU1_server=$server.OU1 ` ... – Loïc MICHEL Dec 04 '15 at 15:49