簡體   English   中英

Powershell導出Active Directory用戶數據不完整

[英]Powershell export Active Directory User data not complete

在刪除帳戶之前,我必須先導出帳戶的用戶數據。 問題是,並非所有的組成員身份都寫入.txt文件中(下面的示例)。

這是代碼:

              Get-ADUser  -Properties * -Filter "cn -eq '$name'" |
              format-list -property @{Label = "Name";Expression = {$_.sAMAccountName}},
              @{Label = "Initials";Expression = {$_.initials}},    
              @{Label = "Email";Expression = {$_.Mail}},
              @{Label = "Groups";Expression = {%{(Get-ADPrincipalGroupMembership $name | select -expandproperty name)}}},
              @{Label = "Creation";Expression = {$_.whenCreated}},
              @{Label = "Deletion";Expression = {%{(Get-Date)}}},
              @{Label = "Last change";Expression = {$_.whenChanged}}  |

              #write data into txt file

             Out-File -append "C:\temp\deleted.txt" -Encoding utf8 

這是輸出:


Name            : John Doe
Initials        : Jdo
Email           : John.Doe@acme.com
Groups          : {Domain-User, Remotedesktopuser, Administrator, Share-User...}
Creation        : 23.03.2018 13:36:44
Deletion        : 17.12.2018 08:46:30
Last Change     : 16.12.2018 10:42:21

確實不是由Format-List引起的,選擇會發生同樣的事情,盡管像這樣使用Format- *並不是真正的事情。 默認情況下,這將是一個列表,因此,沒有真正的理由將其用於所追求的目標。

您甚至不需要擴展。

問題是您不能使用該循環並期望它可以工作,自動格式化程序將不允許它。 您必須直接處理集合,就像這樣...

Get-ADUser  -Properties * -Filter * |
Select-Object -property @{Label = "Name";Expression = {$_.sAMAccountName}},
@{Label = "Initials";Expression = {$_.initials}},    
@{Label = "Email";Expression = {$_.Mail}},
@{Label = "Creation";Expression = {$_.whenCreated}},
@{Label = "Deletion";Expression = {%{(Get-Date)}}},
@{Label = "Last change";Expression = {$_.whenChanged}},
@{Label = "Groups";Expression = {%{(Get-ADPrincipalGroupMembership $_.SamAccountName).Name -join ','}}} |
Out-File -append "C:\temp\deleted.txt" -Encoding utf8 
Get-Content -Path "C:\temp\deleted.txt" 

# Results

Name        : Administrator
Initials    : 
Email       : Administrator@contoso.com
Creation    : 3/31/2017 8:02:15 PM
Deletion    : 12/17/2018 4:07:52 AM
Last change : 12/9/2018 7:23:22 PM
Groups      : Domain Users,Administrators,Schema Admins,Enterprise Admins,Domain Admins,Group Policy Creator Owners,Organization Management,Recipient 
              Management,ADSyncAdmins,ADRMSSuperUsers
…

根據OP評論/問題進行更新

不用擔心,很高興它為您服務。

至於...

您介意為我解釋這兩個AD Group命令之間的區別是什么?

如果你的意思是 ...

Get-ADPrincipalGroupMembership管理員| 選擇-expandproperty名稱
... vs ...(Get-ADPrincipalGroupMembership管理員)。名稱

...表面上看,它們是同一件事,每個產生和陣列的組名列表。

# use the expand switch to show the group name list
Get-ADPrincipalGroupMembership Administrator | select -expandproperty name

Domain Users
Administrators
Schema Admins
Enterprise Admins
Domain Admins
Group Policy Creator Owners
Organization Management
Recipient Management
ADSyncAdmins
ADRMSSuperUsers

# Use the property to view the group name list
(Get-ADPrincipalGroupMembership Administrator).Name

Domain Users
Administrators
Schema Admins
Enterprise Admins
Domain Admins
Group Policy Creator Owners
Organization Management
Recipient Management
ADSyncAdmins
ADRMSSuperUsers

但是,格式化程序在對數據進行序列化時,將嘗試將所有內容放在一行上。 但是,他們會將其截斷以適合屏幕/頁面寬度。 因此,如果您想要不同的布局,則要么需要使用默認的格式化程序文件來處理,要么通過代碼來處理。 就我個人而言,我從不嘗試與他們混為一談,而只是努力用代碼來處理它。 所以這...

(Get-ADPrincipalGroupMembership Administrator).Name -join ','

...只是說,我知道這個集合是一個數組列表。 我知道這將在每個屏幕/頁面寬度上被截斷,因此,將此字符串列表連接為一個字符串並自動換行。

您可以用原來的擴展方式以相同的方式完成相同的事情...

(Get-ADPrincipalGroupMembership Administrator | select -expandproperty name) -join ','

出於美觀原因和較短的形式,我將組列表放在最后,因為我寧願不要編寫不必要的代碼或盡可能多地使用不需要的選項。 每個人都有自己的喜好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM