Powershell:过滤平面阵列 [英] Powershell: Filtering flat arrays

查看:92
本文介绍了Powershell:过滤平面阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究PS脚本,以使我们的某些过程更轻松,更自动化,并且我很快发现自己超出了我(有限的)PowerShell知识.

I'm working on a PS script for making some of our processes easier and more automated and I've quickly found myself well outside my (limited) PowerShell knowledge.

上下文:我正在构建一个UI,该UI具有两个ComboBox控件和一些用于创建资源日历的文本框.第一个ComboBox在我的组织中有3个子部门的静态列表.第二个组合框具有该部门中办公室的列表,当在第一个框中进行选择时,该列表应自动更新.

Context: I'm building a UI that has two ComboBox controls and a handful of text boxes for creating resource calendars. The first ComboBox has a static list of 3 sub-divisions in my organization. The second ComboBox has a list of the offices in that division which should automatically update when a selection is made in the first box.

最终目标是生成使用必需的统一命名约定创建邮箱所必需的Exchange命令行管理程序代码:

The end goal is to generate the Exchange Management Shell code necessary to create mailboxes with the necessary uniform naming convention:

New-Mailbox -Name 'Acme Inc $division $office $subdivision Resource Name' -Alias 'resourcename'-OrganizationalUnit '<path/to/OU>' -UserPrincipalName 'resourcename@acmeinc.lcl' -SamAccountName 'resourcename' 

现在我正在考虑将它们构建在一个具有三个字段的数组中,这些字段分别对应于部门,细分部门和办公室:

Right now I'm thinking I'll build these off of a single array which has three fields that correspond to division, sub-division, and office:

注:部门1没有细分,而部门2有两个细分.组合框1应该列出部门1,部门1,部门2.

$arr_AgencyOffices = @(
     'division 1','division 1','Aberdeen'
     'division 1','division 1','Perth'
     'division 1','division 1','Sacramento'
     'division 1','division 1','Long Beach'
     'division 1','division 1','New York'
     'division 1','division 1','Dallas'
     'division 1','division 1','Miami'
     'division 1','division 1','Vancouver'
     'division 2','subdivision 1','Sacramento'
     'division 2','subdivision 1','Tumwater'
     'division 2','subdivision 1','Vancouver'
     'division 2','subdivision 2','Aberdeen'
     'division 2','subdivision 2','Centralia'
     'division 2','subdivision 2','Sacramento'
     'division 2','subdivision 2','Long Beach'
     'division 2','subdivision 2','Shelton'
     'division 2','subdivision 2','Dallas'
     'division 2','subdivision 2','Stevenson'
     'division 2','subdivision 2','Miami'
     'division 2','subdivision 2','Vancouver'
)

特定问题:在给定特定cbo1.SelectedItem的情况下,如何仅将匹配的办公室位置值返回到$ arr_Offices中?

Specific Question: How do I return only the matching office location values into $arr_Offices given any specific cbo1.SelectedItem?

推荐答案

示例中显示的不是多维数组,而是常规的平面数组.

What you show in your example is not a multi-dimensional array, it's just a regular flat array.

要获取多维(或锯齿状)数组,请执行以下操作:

To get a multi-dimensional (or rather, a jagged) array, do:

$arr_AgencyOffices = @(
    @('division 1','division 1','Aberdeen'),
    @('division 1','division 1','Perth'),
    @('division 1','division 1','Sacramento'),
    @('division 1','division 1','Long Beach'),
    @('division 1','division 1','New York'),
    @('division 1','division 1','Dallas'),
    @('division 1','division 1','Miami'),
    @('division 1','division 1','Vancouver'),
    @('division 2','subdivision 1','Sacramento'),
    @('division 2','subdivision 1','Tumwater'),
    @('division 2','subdivision 1','Vancouver'),
    @('division 2','subdivision 2','Aberdeen'),
    @('division 2','subdivision 2','Centralia'),
    @('division 2','subdivision 2','Sacramento'),
    @('division 2','subdivision 2','Long Beach'),
    @('division 2','subdivision 2','Shelton'),
    @('division 2','subdivision 2','Dallas'),
    @('division 2','subdivision 2','Stevenson'),
    @('division 2','subdivision 2','Miami'),
    @('division 2','subdivision 2','Vancouver')
)

现在,您可以在两个维度上索引值:

Now you can index into the value on two dimensions:

$arr_AgencyOffices[4][2] # New York


话虽这么说,我可能会把办公室安排到一组嵌套的哈希表中,就像这样:


That being said, I would probably arrange the offices into a set of nested hashtables, like so:

$AgencyOffices = @{
    'division 1' = @{
        'division 1' = @(
            'Aberdeen'
            'Perth'
            'Sacramento'
            'Long Beach'
            'New York'
            'Dallas'
            'Miami'
            'Vancouver'
        )
    }
    'division 2' = @{
        'subdivision 1' = @(
            'Sacramento'
            'Tumwater'
            'Vancouver'
        )
        'subdivision 2' = @(
            'Aberdeen'
            'Centralia'
            'Sacramento'
            'Long Beach'
            'Shelton'
            'Dallas'
            'Stevenson'
            'Miami'
            'Vancouver'
        )
    }
}

现在,您可以使用其实际名称来索引细分数组:

Now you can index into the subdivision arrays using their actual names:

PS C:\> $AgencyOffices['division 2']['subdivision 1']
Sacramento
Tumwater
Vancouver

鉴于您只需采用先前选择的选定文本来查找下一个列表,就可以轻松地为组合框检索正确的列表.

This makes it trivial to retrieve the correct lists for the comboboxes given that you can just take the selected text of the preceding choice to find the next list:

$combobox2List = $AgencyOffices[$combobox1.SelectedText].Keys

以此类推

这篇关于Powershell:过滤平面阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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