TEXT

Sunday, January 31, 2021

Using Logging Function within PS Scripts to get more inside.

 

Following function will demonstrate how to use logging within your scrips.

 


#(1)_.Create Log folder vars

$repname = Read-host 'Provide Report Name'

 IF(!($repname)){

 $repname = 'DRFAULT-REPORT'}

 $logout  = $repname+'-Log.TXT'

 $csvout  = $repname+'-Log.CSV'

 $repout  = $repname+'-Advance-Log.CSV'

 $traout  = $repname+'-Transcript.LOG'

 $desFol  = "C:\temp\Reports_\$repname\"

 $logTXT  = $desFol+$now+$logout

 $logCSV  = $desFol+$now+$csvout

 $AdvCSV  = $desFol+$now+$repout

 $scrTXT  = $desFol+$now+$traout

 $luser   = $env:USERNAME

#(2)_.Start Transcript

Start-Transcript -Path $scrTXT | Out-Null

function Function-Create-Log-Folder{

  [CmdletBinding()]

  param(

  [Parameter(Mandatory=$True)]

  [String]$DestinationFolder

  ) 

 Try{

 If (!(Test-Path $DestinationFolder)){

     New-Item -ItemType Directory -Force $DestinationFolder | Out-Null

     }

  }Catch{

  $errofile = $($PSItem.ToString())

  Write-Warning 'Error has occoured'

  Write-host 'Problem FOUND' $errofile -ForegroundColor red -BackgroundColor Black

    }

}

Function-Create-Log-Folder -DestinationFolder $desFol

# Advance Logging

function Write-Log-Advance {

     [CmdletBinding()]

     param(

         [Parameter()]

         [ValidateNotNullOrEmpty()]

         [string]$Count,

         [string]$Message1,

         [string]$Message2,

         [string]$Message3,

         [string]$Message4,

         [string]$Message5,

         [string]$Info1,

         [string]$Info2,

         [string]$Info3,

         [string]$Info4,

         [string]$Error1,

         [string]$Error2,

         [Parameter()]

         [ValidateNotNullOrEmpty()]

         [ValidateSet('Information','Warning','Error')]

         [string]$Severity = 'Information'

     )

     $now = (Get-Date).ToString('MM-dd-yyyy-hh-mm-ss-tt')

     [pscustomobject]@{

        

         Count     = $Count

         Message1  = $Message1

         Message2  = $Message2

         Message3  = $Message3

         Message4  = $Message4

         Message5  = $Message5

         Info1     = $Info1

         Info2     = $Info2

         Info3     = $Info3

         Info4     = $Info4

         Severity  = $Severity

         Time      = (Get-Date -f g)

     } | Export-Csv -Path $AdvCSV -Append -NoTypeInformation

 }

 

 # Test Vars

 $total     = 40

 $i         = 1

 $remaining = $total-$i

 

 # Test Loop

while($i -lt $total)

{

    $remaining = $total-$i

    $user = 'John.Doo'

    $message1 = "Processing $user "

    $message2 = "Server:MBX($i)"

    $message3 = "Completed work"

    $message4 = "total users:$total, Completed:$i , remaining:$remaining"

    $info1 = "$user migration completed"

 

    Write-Progress $message1 $message4

    Write-host "Processing $user($i)"

    Write-Log-Advance -Count ($i) -Message1 $message1 -Message2 $message2 `

                                  -Message3 $message3 -Info1 $message4

    $i++

}

 

# Stop-Transcript

try{

  stop-transcript|out-null

}

catch [System.InvalidOperationException]{}

 

 


Simple Function Do Something

   Use template below to create simple PS function    # Simple Function Do Something # Change $ReportName   function Fu...