运行此代码后如何将屏幕结果输出到txt文件?
#removes disabled clinical or corp accounts from SGs in the I-Drive OU
$searchOU = "OU=I-Drive,OU=SAS,OU=Application Security Groups,OU=Groups,OU=Enterprise,DC=z,DC=x,DC=y"
Get-ADGroup -Filter 'GroupCategory -eq "Security"' -SearchBase $searchOU | ForEach-Object{ $group = $_
Get-ADGroupMember -Identity $group | Get-ADUser | Where-Object {$_.Enabled -eq $false} | ForEach-Object{ $user = $_
$uname = $user.Name
$gname = $group.Name
Write-Host "Removing $uname from $gname" -Foreground Yellow
Remove-ADGroupMember -Identity $group -Member $user -Confirm:$false
}
}
答案 0 :(得分:1)
将Get-ADGroup
的输出输到Set-Content
就像这样:
Get-ADGroup -Filter 'GroupCategory -eq "Security"' -SearchBase $searchOU | ForEach-Object{
$group = $_
Get-ADGroupMember -Identity $group | Get-ADUser | Where-Object { $_.Enabled -eq $false} | ForEach-Object{
$user = $_
$uname = $user.Name
$gname = $group.Name
Write-Host "Removing $uname from $gname" -Foreground Yellow
Remove-ADGroupMember -Identity $group -Member $user -Confirm:$false
}
} | Set-Content filename.txt
如果您需要任何其他输出(警告,详细信息,错误)change the last line a bit to redirect the other streams:
} *>&1 | Set-Content filename.txt
或者,您也可以使用built-in transcript logging将所有内容记录到文件中,也可以从脚本中调用以下其中一项:
Start-Transcript
或者如果您希望日志转到特定位置:
Start-Transcript -Path "\Path\To\LogFile.log"
请注意,与交互式会话相比,脚本中的脚本记录更为有用。
答案 1 :(得分:1)
$abc | Out-File -FilePath "C:\Somewhere\log.txt" -Append -Encoding UTF8