How to create specific user log files in PowerShel

2019-08-23 17:24发布

I know there are question that is already answered however those are the log files I don't need and I can't get it to work. This part of PowerShell is something I didn't learned yet. I'm trying to do the following: I wrote a script to create users, mailboxes, folders etc however I want to enable a log file, just simple and plain.

Something only certain people can access and where the log files have the following info, for example: PSmith created on 01/29/2019 the user account JDoe.

How can I do this? I tried a lot found online however I have to rewrite my whole script for some to work.

2条回答
劳资没心,怎么记你
2楼-- · 2019-08-23 17:43

I believe you need something like this, if not please provide more code.

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

I believe you have the $user in your script.

$log = $creator + "created on " + $date + " the user account " + $user.Name
Out-File $log "C:\temp\log.csv"
查看更多
We Are One
3楼-- · 2019-08-23 17:50

Easiest option: you can enable transcript which would log every single command that is entered at the console potentially creating a huge file but you would have to filter for the specific messages yourself.

There are tools like microsoft orchestrator that would run your script and log the results automatically but those are expensive other than that you would pretty much have to build the logging yourself.

My suggestion would be to send the message to the windows event logs. This way you dont have to manage the log files, windows does it for you with date and time stamps. This would also make it audit and query friendly.

查看更多
登录 后发表回答