Powershell如果不符合特定条件,则比较文本并替换

时间:2014-09-04 14:50:32

标签: powershell powershell-v3.0

以下是我导入的csv文件的示例。

CN,DistinguishedName,extensionattribute7,extensionattribute1
CNPTL73J79ZN1,"CN=CNPTL73J79ZN1,OU=Laptops,OU=Workstations,OU=MSP01,DC=ten,DC=domain,DC=com",tianyang.li,
USPTD079YZLN1,"CN=USPTD079YZLN1,OU=Desktops,OU=Workstations,OU=MSP01,DC=ten,DC=domain,DC=com",gary.ortiz,
USPTD07WM53M1,"CN=USPTD07WM53M1,OU=Desktops,OU=Workstations,OU=MSP01,DC=ten,DC=domain,DC=com",allen.watson,
USPTL7CC1P0P1,"CN=USPTL7CC1P0P1,OU=Laptops,OU=Workstations,OU=MSP01,DC=ten,DC=domain,DC=com",u0147066,
USPTL77BTZ4R1,"CN=USPTL77BTZ4R1,OU=Laptops,OU=Workstations,OU=MSP01,DC=ten,DC=domain,DC=com",U0172604,
U0165724-TPL-A,"CN=U0165724-TPL-A,OU=Laptops,OU=Workstations,OU=MSP01,DC=ten,DC=domain,DC=com",U0165724,167
U0130173-TPL-A,"CN=U0130173-TPL-A,OU=Laptops,OU=Workstations,OU=MSP01,DC=ten,DC=domain,DC=com",U0130173,167
U0068498-TPL-A,"CN=U0068498-TPL-A,OU=Laptops,OU=Workstations,OU=MSP01,DC=ten,DC=domain,DC=com",u0068498,167

我需要做的几件事: 检查CN的格式是否以UXXXXXXX开头 如果没有,请检查extensionattribute7以获取Uxxxxxxx的正确格式化用户ID 如果存在,请将CN替换为Uxxxxxxx-TPL-ZZZ的名称。 -TPL-ZZZ虽然不是所有名称都是一致的。

我完全很困惑如何搜索Uxxxxxxx,但我需要这样的东西,虽然我知道这是完全错误的。

Import-Csv c:\Temp\Windows7_Only.csv
    if ($_CN -NotMatch'[U][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
    {
    Replace the name if extensionattribute7 contains a value of U####### and add the suffix of -TPL-ZZZ
    }

到目前为止,这是我的脚本:

#Create an LDAP searcher object and pass in the DN of the domain we wish to query
$Searcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]"LDAP://DC=ten,DC=domain,DC=com")

#Pass in the ceriteria we are searching for.

$Searcher.Filter = "(&(objectCategory=computer)(objectClass=computer)(!UserAccountControl:1.2.840.113556.1.4.803:=2)(operatingSystem=Windows 7*))"

$Searcher.PageSize = 100000

# Populate General Sheet(1) with information

$results = $Searcher.Findall()

$results | ForEach-Object { $_.GetDirectoryEntry() } |
select @{ n = 'CN'; e = { $_.CN -replace "'", "''" } },
@{ n = 'DistinguishedName'; e = { $_.DistinguishedName -replace "'", "''" } },
@{ n = 'extensionattribute7'; e = { $_.extensionattribute7 -replace "'", "''" } },
@{ n = 'extensionattribute1'; e = { $_.extensionattribute1 -replace "'", "''" } } |
Export-Csv 'C:\temp\Windows7_Only.csv' -NoType -Force

$csv = Import-Csv -Path "c:\Temp\Windows7_Only.csv"
foreach ($row in $csv)
{
    if (($row.CN -notmatch '^U\d{7}') -and ($row.DistinguishedName -like "*Laptops*") -and ($row.extensionattribute7 -match '^U\d{7}$'))
{
    $row.CN = $row.extensionattribute7 + "-TPL-ZZZ"
}
elseif (($row.CN -notmatch '^U\d{7}') -and ($row.DistinguishedName -like "*Desktops*") -and ($row.extensionattribute7 -match '^U\d{7}$'))
{
    $row.CN = $row.extensionattribute7 + "-TPD-ZZZ"
}
$csv | export-csv c:\fixed.csv -Force
}

2 个答案:

答案 0 :(得分:0)

假设我正确理解您的要求:

$csv = Import-Csv -Path "c:\Temp\Windows7_Only.csv"

foreach ($row in $csv) {
    if ($row.DistinguishedName -like "*Desktops*") {
        $suffix = "-TPD-ZZZ"
    }
    elseif ($row.DistinguishedName -like "*Laptops*") {
        $suffix = "-TPL-ZZZ"
    }

    if ( ($row.CN -notmatch '^U\d{7}') `
    -and ($row.extensionattribute7 -match '^U\d{7}$') ) {
        $row.CN = $row.extensionattribute7 + $suffix
    }
}

$csv | export-csv c:\fixed.csv -Force -NoTypeInformation                        

答案 1 :(得分:0)

良好的开端,但请允许我说,如果您可以访问Active Directory管理单元,则应该完全使用它,而不是创建LDAP搜索器,而不是。

现在,关于你的比较......正如马特所说,你的比赛应该是对抗$ .CN。这意味着$ ,它表示循环记录时的当前记录,而.CN部分表示它应该查看记录的CN属性。

然后你可以使用-Match和(再次)像Matt所说的那样(他是新来的,但证明知识渊博),可以缩短为"U\d{8}"

现在,您实际上想要找到不是的那些U\d{8},所以让我们先用!作为-Not的别名。然后让我们检查一下ExtendedAttribute7是否正确。那么这看起来像:

!$_.CN -like "U\d{8}" -and $_.ExtendedAttribute7 -match "U\d{8}"

出色!我们有需要更新的行的过滤器。这正是亚历山大所做的。至于我,我会更多(使用你的脚本作为基础):

#Create an LDAP searcher object and pass in the DN of the domain we wish to query
$Searcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]"LDAP://DC=ten,DC=domain,DC=com")

#Pass in the ceriteria we are searching for.
#In this case we're looking for users with a particular SAM name.

$Searcher.Filter = "(&(objectCategory=computer)(objectClass=computer)(!UserAccountControl:1.2.840.113556.1.4.803:=2)(operatingSystem=Windows 7*))"

$Searcher.PageSize = 100000

# Populate General Sheet(1) with information

$results = $Searcher.Findall()

$Computers = @()
ForEach($Item in $results){ 
    $Comp = $Item.GetDirectoryEntry()
    If($Comp.distinguishedName -like "*desktops*"){$Suffix = "TPD-ZZZ"}else{$Suffix = "TPL-ZZZ"}
    $CN = If(!$Comp.CN -match "U\d{8}" -and $Comp.extensionattribute7 -match "U\d{8}"){$Comp.extensionattribute7+$Suffix}else{$Comp.CN}
    $Computers += [PSCustomObject][Ordered]@{
        'CN' = $CN -replace "'", "''"
        'DistinguishedName' = $Comp.DistinguishedName[0] -replace "'", "''"
        'extensionattribute7' = $Comp.extensionattribute7[0] -replace "'", "''"
        'extensionattribute1' = $Comp.extensionattribute1[0] -replace "'", "''"
    }
}
$Computers | Export-Csv 'C:\temp\Windows7_Only.csv' -NoType -Force
$Computers