Skip to content

Instantly share code, notes, and snippets.

@irwins
Created January 4, 2017 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save irwins/30025a84c1af72d8c576c3d73db95e7c to your computer and use it in GitHub Desktop.
Save irwins/30025a84c1af72d8c576c3d73db95e7c to your computer and use it in GitHub Desktop.
Class WriteLog{
[string]$FileBaseName = 'Temp'
[string]$LogDirectory = 'C:\Scripts\export'
[string]$Preference = 'SilentContinue'
[bool]$Console = $true
[PSObject[]]$InfoMessages
#default constructor
WriteLog(){}
#constructor
WriteLog($fbn,$ld){
$this.FileBaseName = $fbn
$this.LogDirectory = $ld
}
WriteInformation($MessageData){
$info = $false
if ($this.Preference -eq 'Continue') { $Info = $true }
if ($Info) {
$this.InfoMessages += Write-Information -MessageData $MessageData 6>&1 | Select-Object *
}
if($this.Console){
Write-Information -MessageData $MessageData -InformationAction Continue
}
}
ExportToText(){
$this.InfoMessages |
Select TimeGenerated,MessageData |
Out-File "$($this.LogDirectory)\$($this.FileBaseName).txt" -Encoding utf8 -Force -Append -Width 1000
}
ExportToXML(){
$this.InfoMessages |
Export-Clixml -Path "$($this.LogDirectory)\$($this.FileBaseName).xml" -Encoding UTF8 -Force
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment