#region Configure automatic logging for all hosts
$logpath = "C:\logs\powershell\$(Get-Date -Format "`yyyyMMdd_hhmmss`").log"
if( -not ([System.IO.Directory]::Exists((Split-Path -Path $logpath)))) {
New-Item -Path (Split-Path -Path $logpath) -ItemType Directory | Out-Null
}
Start-Transcript -Path $logpath -Force | Out-null
#endregion Configure automatic logging for all hostsTo easily access this file, if it exists, you can use this command from within Powershell:notepad $profile.AllHostsAllUsersThis will throw notify you if the file does not exist only after you open the file. When you attempt to save it, you are forced to renavigate to the path during the save command. I will write up a little script to automate this as well with a quick check before opening the command. Another nice addition for the $profile management module I'm working on.EDIT: After I posted some extra details came to mind which should go in the file name. I added the host name (what is running Powershell) annd the username (the account) to the logfile name. This way, it's much easier to track whose logs correspond with which hosts.
$logpath = "C:\logs\powershell\$((Get-Host).Name)_$($env:USERNAME)_$(Get-Date -Format "`yyyyMMdd_hhmmss`").log"
0 comments:
Post a Comment