在Powershell中延迟加载? [英] Lazy loading in powershell?

查看:570
本文介绍了在Powershell中延迟加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以将变量初始化推迟到需要吗?

我想做的是预定义我的配置文件中的一些变量,这些变量将包含AD计算机列表:

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

让我说:

$ OU1_workstation 填充在OU = workstations,OU = OU1,dc = domain,dc = com

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

$ OU2_workstation 填充在
中找到的计算机 OU =工作站,OU = OU2,dc =域,dc = com等...

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

我使用以下脚本来执行此操作,但是计算需要30秒,因此目前我无法将其保存在个人资料中...

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 * )
}    

powershell中有哪些可用选项?

What options are available in powershell ?

推荐答案

最后,基于 @Richard的回复我的上一个问题,我选择了以下路径来实现某种延迟加载:使用PSCustomObjectscriptproperty. 所以我可以把它放在我的个人资料中

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
}

然后在需要时我可以调用$server.OU1来获取此OU下的所有服务器,$server.OU2等...

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

这篇关于在Powershell中延迟加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