我有一个很棒的PowerShell脚本,我写了一段时间,将用户从一个Active Directory组复制到另一个。我需要记录每个屏幕输出的结果,以便我记录脚本应该出现的问题。我想将输出写入C:\ Temp \ CopyLog.txt,但我无法弄清楚如何让它正常工作。我尝试过Add-Content和out-file,但他们似乎并不喜欢我。
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 :(得分:0)
Start-Transcript -Path C:\Temp\CopyLog.txt -Append
# Rest of script goes here
Stop-Transcript