简体   繁体   English

Powershell将对象作为.replace的一部分传递

[英]Powershell passing object as part of .replace

I have a script that I am writing to return all of the users and their OUs that have mailboxes. 我有一个正在编写的脚本,用于返回所有具有邮箱的用户及其OU。 My only problems is that distinguishedname returns 我唯一的问题是专有名称返回

CN=user,OU=... CN =用户,OU = ...

I just want OU=... 我只想要OU = ...

It is my last line that is not formatted correctly 这是我的最后一行格式不正确

$($objItem.distinguishedname.replace('CN=$($objItem.name),',''))"

In this piece of the last line I want to replace where I find the phrase CN= data returned from the object with a blank. 在这最后一行中,我想替换为从对象返回的短语CN =数据的空白。

So if $(objItem.name) contained Bob 因此,如果$(objItem.name)包含Bob

I would want this to perform the following replace 我希望它执行以下替换

$objItem.distinguishedname.replace('CN=Bob,','')

How would I format this? 我该如何格式化? I have also tried the following 我也尝试了以下

$($objItem.distinguishedname[0].replace('CN=$($objItem.name),',''))

which doesn't return an error but replaces nothing. 这不会返回错误,但是什么也不会替代。 If I change it to 如果我将其更改为

$($objItem.distinguishedname[0].replace('CN=',''))

it removes all the CN= in front. 它会删除前面的所有CN =。 I feel close. 我感觉很亲密。

Below is the full script. 以下是完整的脚本。

$strFilter = "(&(&(&(objectCategory=Person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2)(HomeMDB=*)))))"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"

$colProplist = "name","distinguishedname", "samaccountname"

foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults)
{
    $objItem = $objResult.Properties; 
    write-Host "$($objItem.samaccountname) $($objItem.name)$($objItem.distinguishedname.replace('CN=$($objItem.name),',''))"
}

PowerShell doesn't expand variables inside of single quotes: PowerShell不会在单引号内扩展变量:

'CN=$($objItem.name),'

Perhaps you want this: 也许您想要这样:

"... $($objItem.distinguishedname[0].replace(`"CN=$($objItem.name),`",''))"

Here's an example: 这是一个例子:

$objItem = [pscustomobject]@{DistinguishedName='CN=Bob,OU=blah','';Name='Bob'}
"Blah ... $($objItem.distinguishedname[0].replace(`"CN=$($objItem.name),`",''))"

Outputs: 输出:

Blah ... OU=blah

You can try this: 您可以尝试以下方法:

$($objItem.distinguishedname[0] -creplace 'CN=[^,]+,')

Is this work for you: 这是为您工作吗?

....
...
$objItem = $objResult.Properties;
$del = 'CN={0},' -f $($objItem.name)
.......$($objItem.distinguishedname[0] -creplace "$del")

Rather than replace I'll offer Split as an alternative. 我将提供Split作为替代方法,而不是替代。 This will split on a comma and return a 2 item array, the first item being CN=Name and the second item is OU=Path. 这将以逗号分割并返回2项数组,第一项为CN = Name,第二项为OU = Path。 Then I pipe to Select and skip the first one, returning only the OU= portion of the string. 然后,我通过管道传递给Select并跳过第一个,仅返回字符串的OU =部分。

$objItem.distinguishedname -split ",",2|select -skip 1

Then you could use it something like: 然后,您可以使用类似:

Write-Host ("{0} {1}{2}" -f $objItem.samaccountname, $objItem.name, ($objItem.distinguishedname -split ",",2|select -skip 1))

That will give an output like you have in your example of their SAMAccountName, followed by a space, followed by their Name, and immediately following that will be their DistinguishedName with the CN=Name portion of it removed. 这将提供与示例中的SAMAccountName相同的输出,后跟一个空格,后跟它们的Name,紧随其后的是它们的DistinguishedName,其中删除了CN = Name部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM