Saturday, February 4, 2023

Finding Installed Applications (Get-ItemProperty vs. Get-WMIObject)

 Greetings everyone!

Forgive my absence.  I am working on my AZ-900 certification and there is a lot to learn!

This week I was given a list of servers.  I was tasked with getting all the applications installed on each of those servers.  So, I donned my PowerShell battle armor and faced the challenge.

First, I used the following code:
get-itemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-object DisplayName, DisplayVersion, Publisher, InstallDate | FT -AutoSize

This did give me a list of applications that had an Uninstall registry entry.  However, I know there were other programs that were not on the list.  So, I decided to use the following code:
Get-WmiObject -Class Win32_Product | Select-object Name | FT -AutoSize

It did give me more applications than the get-itemProperty code, but it took a lot longer for the list to appear.  The Get-itemProperty took 65 milliseconds.  The Get-WmiObject took 1 minute 22 seconds.

One article said to use Get-CIMInstance instead of Get-WmiObject in order to get a quicker response.  Get-CIMInstance took 1 minute and 37 seconds.  Oh, well.

I am still looking for a reason why the itemProperty command runs faster than the WmiObject command.  But, my main point is that there are two different ways to list what is installed on a computer, and that Get-WmiObject lists more installed apps than the other command.

As always, post a comment if you have any questions, or if you know why one is faster than the other.  I would appreciate it.  😀

Until next time...

Monday, January 16, 2023

Reviewing the Fundamentals

 Greetings everyone!

I haven't posted in a while due to the holidays, the flu, and taking some much needed time off.  But, I am back and I want to talk about how, sometimes, revisiting the fundamentals is beneficial.

I downloaded Jeff Hicks' "The PowerShell Practice Primer" (https://leanpub.com/psprimer).  For $29.95, it is well worth the spend.  Why?  Well, let me tell ya...

As you all know, I love PowerShell!  It has saved me a lot of time and headache, especially when managing Active Directory.  So, I want to keep my skills sharp and I thought Jeff's book would do that for me.  The exercises in the book showed me that I often overthink a solution to a problem.  For example, Exercise 11 asks you to get all of the 'get' commands in the PSReadLine module.  This was something I didn't know how to do.  After fumbling with some of the cmdlets, I looked at the solutions and found the following:
Get-Command -module PSReadLine -Verb get

Another example, I am used to putting output into a variable and then doing a ForEach loop to get the information I need.   Exercise 19 asks to create an XML file for all processes running under my account name.  My solution was:

$processes = get-process -IncludeUserName
$me_process = Foreach ($process in $processes)
{
  If ($process.UserName -like "*me*")
  {
    $process.ProcessName;$process.UserName
  } 
}
$me_process | Export-Clixml C:\users\trekr\documents\exer19.xml 

As you can see, I used a variable and a ForEach loop...not efficient what so ever.

The solution was much simpler:

Get-Process -IncludeUserName | where-object {$_.Username -eq "$($env:USERDOMAIN)\$($env:USERNAME)" } | Export-Clixml -Path myprocs.xml

Pipelines make things simpler.  I had forgotten that.

We should be creating efficient code.  Which is why I decided to practice my skills.  I feel that people who script, no matter their experience, benefit from stepping back and examining their coding style.  Any little change may be what is needed to improving their coding.

I would be interested in your opinion on this.  So, please feel free to comment.

Until next time...

NOTE: Jeff Hicks gave me permission to use the examples from his book.  He's a good guy.