簡體   English   中英

Powershell - 如何從用戶所在的組列表中刪除尾隨空格

[英]Powershell - How to remove trailing spaces from a list of groups a user is in

我已經復制/創建了一個腳本來獲取用戶所屬的組的所有成員,包括嵌套組。 但是,我的輸出並不完全是我想要的方式。

它以兩種方式之一進行。 要么輸出為一個看起來不錯的大字符串,但每行都有尾隨空格,因此我不能簡單地將其復制並粘貼到 AD 中。 或者,如果我將 Out-String 更改為使用 -stream,它會顯示為亂碼,但可能允許我修剪空格。

我目前在一個簡單的 GUI 中將輸出輸入到 TextBox 中。

Function Get-ADUserNestedGroups {
Param
(
    [string]$DistinguishedName,
    [array]$Groups = @()
)

#Get the AD object, and get group membership.
$ADObject = Get-ADObject -Filter "DistinguishedName -eq '$DistinguishedName'" -Properties memberOf, DistinguishedName;

#If object exists.
If($ADObject)
{
    #Enummurate through each of the groups.
    Foreach($GroupDistinguishedName in $ADObject.memberOf)
    {
        #Get member of groups from the enummerated group.
        $CurrentGroup = Get-ADObject -Filter "DistinguishedName -eq '$GroupDistinguishedName'" -Properties memberOf, DistinguishedName;
   
        #Check if the group is already in the array.
        If(($Groups | Where-Object {$_.DistinguishedName -eq $GroupDistinguishedName}).Count -eq 0)
        {
            #Add group to array.
            $Groups +=  $CurrentGroup;

            #Get recursive groups.      
            $Groups = Get-ADUserNestedGroups -DistinguishedName $GroupDistinguishedName -Groups $Groups;
        }
    }
}

#Return groups.
Return $Groups;
}

 Function Display-UserGroups {

#Get all groups.
$Groups = Get-ADUserNestedGroups -DistinguishedName (Get-ADUser -Identity $userSAM).DistinguishedName;

$ResultsTextBox.Text = $Groups | Select-Object Name| Sort-Object name | Out-String

第一種方式的輸出如下所示:

組名1(這里有八個空格)

組名2(這里有八個空格)

第二種方式的輸出如下所示:

組名1組名2組名3

謝謝你的幫助!

您需要修剪輸出,這可以使用String.Trim方法輕松完成。 但是,它只能應用於字符串。 $Groups將是ADObject類型的數組。 您將需要返回這些對象的Name值並將Trim()方法應用於這些值。

($Groups | Select -Expand Name | Sort).Trim() -join "`r`n"

您可以使用$Groups += $CurrentGroup.trimEnd()添加刪除尾隨空格的值。

您可能想研究的其他一些方法

.trim()      # Trim leading and trailing
.trimEnd()   # Trim trailing
.trimStart() # Trim leading

填充是由 PowerShell 的格式化系統引起的。 Select-Object正在返回單個屬性(名稱)對象。 PowerShell 將其輸出為表格,其中可能有一些填充。 Out-String保留了填充,這就是為什么它會反映在您的最終輸出中...

該組的名稱屬性已經是一個字符串。 如果您從$Groups變量/集合中展開 Name 屬性,則無需使用Out-String

對可讀性和測試進行了一些其他調整。 下面將發出一個字符串,每個組在一個新行上。 那應該可以粘貼到 AD 中,我認為您的意思是 Active Directory 用戶和計算機。

Function Get-ADUserNestedGroups
{
    Param
    (
        [string]$DistinguishedName,
        [array]$Groups = @()
    )
    
    # Get the AD object, and get group membership.
    $ADObject = Get-ADObject $DistinguishedName -Properties memberOf, DistinguishedName
    
    # If object exists.
    If( $ADObject )
    {
        # Enummurate through each of the groups.
        Foreach( $GroupDistinguishedName in $ADObject.memberOf )
        {
            # Get member of groups from the enummerated group.
            $CurrentGroup = Get-ADObject $GroupDistinguishedName -Properties memberOf, DistinguishedName
       
            # Check if the group is already in the array.
            If( $Groups.DistinguishedName -notcontains $GroupDistinguishedName )
            {
                # Add group to array.
                $Groups +=  $CurrentGroup
    
                # Get recursive groups.      
                $Groups = Get-ADUserNestedGroups -DistinguishedName $GroupDistinguishedName -Groups $Groups
            }
        }
    }
    
    # Return groups.
    Return $Groups
    }
    
$userSAM = 'UserName'

# Get all groups.
$Groups = Get-ADUserNestedGroups -DistinguishedName (Get-ADUser -Identity $userSAM).DistinguishedName
($Groups.Name | Sort-Object) -join [System.Environment]::NewLine

在您將看到的次要更改中,我刪除了幾個地方的-Filter參數。 DistinguishedName 可用作-Identity參數,因此無需過濾。

此外,在您檢查$Groups集合是否已經具有當前組的地方,我使用了-notContains運算符。 這應該比使用|Where{...}重復迭代集合更快,代碼也更短,更易讀。

注意:如果我們反轉操作數,我們可以使用-notin ,它可能看起來更好

一邊; 您應該避免使用+=附加數組。 這樣做會導致性能問題,因為它會創建一個新數組,任何復制內容的數組。 處理此問題的最佳方法是讓 PowerShell 為您累積結果。 如果您必須直接進行追加,請考慮使用數組列表。 這方面的資料很多,自己google一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM