Ezine 183 PowerShell Get-ContentEzine 183 PowerShell Get-ContentThe 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 SecretIn 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 MissionThis 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. ˆ Pre-requisites
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 FileThe 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 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 MissionIn 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 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:
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 MissionIf 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 clear-Host 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 CmdletsYou may have noticed from, 'help ac -full', that PowerShell supports a family of verbs that act upon the noun 'Content'.
Add-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-ContentIf 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. If you are looking for handy network utilities, try some of the free downloads at Tools4Ever See more Microsoft PowerShell tutorials• Windows PowerShell Home • Introduction • Cmdlets • Exchange 2007 • Profile.ps1 • $_.Pipeline If you see an error of any kind, do let me know. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit. *
| |||||