Ezine 183 PowerShell Get-Content
The ability to ‘Get-Content’ is useful for itself; moreover, this cmdlet illustrates how easily PowerShell deals with reading from text files. This ezine also demonstrates looping, a classic job for automation via scripting, and a task that PowerShell delivers with deceptive ease.
- This Week’s Secret
- This Week’s Mission
- 1) Pure Get-Content – List Names in a Text File
- 2) A Reminder of WMI’s Role in this Mission
- 3) Putting it All Together: Completing the Mission
♣
This Week’s Secret
In VBScript dealing with files was never straightforward. However, with PowerShell it’s so effortless that you may not realize that the get-Content cmdlet opens and closes as part of its job description.
Incidentally, in PowerShell the mirror image of get-Content would be out-File, that is where you append a command to save the output of a script into a named file. To use get-Content or out-File all you need is the path there is no need for any file open or file close commands because PowerShell takes care of them automatically.
Get-Content is a simple enough cmdlet to understand. What it does is fetch data information stored in files, for example a list of names from a text file. Just playing with get-Content leads to questions such as ‘Why bother’, or ‘What’s the point?’ I hope that your answer to such questions will give you a PowerShell technique that you can incorporate in bigger more complex scripts.
This Week’s Mission
This Week’s Mission is to employ PowerShell and WMI for checking the status of the operating system’s services. The role of get-Content is merely to read a list of hostnames, as a result we can run the WMI code against remote machines. For ease of learning, and so that you could re-cycle the building blocks, I have broken down the mission into three self-contained tasks.
In order to understand my goal let us isolate the PowerShell commands that query the local computer to see which services are running. Once that works we want to extend the scope of the script so that it queries other machines on your network.
Reading lines from a text file is a classic job for a loop. In PowerShell we can initiate the loop with a ‘Foreach’ statement. Inside that loop we use get-Content to read the names of the computers held in a named text file.
Guy Recommends: The Free IP Address Tracker (IPAT) 
Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges.
For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker
Pre-requisites
- Create a text file, make a note of the path as well as the filename.
- Another trivial point, you need to type a list of hostnames corresponding to the computers you wish to interrogate.
- Disable the firewall on both the source computer, where the script executes, and also on the destination machine(s) which we wish to interrogate.
I don’t want to stray too far from the main objective which is to master get-Content, but as a separate mini-project you could obtain a list of computers which are active on your network by using WMI’s Win32_PingStatus. However, for now we will assume that you have a list of computers safely stored in a file called NetMachines.txt.
1) Pure Get-Content – List Names in a Text File
The only purpose of this script is to list the contents of a file line-by-line.
# PowerShell script to list each line of a file
$File = "E:\PowerShell\Loops\NetworkMachines.txt"
get-Content $File | foreach-Object { $_
}
Note 1: All we want to achieve in this example is to practise our get-Content technique. Observe how the variable $File controls the source of the text.
Note 2: Foreach-Object provides the loop. The contents of each cycle are controlled by $_, the resulting output is merely a list of names.
2) A Reminder of WMI’s Role in this Mission
In real life this is the portion of the script that you would amend in order to tackle a specific problem.
# WMI script to list services which are running on local machine
$Wmi=get-wmiobject win32_service -filter "state=’Running’"
$Wmi | Ft name, state, status -auto
Note 1: This is just a simple WMI script whose class is win32_service, just for fun we have filtered only those services that are running.
Note 2: To gain perspective, and ideas, you could try:
get-wmiobject win32_service -filter "name=’spooler’ "
Note 3: The tiny vertical | tells PowerShell to output the contents of $WMI into the second part of that line.
3a) Problems of ‘Remoting’
Running scripts on remote machines has never been easy. Firewalls and administrative credentials mean that access to another machine is unlikely to be straight forward. While PowerShell version 2.0 has much better ‘Remoting’ abilities, here we are dodging some of PowerShell 1.0’s limitations by using WMI’s -computername parameter.
Even with everything in our favour, please humour me and make a crucial configuration change to your machines. Just to get started, I advise that you suspend normal ‘best practice’ and disable the firewalls on both the source (server) and the remote (client) machines. If the script works then you could investigate other options such as selectively opening ports, and using -credential. (Incidentally, adding Impersonate and Credential to scripts rarely works for me.)
3b) Putting it All Together: Completing the Mission
If you remember, our mission was to create a PowerShell script, which ran WMI commands against a list of network machines.
# PowerShell script to check running services on a list of computers
# Author: Guy Thomas
# Version 1.2 March 2009 tested on PowerShell v 1.0
clear-Host
$File = "E:\PowerShell\Loops\NetworkMachines.txt"
get-Content $File | foreach-object {
$Wmi=get-wmiobject win32_service -filter "state=’Running’" -computername $_
"Computer $_ "; $Wmi | Ft name, state, status -auto
}
Note 1: This single script combines the tasks of the two previous scripts. Observe how ‘foreach’ loops through the list of computer names controlled by the $File variable. The WMI class win32_service retrieves services that are running.
Note 2: Ft means format-Table. I have chosen just three properties, name of the service, its state (running) and status.
The Content Family of Cmdlets
You may have noticed from, ‘help ac -full’, that PowerShell supports a family of verbs that act upon the noun ‘Content’.
Add-Content
Clear-Content
Get-Content
Set-Content
There is also Out-File, a very useful command to save results to disk, rather than write to screen.
Summary of PowerShell’s get-Content
If you are coming from scripting languages such as VBScript, you may not be aware of how easy it is to read from text files in PowerShell. Get-Content not only reads the data line-by-line, but also deals with opening and closing the named file automatically.
Guy Recommends: Tools4ever’s UMRA
Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.
It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.
If you like this page then please share it with your friends
See more Microsoft PowerShell tutorials
• PowerShell Tutorials • Methods • Cmdlets • PS Snapin • Profile.ps1 • Exchange 2007
• Command & Expression Mode • PowerShell pipeline (|) • PowerShell ‘where‘ • PowerShell ‘Sort’
• Windows PowerShell Modules • Import-Module • PowerShell Module Directory
If you see an error of any kind, do let me know. Please report any factual mistakes, grammatical errors or broken links.