Coffee Break 16 – Using Windows PowerShell to Mark and Compare
This time we will take a fingerprint of a specific situation, and then use Diff to compare in order to see if the situation has changed over time or compared to a reference machine.
Coffee Break – Fingerprints and Diff-Checks
Sometimes you want to compare the current situation against a reference to see if the current situation is OK, or to check if something was changed. In this post we will create a reference (a fingerprint), and then use Diff to compare the current situation against this reference. The reference could be running processes, files, permissions, or more or less anything else.
Customer story:
The system admin wants to check that all or all the right client files are in the client folder. So they create a fingerprint of what files should be there. This makes it possible to check if something or somebody modified client files.
A different scenario is where the fingerprint is a platform update, and the Diff-check is about verifying which clients must have their client files updated.
Take a fingerprint of a folder content
See what’s in a folder:
Get-ChildItem ‘C:\Program Files (x86)\Microsoft Dynamics NAV\80\RoleTailored Client’
Save this folder content as a reference fingerprint:
Get-ChildItem ‘C:\Program Files (x86)\Microsoft Dynamics NAV\80\RoleTailored Client’ | Export-Clixml C:\Temp\ReferenceClient.xml
Then keep this file ReferenceClient.xml, or copy it to a client machine, and whenever you need to know if something was changed:
diff -ReferenceObject (Import-Clixml C:\Temp\ReferenceClient.xml) -DifferenceObject (Get-ChildItem ‘C:\CUs\Build41779_Upd9\RoleTailoredClient\program files\Microsoft Dynamics NAV\80\RoleTailored Client’) -Property LastWriteTime, Name
CliXML is a format specific to PowerShell and a very simple way to pipe any output into a structured document. Structured making it better suited to compare.
Other ways to use Diff
You can compare services, compare processes, and compare Dynamics NAV permissions.
Compare Services
Let’s say we want to be able to check that certain services exist and are running. Check services:
Get-Service
Save this situation as a reference fingerprint:
Get-Service | Export-Clixml -path C:\Temp\ReferenceServices.xml
Then at a later time or on a different machine run a compare against this reference. Compare Status and Name:
diff -ReferenceObject (Import-Clixml C:\temp\ReferenceServices.xml) -DifferenceObject (Get-Service) -Property Status,Name
If for example a Dynamics NAV Server service was stopped, then this could be the result:
Status Name SideIndicator
—— —- ————-
Stopped MicrosoftDynamicsNavServer$DynamicsNAV80 =>
Running MicrosoftDynamicsNavServer$DynamicsNAV80 <=
Notice that the cmdlet will show each difference two times. The SideIndicator arrow points to the left if something was only on the left hand side (reference). If it points to the right then it was only on the right hand side.
Compare processes:
Get-Process | Export-Clixml -Path C:\temp\ReferenceProcess.xml
diff -ReferenceObject (Import-Clixml C:\Temp\ReferenceProcess.xml) -DifferenceObject (Get-Process) -Property Name
Compare Dynamics NAV permissions:
Import-Module ‘C:\Program Files\Microsoft Dynamics NAV\80\Service\Microsoft.Dynamics.Nav.Management.dll’
Get-NAVServerPermission dynamicsnav80 -PermissionSetId CASHFLOW | Export-Clixml -path c:\temp\ReferencePermissions.xml
diff -ReferenceObject (Import-Clixml C:\Temp\ReferencePermissions.xml) -DifferenceObject (Get-NAVServerPermission Dynamicsnav80 -permissionsetid CASHFLOW) -Property objectname
Jasminka Thunes, Escalation Engineer Dynamics NAV EMEA
Lars Lohndorf-Larsen, Escalation Engineer Dynamics NAV EMEA
Bas Graaf, Senior Software Engineer Dynamics NAV