尝试看看是否有更好的方法来重建对象

时间:2018-07-06 03:48:44

标签: arrays powershell object multidimensional-array hashtable

我知道做到这一点的唯一方法是添加大量代码来构建自定义对象,以将我的值放入并传递。我希望有一种更有效的方法并寻求建议。

我试图找出两个部分: 逻辑与以下相同,只是命令不同。

  1. 对象A-自称返回多个结果 Get-ADUser -resultsize 10命令。
    1a。为了完成第1部分,在对象A的foreach循环中。我想要:$MyObjA += Object A(单个),来自foreach循环。

  2. 增加复杂性(也许)
    2a。我想从Get-ADUser到(Object A)左连接(Object B)Get-Mailbox的输出,将其所有属性组合到Object A中并与Object A复制。因此,记录将显示Object A和Object B

现在我在上面的询问中只使用了Get-ADUserGet-Mailbox,因为这些是大家都知道的命令。话虽如此,如果我正在使用这些命令,那么我将不需要做的事情。因此,请引导我了解处理对象的最佳方法。

下面的实际问题是https://enoten.com/与UDM Pro一起使用。

他们的对象并不像我想要的那样友好,但是它就在那里。我的最终目标是建立功能来分解网站,直到这些功能可以告诉我可用的号码以及分配给谁的号码为止。我在那儿。我真的希望我不必构建自定义对象,或者至少不用我知道的方式。
每个功能都基于先前的功能。 我当前的问题是在Get-UDMRange_DIDBlocks_withAvailableNumbers过程中,我尝试执行CustomObject += Object A,其中object A是组的单个对象。我正在尝试建立同一对象的新组。

Function Get-UDMLocationNumberBlock_ID
{
    Param ($SiteCode)
    $Return = ''
    #Get-UdmLocation | Where {$_.name -like '*012a*'}
    #$Location = Get-UdmLocation | Where {$_.name -like "*$Sitecode*"}

    $Location = Get-UdmLocation | Where {$_.code -like "*$Sitecode*"}
    $Return = $Location.id

    Return $Return
}
Function Get-UDMLocationNumberBlock_NAME
{
    Param ($SiteCode)
    $Return = ''
    #Get-UdmLocation | Where {$_.name -like '*012a*'}
    $Location = Get-UdmLocation | Where {$_.name -like "*$Sitecode*"}

    #$Location = Get-UdmLocation | Where {$_.code -like "*$Sitecode*"}
    $Return = $Location.Name

    Return $Return
}
Function Get-UDMRange_DIDBlocks_byName
{
    Param ($SiteCode)

    $Return = ''
    $Return = Get-UdmRmNumberBlock | where {$_.rangeName -like "*$Sitecode*"}

    Return $Return
}
Function Get-UDMRange_DIDBlocks_byBlockNumber
{
    Param ($DIDBlockNumber)

    $Return = ''
    $Return = Get-UdmRmNumberBlock | where {$_.phoneNumber -like "*$DIDBlockNumber*"}

    Return $Return
}
Function Get-UDMRange_DIDBlocks_withAvailableNumbers
{
    Param ($SiteCode)
    $DIDBlocks = Get-UDMRange_DIDBlocks_byName -SiteCode $SiteCode
    $AvailDIDBlocks = ''

    foreach ($DIDBlock in $DIDBlocks)
    {
        if ($DIDBlock.usedQuantity -ne $DIDBlock.Quantity)
        {
                $AvailDIDBlocks += $DIDBlock
        }
    }
    Return $AvailDIDBlocks
}
Function Get-UDMRangesNumbers_bySite
{
    param ($Sitecode,[bool] $OnlyDIDBlocksWithAvailableNumbersInThem = $true)

    if ($OnlyDIDBlocksWithAvailableNumbersInThem)
    { #excludes DID where the block of numbers are full
        $AvailDIDBlocks = Get-UDMRange_DIDBlocks_withAvailableNumbers -SiteCode $Sitecode
    }
    else
    { #includes DID where the block of numbers are full
        $AvailDIDBlocks = Get-UDMRange_DIDBlocks -SiteCode $Sitecode
    }

    foreach ($AvailDIDBlock in $AvailDIDBlocks)
    {
        $DIDNumbers = Get-UdmRmNumber | where {$_.phoneNumberBlockId -eq $AvailDIDBlock.id}

        foreach ($DIDNumber in $DIDNumbers)
        {
            $DIDNumbersfromDIDBlocks += $DIDNumber
        }
    }
    Return $DIDNumbersfromDIDBlocks
}
Function Get-UDMRangesNumbers_bySpecificDID
{
    param ($DIDBlockNumber)

    $AvailDIDBlocks = Get-UDMRange_DIDBlocks_byBlockNumber -DIDBlockNumber $DIDBlockNumber

    foreach ($AvailDIDBlock in $AvailDIDBlocks)
    {
        $DIDNumbers = Get-UdmRmNumber | where {$_.phoneNumberBlockId -eq $AvailDIDBlock.id}

        foreach ($DIDNumber in $DIDNumbers)
        {
            $DIDNumbersfromDIDBlocks += $DIDNumber
        }
        #$DIDNumbersfromDIDBlocks += Get-UdmRmNumber | where {$_.phoneNumberBlockId -eq $AvailDIDBlock.id}
    }
    Return $DIDNumbersfromDIDBlocks
}

