简体   繁体   中英

Custom Log File Creation - Powershell

I have the following requirement for a script:

a. Get the Scripts Name and Path. b. Create a ScriptPath\\Log-Time|date\\Logfile.Log c. Give the user 3 options, depending on the input update the log file.

For the above requirement ive created the following script:

#Variables Declaration-------------------------------------------
$pat = Split-Path $script:MyInvocation.MyCommand.Path -parent
$LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$a = "[Info]:"+$LogTime+" Logged into Server"
$b = "[Warning]:"+$LogTime+" Unauthorized Access"
$c = "[Error]:"+$LogTime+" Wrong Credentials"
$ScriptName = $MyInvocation.MyCommand.Name
$path = $pat

#Folder and Log Creation------------------------------------------
if([IO.Directory]::Exists($path))
{
$m = New-Item -ItemType directory -Path $path -name Log
$n = New-Item -ItemType directory -Path $m -name $LogTime
}

$LogName = $ScriptName+"_Log_"+$LogTime+".log"
$log = New-Item -ItemType file -path $n -name $LogName

# Log Function------------------------------------------------------
log($in)
function log 
{
$in = Read-Host "Enter your Option"

if ($in -eq "0") 
{
    $latest = Get-ChildItem -Path $n | Sort-Object LastAccessTime -Descending | Select-Object -First 1
    $p = $path+$latest.name
    Add-Content $p  -value $a 
}
elseif ($in -eq "1") 
{

    $latest = Get-ChildItem -Path $n | Sort-Object LastAccessTime -Descending | Select-Object -First 1
    $p = $path+$latest.name
    Add-Content $p  -value $b 

}
elseif ($in -eq "2") 
{

    $latest = Get-ChildItem -Path $n | Sort-Object LastAccessTime -Descending | Select-Object -First 1
    $p = $path+$latest.name
    Add-Content $p  -value $c 

}
else
{
    $o = "Invalid Input"
    $o
}

Move-Item $p $n
}

Whenever i run this, i get two logfiles created.

Exec2.ps1_Log_04-04-2014_10-21-11.log &&&& MExec2.ps1_Log_04-04-2014_10-21-11.log [M is the folder where the script is run]

and the first log file is empty while the second one contains the text.

Could anyone please help me fix this? and if possible make the script short and sweet somehow?

Thanks and Regards, Kain

Some of your code seemed redundant, so I removed some. Such as defining $pat and then duplicating it into $path so I just defined $path once. I got rid of $a , $b , and $c since you can just as easily enter the value directly.

Then I got rid of the whole get the latest file in the log folder, and just added the content to the log file defined earlier. I changed the log entries so they were done by tabs so the entry type, date, and the actual entry all line up nicely when you view the log file.

Lastly I changed the whole If>ElseIf>ElseIf thing into a Switch, because Switch is awesome and totally underused! Oh yeah, I got rid of $o = "Invalid Input"; $o $o = "Invalid Input"; $o because it's pointless to define a variable and then just echo it. What you could do is in the default section have it write-host the error and call the log function again, so people are forced to enter a valid entry.

#Variables Declaration-------------------------------------------
$path = Split-Path $script:MyInvocation.MyCommand.Path -parent
$LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$ScriptName = $MyInvocation.MyCommand.Name

#Folder and Log Creation------------------------------------------
$m = New-Item -ItemType directory -Path $path -name Log
$n = New-Item -ItemType directory -Path $m -name $LogTime

$LogName = $ScriptName+"_Log_"+$LogTime+".log"
$log = New-Item -ItemType file -path $n -name $LogName

# Log Function------------------------------------------------------
log
function log{
    $in = Read-Host "Enter your Option (0,1,2)"

    Switch ($in){
        0 {Add-Content $log  -value ("[Info]:`t`t" + $LogTime + "`tLogged into Server")}
        1 {Add-Content $log  -value ("[Warning]:`t" + $LogTime + "`tUnauthorized Access")}
        2 {Add-Content $log  -value ("[Error]:`t" + $LogTime + "`tWrong Credentials")}
        default {Write-Output "Invalid Input"}
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM