我有一个修改活动目录中用户属性的代码。 如何修改代码,在日志文件中查看更改了什么?如果有错误,我也想看看。如果是错误那是什么。
代码:
$csvdata = Import-Csv $csv -Delimiter $delimiter -Encoding "UTF8"
ForEach-Object -InputObject $csvdata {
$params = @{Identity = $_.ObjectGUID}
$sn = $_.Surname.Trim()
$gn = $_.GivenName.Trim()
$Manager = $_.Manager
if (-not [string]::IsNullOrWhiteSpace($_.Surname)) {
$params.Surname = $_.Surname
}
if (-not [string]::IsNullOrWhiteSpace($_.Givenname)) {
$params.Givenname = $_.Givenname
}
Set-ADUser @params
}
答案 0 :(得分:1)
您可以使用成绩单命令。
Start-Transcript
& ".\Script.ps1"
Stop-Transcript
答案 1 :(得分:0)
我编写了以下函数,用于登录我做了一段时间的小脚本,随意将其删除并滥用它!
function Write-Log {
param(
[Parameter(ValueFromPipeline =$true,
ValueFromPipelineByPropertyName=$true,
Position=0
)]
[string[]]
$Message,
[string]
$Level = "Informational"
)
if (!$loglocation)
{
write-error -Message 'No $LogLocation set.'
break
}
$logarchive = split-path $loglocation
$logarchive = $logarchive + "\Archive.log"
$truedate = get-date -f yyyy-MM-dd_HH:MM:ss
if (test-path $loglocation)
{
if ((get-item $loglocation).length -ge 5242880)
{
"$truedate : Log exceeds 5MB, archiving and creating new log." | out-file -append -filepath $loglocation
if (test-path $logArchive)
{
"$truedate : Log archive already exists, removing this log first before archiving current log." | out-file -append -filepath $loglocation
get-item $logArchive | remove-item
}
get-childitem $loglocation | rename-item -NewName {"Archive.log"}
"$truedate : New log created, old log archived." | out-file -append -filepath $loglocation
}
}
"$truedate : $level - $message" | out-file -append -filepath $loglocation
}
您只需要在脚本中将名为$loglocation
的变量设置为您希望日志保存的文件路径,即“C:\ MyLogs”等。然后当您想要记录某些内容时,只需使用write-log -Message "Whatever you are logging"
。