在通过活动目录查询'-'字符之前删除



输入:

John.Dow-GStephanie.adam-pPaul.Douglas-Steve-Svr-Zjane.young-ac-a

输出:

-G-p-svr-Z-ac-a

我的查询是固定的,但是如何使变量在上面的powerShell中提到的" - " powershell中的" - "中删除字符?

Get-ADUser -Identity john.dow-g -Server 'domaincontroller.com' -Properties * |
    select {n="suffix";e={$_.SamAccountName.Substring(9)}}
get-aduser -identity john.dow-g -server 'domaincontroller.com' -properties *| 
    Foreach-object{ 
       $Array = ($_.split('-'))
       '-' + (($Array[1..($Array.count-1)]) -join '-')
    }

您将继续遇到3个示例。

我认为您缺少有关该问题的信息,您要么有一个模板可以比较,即。@(' - abc-a', - ABC-A(,或者您检查最后2' - '以及中间的内容。

我将在第二个扩展:

管道:

'paul.douglas-steve-svr-z' | % {$_.Substring($_.Substring(0,$_.LastIndexOf('-')).lastIndexOf('-'),$_.Length-($_.Substring(0,$_.LastIndexOf('-')).lastIndexOf('-')))}

您可以看到少数,并且可以将其扩展到尽可能多的" - "。

更好的方法(IMO(:

$text='paul.douglas-steve-svr-z'
if($text.Split('-').Count -ge 3){
    $lastSymbolPosition = $text.LastIndexOf('-')
    $secondLastSymbilPosition = $text.Substring(0,$lastSymbolPosition).LastIndexOf('-')
    $prefix=$text.Substring($secondLastSymbilPosition,$text.Length-$secondLastSymbilPosition)
    $prefix
}else{
    'Out of the solution scope'
}

尽管我无法给您一个确定的答案,因为我不知道此文本处理的目的。

希望这会有所帮助。

最新更新