PowerShell, Scripting
comment 1

ConvertTo-PowerShell: PowerShell versus “other” scripting examples – Part 3

Yesterday, I mentioned that PowerShell can take care of many important aspects like formatting output, etc and let you focus on the core aspects of what you want to achieve using a PowerShell script. In today’s post I will show an example to demonstrate this.  

In this example, let us first look at a VBScript that gets a list of processes running on a set of remote computers and then we willl look at how PowerShell simplifies that.  

VBScript  

Look at this VBScript. We loop through the contents of a text file, pickup each line (a server name) as strComputer, connect to the computer using WMI, retrieve a list of processes and then finally, iterate through the process object to display list of properties we need. Many steps involved in doing this that require you to have knowledge of many others such as WMI. The output formatting is done by us. Here we just pickup three properties of a process object. Imagine writing up Wscript.Echo statement for all the 20+ object properties. Also, we are not really formatting the output to be in a readable format. Doing that would require some more effort and more lines of code. Now, let us take a look at PowerShell way of doing this.  

PowerShell 

See this..! I need not worry about how do I open a text file and read each line. I need not know what method to use to connect to a remote computer. The out-of-box cmdlets take care of that. Even better, I am not worried about output formatting at all. PowerShell engine itself takes care of that. But this would display several properties of process object in a tabular form. What if I want to select only those properties as I did in the VBScript example. 

Simple, again. There are out-of-box cmdlets to select only the required properties and then show the output in a chosen format. 

This is it for today. I will post another example tomorrow.

Filed under: PowerShell, Scripting

by

Ravikanth is a principal engineer and the lead architect for Microsoft and VMware virtualized and hybrid cloud solutions within the Infrastructure Solutions Group at Dell EMC. He is a multi-year recipient of Microsoft Most Valuable Professional (MVP) award in Windows PowerShell (CDM) and Microsoft Azure. Ravikanth is the author of Windows PowerShell Desired State Configuration Revealed (Apress) and leads Bangalore PowerShell and Bangalore IT Pro user groups. He can be seen speaking regularly at local user group events and conferences in India and abroad.