2016-05-02 63 views
0

我有這個偉大的powershell腳本,我寫了一段時間後,哪些副本的用戶從一個Active Directory組到另一個。我需要記錄每個屏幕輸出的結果,以便記錄腳本應該做什麼的日誌。我想寫輸出到C:\ Temp \ CopyLog.txt,但我無法弄清楚如何讓它正常工作。我嘗試過添加內容和外部文件,但他們似乎不喜歡我。需要將我的結果記錄到單獨的文件

Import-Module activedirectory 

$string = Get-Content C:\Temp\GroupsToCopy.txt 
foreach($i in $string) 
{ 
$split = $i.split(";") 
$Source_Group = $split[0] 
$Destination_Group = $split[1] 

"***Results before script***" 

"Current Member's of: $Source_Group" 
Get-ADGroupMember $Source_Group | select name | ft -hide 
"`n" 
"Current Member's of: $Destination_Group" 

$Source = Get-ADGroupMember $Source_Group | select name | ft -hide 

$DST = Get-ADGroupMember $Destination_Group 


if ($DST -eq $null) 
{ 
"Group has no members" 
} 
else 
{ 
Get-ADGroupMember $Destination_Group | select name | ft -hide 
} 

$Target = Get-ADGroupMember $Source_Group 
foreach ($Person in $Target) 
{ 
    Add-ADGroupMember $Destination_Group -Members $Person.distinguishedname 
} 

"`n" 
"***Results after script***" 
"`n" 

"Current Member's of: $Source_Group" 
Get-ADGroupMember $Source_Group | select name | ft -hide 
"`n" 
"Current Member's of: $Destination_Group" 
Get-ADGroupMember $Destination_Group | select name | ft -hide 
"`n" 
} 

回答

0

使用Start-Transcript

Start-Transcript -Path C:\Temp\CopyLog.txt -Append 
# Rest of script goes here 
Stop-Transcript 
相關問題