Skip to main content

Posts

Why are Objects so Important?

We are in day 1 of a PowerShell class here in Phoenix.  I’ve been keeping an eye on PowerShell.com and PowerShell.org to try and find questions that we can help with.  Right before we started our topic on saving and retrieving objects from disk, an IT pro asked a question concerning getting and processing information about their virtual machines. Get-VM | Out-File C:\ps\VMS.txt $VMs = Get-Content -Path C:\PS\VMS.txt ForEach ( $VM in $VMs ) {     $VM | Select-Object -Property Name     Get-VM $VM | Get-DatastoreCluster } Do you see the problem here? The IT Pro is correctly getting his virtual machine objects.  The problem is he is saving them to a text file.  Let’s look at the text file. PS C:\> Get-Content -Path C:\ps\VMS.txt Name  State   CPUUsage(%) MemoryAssigned(M) Uptime             Status   ...

Find the index of an Item in an Enumeration

Well, I’m back in the warmth of Phoenix.  We had a lot of great questions from our PowerShell class in Toronto last week.  While we were discussion .NET, a question popped up about finding the index number of a value of an enumeration.  Sure, no problem. Function Get-EnumIndex { Param (     [ parameter (Mandatory= $true )]     $Enum ,         [ parameter (Mandatory= $true )]     [ String ]     $Value ) [ enum ]::GetValues( $Enum )|         foreach { [ PSCustomObject ]@{ValueName = $_ ; IntValue = [ int ] $_   }   } |         Where ValueName -eq $Value <# .SYNOPSIS Searches and enumeration for the index number of the provided value. .DESCRIPTION Searches and enumeration for the index number of the provided value. .PARAMETER Enum The enu...

Who Changed my LCM Configuration?

Tonight I am in Toronto delivering PowerShell.  Yes, this is a far cry from the flip flops that I was wearing in Phoenix just a few days ago.  This class is a little more difficult for reasons other than the snow fall that is coming over the next 24 hours. Instead of 5 days, I only have 3 days to complete this class.  I have to be a little bit picky on the amount of depth that I can go into.  One area that I did not want to skimp on, is the creation of custom properties with Select-Object. The creation of a hash table is used in other areas of PowerShell, so I took them through the full lesson.  We left off there this evening.  Tomorrow, I’m going to show them a more real life application to show the importance of this skill that many do not care to learn. At least not at first. Who changed the configuration of my Local Configuration Manager on one of my servers?  As we begin our shift into the DSC world, good practices may be a bit lagging....

Finding Your DSC Event Log Messages

Happy Friday!!! Often when I am writing large amounts of code, I like to enter items into an event log of some type.  For those of you who have been sitting in my Advanced PowerShell classes where we use SAPIEN PowerShell Studio, you may have noticed that I will sometimes include a tabbed page where I have a log file.    In DSC we have an Analytic log that we can send messages to using a DSC resource called Log, PS C:\WINDOWS\system32> Get-DscResource -Name Log ImplementedAs   Name   ModuleName                     Version    Properties         -------------   ----   ----------                     -------    ----------     ...

Using Calculated Properties with Select-Object

From time to time, I take a good look at how I am teaching PowerShell just to make sure that I am providing the most effective delivery.  That may be why when someone audits the class, they say they learn new material.  Besides, if I did not make changes, I would get bored. Over the last few months I’ve been teaching how to create calculated properties with Select-Object a little differently.  This is a key skill to have so I need to make sure that we get it right.  Here is an explanation of how to create a custom property. A custom property simply adds an additional property to an object in the PowerShell pipeline.  It does not change the original object, just the copy of it in the pipeline. Take a look at the properties of Get-Date. PS C:\> Get-date | Select * DisplayHint : DateTime DateTime    : Thursday, February 11, 2016 7:46:10 PM Date        : 2/11/2016 12:00:00 AM Day  ...

Using Invoke-CIMMethod

In my PowerShell class last week, we took a lab a bit further.  This lab involved using the old WMI commands.  Well, that is no fun.  I was providing my answers to the lab using the new CIM cmdlets. I did this once before, but I forgot to update my answers.  You see, there is a difference in how you provide information to Invoke-CIMMethod and Invoke-WMIMethod .  The objective was to change the Start Mode of the WinRM service.  To do this in WMI:        Get-WmiObject –Class Win32_Service –Filter "Name='WinRM'" |            Invoke-WmiMethod –Name ChangeStartMode –Argument 'Automatic' You would think that you could do the same with the CIM cmdlets, but PowerShell did not like this.       Get-CimInstance -ClassName Win32_Service –Filter "Name='WinRM'" |         Invoke-CimMethod -MethodName ChangeSta...