未加星标

A simple logger using PowerShell Class

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

‘Sup PSHomies,

At our last DuPSUG meeting, Jaap Brassers did a short Demo on the latest addition to PowerShell Streams, Information.How did I miss this?

So what’s so great about the new Information stream ? The Information stream solves the Write-Host issue of not sending output to a stream like: error;verbose;warning etc. As such we were told that Write-Host is evil and every time you use it a puppy dies… Who wants that on their conscious right?

Well with the Information stream Write-Host is allowed… no more puppy genocide!

Jaap showed us what the stream output looks like and how to write to the information stream. Here’swhat the output looks like


A simple logger using PowerShell Class

Here are a few properties we can play with: TimeGenerated,Source,User,Computer,Tags and MessageData. You get all of this by writing/redirecting to the information stream. The TimeGenerated property caught my eye, imagine creating a timeline of sorts. While googling (don’t act like it’s just me…) I came across a great article by Jeff Hicks on the subject (Seriously, how did I miss this?) In it he also talked about generating a timeline view…:wink:

After I had some time to think about how to make us of this, it dawned on me… You know what else is native to PowerShell v5? Classes!When Classes were introduced in v5, they were another way to write DSC Resources . I’ve always thought that there’s more to classes than meets the eye.

If you’re interested in classes, Richard Siddaway has a great article taking you through each step, worth trying out.

Jaap’s demo gave me the idea to try my hand at a simple logger using classes. Here’s what I came up with…

Here’s a quick rundown:


A simple logger using PowerShell Class

The idea is to gather output from the Information stream. Constructors are a way of initiating an object for use (Shout out to Doug Finke for breaking this down for me!)


A simple logger using PowerShell Class

I “borrowed” Jeff Hicks idea of validating the preference first. This way you can toggle between showing Write-Information and/or Write-Host data. You can use Write-Host for a more colorful experience. Did I already mention that Write-Host is cool again?:stuck_out_tongue:. I also added the possibility to show data in the console if you choose to, a verbose action of sorts…


A simple logger using PowerShell Class

I also added two methods of exporting: text and xml. That’s where LogDirectory and FileBaseName come in. For the text export, I selected TimeGenerated and MessageData for a timeline view (I know I know Jeff Hicks did it first :-P). Export to xml saves InfoMessages in raw format.

Here’s some code to play with.

function Get-PowerShellProcess{ [cmdletbinding()] param() Write-Verbose "InformationPreference = $InformationPreference" $Logger.Preference = $InformationPreference $Logger.WriteInformation("Starting $($MyInvocation.MyCommand)") Get-Process -Name *PowerShell* $Logger.WriteInformation("Finishing $($MyInvocation.MyCommand)") } #region Main #Initiate variable $Logger = [WriteLog]::New('psProcess','C:\scripts\temp') #Run with InformationAction. Information will be saved and not shown Get-PowerShellProcess -InformationAction Continue -Verbose #Run without InformationAction with Console set to $true $Logger.Console = $true Get-PowerShellProcess -Verbose #Run without InformationAction with Console set to $false $Logger.Console = $false Get-PowerShellProcess -Verbose #endregion

Here are some screenshots of the output

I initiated the object a FileBaseName value ‘psProcess’ and LogDirectory ‘C:\scripts\temp’. Default Preference is ‘SilentContinue’ and no ouput to the console


A simple logger using PowerShell Class

First up run with action set to continue


A simple logger using PowerShell Class

$Logger.InfoMessages has redirected data.


A simple logger using PowerShell Class

When console is set to true we’ll see the Information message without explicitly settingInformationAction to ‘continue’. Using InformationAction will save the Information data to InfoMessages property.


A simple logger using PowerShell Class

Setting the console preference back to $false stops displaying output to the console


A simple logger using PowerShell Class

I hope that you were able to follow it all. 2016 was all about Operation Validation for me. I think classes are going to be mainstream in 2017! PowerShell + Classes will definitely give you an edge and not to mention make you feel more like a developer!:wink:

The line between Dev and Ops is being blurred…

Hope it’s worth something to you…

Ttyl,

Urv

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

主题: PowerShellSU
分页:12
转载请注明
本文标题:A simple logger using PowerShell Class
本站链接:http://www.codesec.net/view/523424.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(36)