Powershell 如何输出到日志文件
运行此代码时,如何将屏幕结果输出到txt文件Powershell 如何输出到日志文件,powershell,Powershell,运行此代码时,如何将屏幕结果输出到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"' -
#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
}
}
将的输出管道化为如下所示:
如果需要任何其他输出(警告、详细信息、错误):
或者,您也可以使用将所有内容记录到文件中,也可以从脚本中调用以下命令之一:
Start-Transcript
或者,如果您希望日志转到特定位置:
Start-Transcript -Path "\Path\To\LogFile.log"
请注意,脚本中的记录比交互式会话中的记录更有用。- 将要登录的值存储在变量中,例如$abc
- 写入日志文件并继续追加
$abc | Out file-FilePath“C:\Somewhere\log.txt”-追加-编码UTF8
请参阅-谢谢!这太完美了!
Start-Transcript
Start-Transcript -Path "\Path\To\LogFile.log"