2017-08-15 40 views
1

我有一個GUI接受來自用戶的幾個DN。我知道如何獲得文本,但我不能在我的生活中弄清楚如何將這個DN分成它的各個部分。零件的數量可能會有所不同。例如:PowerShell:將用戶定義的專有名稱拆分爲單獨的部分

$string1 = "ou=this,ou=is,dc=a,dc=string" 
$string2 = "ou=this,ou=is,ou=another,dc=dn,dc=string 

我想要做的是將ou從dc中分離出來並創建ou,如果它們不存在的話。我只是無法弄清楚如何在OU的從DC的分離,使它們成爲獨立的字符串,這樣我會留下:

$newString1 = "ou=this,ou=is" 
$newString2 = "dc=a,dc=string" 
$newString3 = "this,ou=is,ou=another" 
$newString4 = "dc=dn,dc=string 

我知道我可以通過使用$string1.Split(",")分割字符串,但我在這一點上,如何將單個值存儲在一個新變量中。我對PowerShell相當陌生,所以任何建議都將不勝感激。

我知道我可以根據自己的信息創建ou,但是我需要將它們分開以用於代碼中要完成的其他工作。也很高興看到他們分開,以便我可以單獨抓住ou。

+0

您應該將這些附加註釋編輯到問題中,而不是添加註釋。另外,你是否正在使用兩個着名的名稱?或者有一個可變數字,比如從文件讀入?如果你有2個DN,通過'$ newString4'變量具有'$ newString1'就沒有問題,但如果你有20個或200個,則沒有那麼多。 – BACON

+0

我會得到兩個DN。一個用於組和一個用戶。我基本上讓用戶定義他們希望組和用戶位於Active Directory上的位置,然後讓我的腳本創建這些目錄。總而言之,我真的很想學習如何在Powershell中將這樣的字符串分解爲單獨的塊,因爲它將由用戶定義。 – Individual

回答

0

希望這對於所提問題有所幫助。這假定您想要將相對可分辨名稱存儲在兩個組中:在您遇到dc=之前以及在碰到(包括)您碰到dc=之後的那些。

$string1 = "ou=this,ou=is,dc=a,dc=string" 
$string2 = "ou=this,ou=is,ou=another,dc=dn,dc=string" 

foreach ($dn in $string1, $string2) 
{ 
    # Break the distinguished name up into a list of relative distinguished names 
    $rdnList = $dn -split ','; 
    $cnList = @(); 
    $nextIndex = 0; 

    while ($nextIndex -lt $rdnList.Length) 
    { 
     $rdn = $rdnList[$nextIndex]; 
     $name, $value = $rdn -split '='; 

     # Stop looping if we've hit a dc= RDN 
     if ($name -eq 'dc') 
     { 
      break; 
     } 

     $cnList += $rdn; 
     $nextIndex++; 
    } 
    # The remainder of the array is our RDN list 
    $dcList = $rdnList[$nextIndex..($rdnList.Length - 1)]; 

    # Reassemble both lists into strings 
    $cnText = $cnList -join ','; 
    $dcText = $dcList -join ','; 

    # The data has now been processed; print it out  
    Write-Host "`$dn: $dn"; 
    Write-Host "`$cnText: $cnText"; 
    Write-host "`$dcText: $dcText"; 
    Write-Host; 
} 

輸出是...

$dn: ou=this,ou=is,dc=a,dc=string 
$cnText: ou=this,ou=is 
$dcText: dc=a,dc=string 

$dn: ou=this,ou=is,ou=another,dc=dn,dc=string 
$cnText: ou=this,ou=is,ou=another 
$dcText: dc=dn,dc=string 
+0

謝謝,並感謝您的快速答覆!這是更難比我想象的要多 – Individual

+0

最好[避免'Write-Host'](http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/) – mklement0

+0

@ mklement0這只是診斷代碼,以便在分割前後顯示字符串。我不知道作者將如何處理這個問題,但是這就是他們將代碼放入其中以利用這些變量的地方。 – BACON

0

試戴分成數組,像這樣:

c:\>$string = "ou=this,ou=is,ou=another,dc=dn,dc=string" 
c:\>$array = $string.split(",") 
c:\>$array 
ou=this 
ou=is 
ou=another 
dc=dn 
dc=string 
c:\> 

與陣列就可以判斷元素的個數:

c:\>$array.Length 
5 
c:\> 

或循環處理它:

c:\> foreach ($i in $array) { $i } 
ou=this 
ou=is 
ou=another 
dc=dn 
dc=string 
c:\> 

顯然,該循環與輸出$ array變量沒有什麼不同,但應該提供深入的見解nto如何使用循環。

希望這會有所幫助。

+0

我得到了foreach循環;然而,我怎麼不能這樣做: foreach($ i $ array){if($ i -contains「ou」){Write-Host $ i}} 我仍然無法弄清楚與單獨的部分一起工作。我打算倒轉陣列,我知道該怎麼做,但無法弄清楚如何拔出並與個別大塊工作:(。 – Individual

1
# Define input strings. 
$strings = 'ou=this,ou=is,dc=a,dc=string', 
      'ou=this,ou=is,ou=another,dc=dn,dc=string' 

foreach ($string in $strings) { 

    # Split each string into the ou=... part and the dc=... part. 
    $ouPart, $dcPart = $string -split ',(?=dc=)', 2 

    # Now split each part into its constituent OU and DC names as arrays. 
    $ous = $ouPart -split '(?:^|,)ou=' -ne '' -replace '\\' 
    $dcs = $dcPart -split '(?:^|,)dc=' -ne '' -replace '\\' 

    # Output result. 
    [pscustomobject] @{ ouPart = $ouPart; dcPart = $dcPart; ous = $ous; dcs = $dcs} 

} 

上面收率:

ouPart     dcPart   ous     dcs   
------     ------   ---     ---   
ou=this,ou=is   dc=a,dc=string {this, is}   {a, string} 
ou=this,ou=is,ou=another dc=dn,dc=string {this, is, another} {dn, string} 

說明:

  • $string -split ',(?=dc=)', 2將輸入字符串拆分爲2部分,第一次出現子字符串,dc=。只有2個半部之間的,被去除,因爲(?=...) - 一(正)前向斷言 - 匹配封閉表達,但不捕獲它;在這種情況下,它確保後半部分仍以dc=開頭。

  • $ouPart, $dcPart = ...指定-split返回到單個變量的2元素數組的元素。

  • $ouPart -split '(?:^|,)ou='拆分ou=...,ou=...一半到嵌入式OU名稱的數組(分裂dc=...,dc=...半作品類似):

    • (?:^|,)ou=匹配ou=發生之前或者由字符串(^)或開始(| )a ,
      • 非捕獲子表達式((?:...))圍^|,確保了子表達式不包括在-split結果。
  • -ne ''過濾出從輸入字符串的開始發生的-split定界符表達導致空數組元素。

  • -replace '\\'去除每個名稱\情況下,因爲它們可能包含\轉義字符,如嵌入式,轉義爲\,以價值

  • [pscustomobject] @{ ... }(PSv3 +)從一個構建自定義對象散列表字面值(@{ .... })與提取的值作爲屬性。 請注意,只是不將對象分配給變量,它是隱式輸出,PowerShell的默認輸出格式會自動提供漂亮的表格表示。