【问题标题】:How to create specific user log files in PowerShell?如何在 PowerShell 中创建特定的用户日志文件?
【发布时间】:2019-01-29 10:50:46
【问题描述】:

我知道有些问题已经得到解答,但是这些是我不需要的日志文件,我无法让它工作。 PowerShell 的这一部分是我还没有学过的东西。我正在尝试执行以下操作:我编写了一个脚本来创建用户、邮箱、文件夹等,但是我想启用一个日志文件,简单明了。

只有某些人才能访问的内容,并且日志文件包含以下信息,例如:PSmith 在 2019 年 1 月 29 日创建了用户帐户 JDoe。

我该怎么做?我尝试了很多在网上找到的方法,但是我必须重写我的整个脚本才能使某些脚本正常工作。

【问题讨论】:

  • 你指的是一些审计日志吗?
  • @PrasoonKarunanV - 我们想监控谁创建了一个帐户,它可以导出到存储在另一个磁盘上的隐藏 .txt 文件(或其他东西,但我正在检查什么是最好的解决方案)。

标签: powershell logging active-directory


【解决方案1】:

最简单的选择:您可以enable transcript 记录在控制台输入的每个命令,这可能会创建一个巨大的文件,但您必须自己过滤特定消息。

有像 microsoft orchestrator 这样的工具可以运行您的脚本并自动记录结果,但这些工具很昂贵,除非您几乎必须自己构建日志记录。

我的建议是将消息发送到windows event logs。这样您就不必管理日志文件,Windows 会为您提供日期和时间戳。这也将使其审核和查询友好。

【讨论】:

    【解决方案2】:

    我相信你需要这样的东西,如果不需要,请提供更多代码。

    $creator = (get-aduser $env:username | Select Name).Name
    $date = get-date -UFormat "%d/%m/%Y"
    

    我相信你的脚本中有$user。

    $log = $creator + "created on " + $date + " the user account " + $user.Name
    Out-File $log "C:\temp\log.csv"
    

    【讨论】:

    • 谢谢,但是我收到以下错误:Out-File : Cannot validate argument on parameter 'Encoding'. The argument "C:\temp\log.csv" does not belong to the set "unknown,string,unicode,bigendianuni code,utf8,utf7,utf32,ascii,default,oem" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again. At C:\Users\user\Desktop\tmp\log.ps1:7 char:15 + Out-File $log "C:\temp\log.csv" 当我尝试删除第二个 get-date 并将 $log 更改为 Out-File -filepath "C:\temp\log.csv" 并重新运行脚本时比我不会出错
    • 但是,当我打开 csv 文件时,我在第一列中看到的唯一内容是:ÿþ。
    • 这有点多余:(get-aduser $env:username | Select Name).Name 使用:$creator = Get-Aduser $env:USERNAME | Select-Object -Expandproperty Name 或更短的$creator = (Get-Aduser $env:USERNAME).Name
    • @LotPings - 我仍然有这个错误:Out-File : Cannot validate argument on parameter 'Encoding'. The argument "C:\temp\log.csv" does not belong to the set "unknown,string,unicode,bigendianuni code,utf8,utf7,utf32,ascii,default,oem" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again. At C:\Users\user\Desktop\tmp\log.ps1:7 char:15 + Out-File $log "C:\temp\log.csv"
    • @signedup 我的评论只是针对第一个命令。请参阅Get-Help Out-File 第一个参数是 -Path 或 -Encoding。您可以使用$log | Out-File -FilePath "C:\temp\log.csv" 或Out-File -FilePath "C:\temp\log.csv" -Inputobject $log
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多