CMTrace log viewer

CMTrace is a great log viewer:

http://www.adamtheautomator.com/building-logs-for-cmtrace-powershell/

Nice to have as it is showing errors and warnings in different colours and gives you possiblity to look at the log in the real time as it is still building:

https://www.microsoft.com/en-us/download/details.aspx?id=50012

Toolkit default installation path is “C:\Program Files (x86)\ConfigMgr 2012 Toolkit R2”.

IT IS FRIDAY!

9a4a98367ecac8a48762257dc4aedece

A few PowerShell tips – Part 2 – Useful commands / switches.

Few days ago I wrote similar article:

Part 1 : https://paweljarosz.wordpress.com/2016/02/21/a-few-powershell-tips-part-1-the-one-about-help/

only_lowe

Here is another portion of PowerShell features I find worth remembering, and I tend to forget about. Actually this is the reason why all these articles are here ^^

 

Show-Command – GUI to build PowerShell commands

 

Show-Command: That feature came out in PowerShell 3.0. Gives great ability to create commands in situation when you are not feeling so well with the shell yet, or if you simply would like to check all the attributes of a certain command:

PowerShell_Tips1

We can see that there is possibility to set even things like pipeline variable (that particular appeared in PowerShell 4.0 – here is a great article about it: https://rkeithhill.wordpress.com/2013/07/20/powershell-v4-pipelinevariable-common-parameter/) and many others through the GUI.

 

How to check PowerShell version installed

 

By the way, to check PowerShell version commands “host” or “$PSVersionTable” can be used:

PowerShell_Tips2

 

Simply “one-liners” simpler with version 3.0

 

Starting from version 3.0 there there is a possibility to use simplified syntax (but only for simple commands like single property listing etc.), for example in PowerShell 2.0 simply Get-Mailbox would look like:

Get-Mailbox | ? {$_.name -match “test”}

And other, shorter, way we can do the same in PowerShell 3.0:

Get-Mailbox | ? name -match test

PowerShell_Tips3

So there is no need for “$_.”, braces and quotes 🙂

 

Be lazy and set default parameters for cmdlets 🙂

 

Another worth mentioning thing is $PSDefaultParameterValues. Variable is set per session, and can be overwritten. Example usage:

$PSDefaultParameterValues = @{“get-wmiobject:class”=”win32_bios”}

After that when running gwmi cmdlet we wil by default will get win32_bios class:

PowerShell_Tips4

 

 -expandproperty – but why?

 

Exactly!

giphy1.gif

If you haven’t used that yet, you might be asking, why it is a cool thing to use. Here is a good example. You plan to use Get-WmiObject cmdlet that is not accepting pipelining – why? Because yo udo not want to use Get-CimInstance that accepts pipelining and does same…  🙂 Anyway after trying to read Win32_BIOS class you might get:

expand1.JPG

The exit from that situation is to get computer name, and puke it out from cmdlet as string – not an object:

expand2.JPG

expand.JPG

 

Label and expression – custom naming properties

 

This one is more useful you might expect 🙂 Great example can be found here:

https://mva.microsoft.com/en-us/training-courses/getting-started-with-powershell-3-0-jump-start-8276

where Jeff and Jason are explaining how to understand usage of pipe.

Here is also great article about it:

https://blogs.technet.microsoft.com/heyscriptingguy/2013/03/25/learn-about-using-powershell-value-binding-by-property-name/

Ed tells there that we can run a pipeline using property “by value” and “by name”, but there is a problem while we have a different name. And here somes the help! Example:

Let’s say we got one workstation and we would like to read status of particular service form it. If we run it Get-ADComputer and we run a simply pipe on Get-Service – we will ger an error:

GetServiceGetAdComputerNameFailed

BUT WHY!?!?!?!!?

whydawsons.JPG

Hey sure! Telling you now. So what we need to check is: Do we have something, which can let us to connect these two cmdlets to each other?

Firstly let’s check Get-ADComputer:

getAdComputer

Property is with the computer name is “Name”. Now we need to get state of one service on that workstation, we check if Get-Service if there is a change to get hookup:

Get-ServiceProperty

So what we need to do here is…:

GetServiceGetAdComputerName

…and TAAADAAAAM!! All we needed to do is to change the property name.

“n” is alias “name”, you can also use “l” for label in exchagne. “e” is for expression. So that part of a command might look like:

select @{name=’ComputerName’;expression={$_.name}}

or

select @{label=’ComputerName’;expression={$_.name}}

 

-PassThru the command

 

You want to run a command and see a right away output of what you did? No problem, just use “-PassThru” switch!

PassThru.JPG

 

Match and Like shows that string has “$”,”*” and “?” while it doesn’t.

“Microsoft Office 365: Exchange Online Implementation and Migration” tells that we need to check following users’ attributes in regards to occurrence special characters:

    sAMAccountName: Invalid character examples are !, #, $, %, ^, &, {, }, \, /, `, ~, “, [, ], :, @, <, >, +, =, ;, ?, and *
    displayName: Invalid character examples are ?, @, and +
mail: Invalid character examples are [, !, #, $, %, &, *, +, \, /, =, ?, ^, `, {, }, and ]
    mailNickname: Invalid character examples are “, \, /, [, ], :, >, <, ;, and spaces
    proxyAddresses: Invalid character examples are [, !, #, $, %, &, *, +, \, /, =, ?, ^, `, {, }, and ]
userPrincipalName: Invalid character examples are }, {, #, ‘, *, +, ), (, >, <, \, /, =, ?, and `

Well, you agree that nothing easier. So I wrote a simple script that was checking for it, but to my surprise, I noticed that in 3 of the attributes I was getting “$”, “*” and “?” characters. But attributes definitely didn’t have that value, so I replaced “-match” with “-like” in the script.

Well, it was better but still not brill. I  was still getting $ and *. But the ? char was not being reported. I started to think what the hell is wrong felling a bit like:

ezgif.com-crop

I was using “-match” and “-like” all the time in scripts, I was shocked it was messing around with me. So I started to check if there are any other way to compare if char exist in a string. And there it was! Totally forgot about “contains” operator/method!

Changed all my “matches” and “likes” to “-contains” and taaaddaaam 🙂 All worked like a charm!

Answer to question why it might have not worked can be found for example under here:

http://www.tjrobinson.net/?p=109

Tom is telling there that:

-contains is designed to work on arrays, not strings (referred to as a containment operator)
-match looks for a match inside a string and supports regexes
–like looks for a match inside a string and supports wildcards

And here is the thing, my solution was about to check if string, ex. “mail” contains: one of [, !, #, $, %, &, *, +, \, /, =, ?, ^, `, {, }, ] . Syntax was:

$Characters_To_Check = @(“[“, “!”, “#”,”$”, “%”, “&”, “*”, “+”, “\”, “/”, “=”, “?”, “^”, “`”, “{“, “}”, “]”)

$String_To_Check = “$User.mail”

Foreach ($Character in $Characters_To_Check ) {

if ($String_To_Check -match $Character) {Do things}

}

because “match” and “like” operators support regex and wildcards, they were treating these special characters as these. Only contains was working properly in that particular case.

 

Script to check mailbox permissions

Recently I have written a script for getting mailbox permissions.

Script reads permissions and puts it in CSV. I have used a lot from Get-MailboxReport.ps1 script wrote by Paul Cunningham.

Data can be read from mailboxes individually, for specific mailbox server or all mailboxes:

get_mailboxpermissions2

get_mailboxpermissions1

At the end it gives CSV output like:

get_mailboxpermissions3

Script can be found on Technet – link >> TECHNET

Im_helping