2 个答案:

答案 0 :(得分:1)

Stephane van Gulick在http://powershelldistrict.com/how-to-combine-powershell-objects/创建的功能可能会对您有所帮助。

该功能的基本要素是:

Function Combine-Objects {
    Param (
        [Parameter(mandatory=$true)]$Object1, 
        [Parameter(mandatory=$true)]$Object2
    )
    $arguments = [Pscustomobject]@()
    foreach ( $Property in $Object1.psobject.Properties) {
        $arguments += @{$Property.Name = $Property.value}  
    }
    foreach ( $Property in $Object2.psobject.Properties) {
        $arguments += @{ $Property.Name= $Property.value}
    }
    $Object3 = [Pscustomobject]$arguments
    return $Object3
}

答案 1 :(得分:0)

如果我正确理解了您的问题,则可以执行以下操作。
我首先从数字的哈希图开始,最后得到一个用某种方式处理过的数字重建的对象。

@{a=104; b=307; c=10; d=12; e=53}.GetEnumerator() `
| &{
    Process{
        $args[0][$_.Key]="$($_.Value)$(@{1='st';2='nd';3='rd'}[$_.Value] -replace '^$','th')";
    }
    End{$args[0]}
} @{}

输出:

Name                           Value
----                           -----
c                              10th
e                              53rd
d                              12nd
b                              307th
a                              104th

因此,对于您的脚本,我从Get-UDMRangesNumbers_bySite开始,然后设法解决了。
它从49行下降到32行(第三),而那些巨大的变量名则少得多。

#Get-UDMRangesNumbers_bySite
$SiteCode `
| &{
    Param ([bool]$OnlyDIDBlocksWithAvailableNumbersInThem=$true)
    Process {
        if (!$OnlyDIDBlocksWithAvailableNumbersInThem) {
            #you didn't give us code for this
            Get-UDMRange_DIDBlocks -SiteCode $_
            return
        }

        #Get-UDMRange_DIDBlocks_byName
        #you didn't give us code for this one either
        Get-UdmRmNumberBlock `
        | ?{ $_.rangeName -like "*$Sitecode*" } `
        | ?{ $_.usedQuantity -ne $_.Quantity } `
        | &{ #Get-UDMRange_DIDBlocks_withAvailableNumbers
#I'm not sure your function is working, this will output a single string
#you may have meant `@()`, and in that case you can just delete this block entirely
            Begin { $AvailDIDBlocks='' }
            Process { $AvailDIDBlocks += $_ }
            End { $AvailDIDBlocks }
        }
    }
} -OnlyDIDBlocksWithAvailableNumbersInThem=<#?#> `
| %{
    $AvailDIDBlock=$_
    #you didn't give us this function
    Get-UdmRmNumber | ?{$_.phoneNumberBlockId -eq $AvailDIDBlock.id}
} `
| %{
    #this variable didn't exist so I assume the purpose is to add to it from in here
    $DIDNumbersfromDIDBlocks += $DIDNumber
}

流真的很棒而且干净(出于脚本目的)。免费返回,免费连接,免费循环,无嵌套地狱以及易于理解的“堆栈”(数据自上而下;但是您的函数相互潜入,弹出,潜入)。
PowerShell比人们意识到的功能强大得多,人们没有利用所有非过程能力。

即使您需要在其他地方重用这些块,也可以将它们拉出,而不是像我已经完成的那样使它们内联并使它们起作用,并且仍然可以很好地传递给它们。