Posts

Showing posts from January, 2018

New cmdlets in CPolydorou.General v2.9.0

There is a newer version of my CPolydorou.General PowerShell module that contains two new cmdlets, Remove-Duplicate and Compare-ObjectByProperty.

Let me guide you through the usage of these cmdlets.

We'll start with Compare-ObjectByProperty. This cmdlet compares two objects using the values on their properties. Let's take the following csv file as example:

Property1Property2Property3abcabdefgefg
Using the Import-Csv cmdlet we are going to save it to the "data" variable for later use:

PS C:\> $data = Import-Csv -Path .\data.csv

PS C:\> $data

Property1 Property2 Property3
--------- --------- ---------
a         b         c
a         b         d
e         f         g
e         f         g
If we compare the third and the fourth object using the "-eq" operator we get that the objects are not equal which is correct since those are different objects.

PS C:\> $data[2] -eq $data[3]
False
However, this is not what we are look…

The CPolydorou.IIS PowerShell module!

Recently I've published another of my PowerShell modules, this time it's CPolydorou.IIS.

This module contains function that have been proven very helpful when I was dealing with IIS web servers and websites.

Let's take a quick look on the functions in this module!

The first three functions are related to application pools and more particularly getting the application pools, restarting them and getting their worker processes:


Get-IISApplicationPool
Get the application pools on the local or remote servers.Restart-IISApplicationPool
Restart one or more application pools on the local or remote serverGet-IISApplicationPoolWorkerProcesses
Get the worker process of an application pool The other two functions are: Get-WebConnections
Get the connections to a web siteSearch-IISSiteLog
Search the IIS logs (or any other log file in text format) for one or multiple servers in parallel Although there are examples provided in the module help, I will provide more details on the above functions a…

Getting the Password Value from PSCredential objects

PowerShell PSCredential objects are probably the best way to use credentials within PowerShell and protect the password. There's a catch however when it comes to the need to have the password saved in the object in plain text format in order to use it where the object is not acceptable as input. Take for example a connection to a SQL server where a connection string is required. Below is the way to extract the password.

We'll start of by creating a PSCredential object to use as example:

PS C:\> $cred = Get-Credential

PS C:\Users\cpolydorou> $cred

UserName Password
-------- --------
testuser System.Security.SecureString
As you may see on the output of the command, the "Password" property is an object of type SecureString.

Fortunatelly, the PSCredential object has a method that can provide the plain text value of the password.

By using the GetNetworkCredential method, we have access to the username and password in plain tex…

LogArchiving script: Updates on version 1.0.6

Hello fellow administrators,

The latest version of my LogArchiving script (1.0.6) that has just been published contains a new feature where the result of the each task can be embedded in the subject of the email report. This is very helpful in case you have lots of servers and you need to check the result with a quick look or a rule on the messages.

A new property named "EmailSubjectResult" has been added to each task in the configuration file. This can be either "append", "prepend" or empty.

Using "append" will add the result of the task to the end of subject, "prepend" will add the result to the begginng of the subject whilst leaving this field empty will not alter the subject of the message at all.

Please note that if you decide to update your scripts to this version, you need to update the configuration files as well!!!

Also, do not solely rely on the messages for monitoring your tasks. The script also writes to the application log…

PowerShell and MS-DOS variables

The first question that comes to mind after reading the title is "What do MS-DOS variables have to do with PowerShell???".

Well, although you may be working solely with PowerShell - just like myself - you will eventually come across a variable in a configuration file, registry key etc. that is going to contain an MS-DOS formatted variable.

Let's take the physical path of an IIS web site for example:

PS C:\> Get-Website -Name "Default Web Site" | % physicalpath
%SystemDrive%\inetpub\wwwroot

PS C:\>
In order for this value to be usable within PowerShell, we'll probably need to expand the "SystemDrive" variable.

Fortunately, .NET has the appropriate tool for us, the "ExpandEnvironmentVariables" function.

Invoking the above function with a string that contains a variable will return a new string with the variable extended.

We are going to use the website's physical path once more and this time we'll expand the result:

PS C…

Getting the Object Type in PowerShell

First post of the year and in a while and I would like to talk about the types of the objects in PowerShell.

Much like C#, each object has a type in PowerShell. This type is proven to be very helpful when developing functions and cmdlets since it provides information about the object we are dealing with.

For example you may have a variable that holds the value "1" but it can be of type string, integer, double and others. The way to differentiate is to examine the type of the variable.

There are may ways to get the type of a PowerShell object but first, let's get the newest event in the application log to use as example:

PS C:\> $obj = Get-EventLog -LogName Application -Newest 1

PS C:\> $obj

Index Time         EntryType   Source InstanceID Message
----- ----         ---------   ------ ---------- -------
3882  Jan 07 20:39 Information ESENT  916        svchost (3356,G,0) The beta ...
The first way to get the type of the object is to use the "GetType" me…