Powershell正则表达式将从专有名称中获取DC

时间:2018-12-17 14:47:20

标签: regex powershell

我正在尝试为给定的专有名称显示第一个DC,如下所示:

CN=blah1,CN=Computers,DC=blah2,DC=blah3

因此,我希望用简单的英语“替换所有字符串,直至'DC =',并返回从DC=到下一个,的所有值

我已经尝试过使用在线计算器进行计算,但是某种程度上是行不通的。

4 个答案:

答案 0 :(得分:1)

看看这个:

$str = "CN=blah1,CN=Computers,DC=blah2,DC=blah3"
$str -match '^.*?(DC=.*?),'
$Matches[1] # DC=blah2 

它找到第一个DC=*,其中*=之后的字母,直到下一个逗号为止。

答案 1 :(得分:1)

介绍第二种简单的方法来grep字符串的某些部分。尝试使用- split

$string = "CN=blah1,CN=Computers,DC=blah2,DC=blah3"

#Seperate the string at ",DC=" and get the second part
($string -split ',DC=')[1]

返回:blah2

答案 2 :(得分:0)

我认为最好的答案(“最好”是指“最可靠的”)首先不是进行字符串解析或正则表达式,而是使用IADSPathname interface来检索名称的一部分对此感兴趣。它可以通过 Pathname COM对象在PowerShell中使用(尽管有些复杂)。 路径名对象会自动处理所有转义字符。

示例:

$ADS_SETTYPE_DN = 4
$ADS_DISPLAY_VALUE_ONLY = 2

$Pathname = New-Object -ComObject Pathname

$distinguishedName = "CN=blah1,CN=Computers,DC=blah2,DC=blah3"

# Set the AD path in the object
$Pathname.GetType().InvokeMember("Set", "InvokeMethod", $null, $Pathname, @($distinguishedName,$ADS_SETTYPE_DN))

# Get the number of name elements
$numElements = $Pathname.GetType().InvokeMember("GetNumElements", "InvokeMethod", $null, $Pathname, $null)

# Retrieve the second-to-last name element (outputs "DC=blah2")
$Pathname.GetType().InvokeMember("GetElement", "InvokeMethod", $null, $Pathname, $numElements - 2)

# Set the display type to values only
$Pathname.GetType().InvokeMember("SetDisplayType", "InvokeMethod", $null, $Pathname, $ADS_DISPLAY_VALUE_ONLY)

# Retrieve the second-to-last name element (outputs "blah2")
$Pathname.GetType().InvokeMember("GetElement", "InvokeMethod", $null, $Pathname, $numElements - 2)

诚然,路径名 COM对象在PowerShell中不容易使用,因为您必须间接调用它。通过使用“包装器”函数调用对象的方法,可以在某种程度上缓解这种情况。示例:

$ADS_SETTYPE_DN = 4
$ADS_DISPLAY_VALUE_ONLY = 2

$Pathname = New-Object -ComObject Pathname

function Invoke-Method {
  param(
    [__ComObject] $object,
    [String] $method,
    $parameters
  )
  $output = $object.GetType().InvokeMember($method, "InvokeMethod", $null, $object, $parameters)
  if ( $output ) { $output }
}

$distinguishedName = "CN=blah1,CN=Computers,DC=blah2,DC=blah3"

# Set the AD path in the object
Invoke-Method $Pathname "Set" @($distinguishedName,$ADS_SETTYPE_DN)

# Get the number of name elements
$numElements = Invoke-Method $Pathname "GetNumElements"

# Retrieve the second-to-last name element (outputs "DC=blah2")
Invoke-Method $Pathname "GetElement" ($numElements - 2)

# Set the display type to values only
Invoke-Method $Pathname "SetDisplayType" $ADS_DISPLAY_VALUE_ONLY

# Retrieve the second-to-last name element (outputs "blah2")
Invoke-Method $Pathname "GetElement" ($numElements - 2)

为获得完整的解决方案,我编写了一个名为ADName的PowerShell模块,该模块为路径名NameTranslate对象提供了易于使用的界面。

在ADName模块中,Get-ADName cmdlet是 Pathname 对象的包装,而Convert-ADName cmdlet是 NameTranslate 对象的包装。示例:

# Get elements of name as an array
$nameElements = Get-ADName "CN=blah1,CN=Computers,DC=blah2,DC=blah3" -Split
# Output second-to-last element (e.g., "DC=blah2")
$nameElements[-2]

# Get name elements (values only)
$nameElements = Get-ADName "CN=blah1,CN=Computers,DC=blah2,DC=blah3" -Split -ValuesOnly
# Output second-to-last element (e.g., "blah2")
$nameElements[-2]

我发现Get-ADName和Convert-ADName cmdlet在各种情况下都非常有用。一个例子:

$name = "CN=blah1,CN=Computers,DC=blah2,DC=blah3"
# Output canonical name of parent path; e.g.: "blah2.blah3/Computers"
$name | Get-ADName -Format Parent | Convert-ADName Canonical

答案 3 :(得分:-1)

Split就足够了,即:

$s = "CN=blah1,CN=Computers,DC=blah2,DC=blah3"
$s.Split(",")[2].Split("=")[1]
# blah2

Powershell Demo