簡體   English   中英

搜索Active Directory並將多線程組中的名稱列表導出到txt文件中以獲取Powershell

[英]Searching Active Directory and exporting a list of names in mulitpule groups to a txt file for powershell

我試圖創建一個簡單的PowerShell腳本,該腳本將查看文本文件中的Active Directory組列表,然后在Active Directory中搜索組(無論類型如何),然后獲取每個組中的成員列表,以及然后將每個用戶的“名稱”屬性輸出到輸出文本文件。 現在,除了最后一部分,我幾乎完成了所有工作。 當將名稱輸出到文本文件時,它僅向文件輸出一個名稱,而不向其他文件輸出300。但是,如果取消輸出功能,腳本將僅在控制台中輸出所有想要的名稱。 有人可以解釋為什么我無法做到這一點的方法嗎? 我真的很好奇為什么我不能按照我想要的方式將輸出定向到文件。 此外,腳本可以正確地循環遍歷所有內容,而且我知道它也可以找到組(我知道這是因為當我查看文本文件時,我看到每個文件都有一個條目),但是腳本會遍歷前8個組並開始引發錯誤,指出它找不到應該循環的特定組。 但是它已經找到它們,並且每個文件僅輸出一個條目。 為什么是這樣?

我對第一個問題的答案更感興趣,因為腳本仍然可以正常執行任務。

因此,重申一下,我想知道為什么當腳本為每個組輸出300+時,腳本僅向文件輸出一個名稱。

      ##This is variable that will hold the file path for the list of Active Directory Groups##

 $file='C:\Users\me\Desktop\DL_Names.txt';

 ##Command to dump the list into a variable##

 $DLnames=get-content $file;

 ##This is the variable to hold the path for where the output files are to be placed##

 [string]$path='C:\Users\me\Desktop\DL_repository\';

 ##Loop through the variable and for each entry preform the instruction listed##

 foreach ($name in $DLnames)
 {

 ##These two variables are used to create the file name for the output files##

 [string]$filename=$name+'.txt';

  [string]$Fullpath=$path+$filename;

 #This variable is used to determine if the groups exists in Active Directory##

 $verifygroupexists = Get-ADGroup -Identity $name;

 ##This is the if statement that is used to determine if the group exists in Active Directory##

 if($verifygroupexists -eq $null)

 {

 ##If the group doesnt exist, create a file and output the string to the file stating so with the group name##

 ##Still working on the removing portion of this, need help##

 New-Item $fullpath -ItemType file;

 [string]$error='AD Group'+' '+$name+' '+'does not exist';

 $error | Out-File -filepath $Fullpath;

 $Removeentry=$name;

 $name.Remove($Removeentry);

 }  

 else

 {

 ##If the group does exist in Active Directory then create a new text file to be used for output.

 New-Item $fullpath -ItemType file;

       ##Get the list of memebrs in the group and place them into a new variable##

 $groupmember=get-adgroupmember -Identity $name;

              ##Now loop through each entry in the new variable and output to the text file each member's 'name' (A.K.A. Displayname)##

 foreach ($user in $groupmember)

 {


              ##This is where my issue is, its not outputting all of the names to the text file##

             $displayname=get-aduser -identity $User.SamAccountName | select name | Out-File -filepath $Fullpath; 


              };



         };


 }; 

例如,輸出總是像這樣:

名稱

最后,姓名1

什么時候應該是:

名稱

最后,姓名1

最后,Name2

最后,姓名3

最后,姓名4

最后,姓名5

等等等等

該行將為組中的每個用戶調用,並且每次運行時,它將覆蓋文件的內容。

$displayname=get-aduser -identity $User.SamAccountName | select name | Out-File -filepath $Fullpath;

如果將Out-File替換為Add-Content ,那么該命令將追加到文件中。 如果您反復運行腳本,請確保首先使用Set-Content清除文件。 您也不需要在此步驟中設置變量$displayname的值:

Set-Content -Value "" -Path $Fullpath
get-aduser -identity $User.SamAccountName | select name | Add-Content -Path $Fullpath;

暫無
暫無

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

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