ldif 文件的 powershell 脚本

问题描述 投票:0回答:1

如何编写 powershell 脚本来提取没有 goEmpid 的记录并将其输出到文件中对于包含此的 ldif 文件

"dn: cn=username,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca,
cn:username mail:[email protected]
uid: username
goEmpid:2273773

dn: cn=username1,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca,
cn:username1 mail:[email protected]
uid: username1

dn: cn=username2,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca,
cn:username2 mail:[email protected]
uid: username2

dn: cn=username3,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca,
cn:username3 mail:[email protected]
uid: username3
goEmpid:2273776"
# the path to LDIF file
$ldifFilePath = "C:\Users\91701\Desktop\pki.ldif"

# the path for the output file
$outputFilePath = "C:\Users\91701\Desktop\output.txt"

# Reading the content of the LDIF file
$ldifContent = Get-Content $ldifFilePath -Raw

# Split LDIF content into individual records
$ldifRecords = $ldifContent -split "^\s*$" | Where-Object { $_ -match '\S' }

# Initializing an empty array to store records with missing 'goEmpid'
$missingGoEmpidRecords = @()

# Loop through each record and check if 'goEmpid' is missing
foreach ($record in $ldifRecords) {
    if ($record -notmatch 'goEmpid') {
        $missingGoEmpidRecords += @"
        Record with missing 'goEmpid':
     $record
    -------------------------
     "@
}

}

# Output the missing 'goEmpid' records to the console
Write-Output $missingGoEmpidRecords

# Save the output to a file
$missingGoEmpidRecords | Out-File -FilePath $outputFilePath -Append

问这给了我一个空文件,而不显示缺少 goEmpid 的记录`'

powershell active-directory ldap
1个回答
0
投票

好吧,首先我假设 LDIF 是 LDAP 数据交换格式,但看起来您的数据有点混乱,因为您的

dn
以逗号结尾,并且您的
cn:
mail:
都处于打开状态同一条线。

为了这个答案,我要把它改成这样:

$data = @"
dn: cn=username,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca
cn:username
mail:[email protected]
uid: username
goEmpid:2273773

dn: cn=username1,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca
cn:username1
mail:[email protected]
uid: username1

dn: cn=username2,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca
cn:username2
mail:[email protected]
uid: username2

dn: cn=username3,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca
cn:username3
mail:[email protected]
uid: username3
goEmpid:2273776
"@

接下来的事情可能是将数据解析为我们可以更轻松地使用的记录。这绝不是一个完整的 LDIF 解析器,但它适用于您的示例数据,所以希望我也能处理它的其余部分......


function ConvertFrom-Ldif
{
    param(
       [Parameter(Mandatory=$True)]
       [string] $ldif
    )
    $records = @( $ldif -split "`n`n" );
    $records | foreach-object {
        $fields = $_ -split "`n"
        $record = $fields | foreach-object `
            -begin {
                $properties = [ordered] @{}
            } `
            -process {
                $split = ($_ -split ":").Trim()
                $properties[$split[0]] = $split[1]
            } `
            -end {
                return [pscustomobject] $properties
            }
        $record
    }
}

这给出:

$records = ConvertFrom-Ldif $data
$records | format-list *

dn      : cn=username,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca,cn
mail    : [email protected]
uid     : username
goEmpid : 2273773

dn   : cn=username1,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca,cn
mail : [email protected]
uid  : username1

dn   : cn=username2,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca,cn
mail : [email protected]
uid  : username2

dn      : cn=username3,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca,cn
mail    : [email protected]
uid     : username3
goEmpid : 2273776

现在我们可以过滤以仅获取具有

goEmpId
属性的记录:

$filtered = $records | where-object { @($_.psobject.Properties.Name) -contains "goEmpid" }
$filtered | format-list *

dn      : cn=username,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca,cn
mail    : [email protected]
uid     : username
goEmpid : 2273773

dn      : cn=username3,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca,cn
mail    : [email protected]
uid     : username3
goEmpid : 2273776

最后我们可以将过滤后的记录转换回 ldif 文本字符串:

function ConvertTo-Ldif
{
    param(
       [Parameter(Mandatory=$True)]
       [object[]] $objects
    )
    $records = @( $objects | foreach-object {
        $fields = $_.psobject.Properties | foreach-object {
            "$($_.Name): $($_.Value)"
        }
        return @($fields) -join "`n"
    })
    @($records) -join "`n`n"
}

给予:

$ldif     = ConvertTo-Ldif $filtered
$ldif

dn: cn=username,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca
cn: username
mail: [email protected]
uid: username
goEmpid: 2273773

dn: cn=username3,ou=organisationname,ou=org-ca,ou=go-pki,o=finance of organisation,st=on,c=ca
cn: username3
mail: [email protected]
uid: username3
goEmpid: 2273776
© www.soinside.com 2019 - 2024. All rights reserved.