fbpx Automate Windows PowerShell Data-Intensive Tasks | Windows PowerShell, Windows Server | HostingUltraso.com
24/7/365 Support

Automate Windows PowerShell Data-Intensive Tasks

Problem

You want to invoke a simple task on large amounts of data.

Solution

If only one piece of data changes (such as a server name or user name), store the data in a text file. Use the GetContent cmdlet to retrieve the items, and then use the ForeachObject cmdlet (which has the standard aliases foreach and %) to work with each item in that list.

Example 25. Using information from a text file to automate dataintensive tasks

PS >GetContent servers.txt SERVER1 SERVER2 PS >$computers = GetContent servers.txt PS >$computers | ForeachObject { GetWmiObject Win32_OperatingSystem Computer $_ }

SystemDirectory : C:\WINDOWS\system32 Organization : BuildNumber : 2600 Version : 5.1.2600

SystemDirectory : C:\WINDOWS\system32

Example 25. Using information from a text file to automate dataintensive tasks (continued)

Organization : BuildNumber : 2600 Version : 5.1.2600

If it becomes cumbersome (or unclear) to include the actions in the ForeachObject cmdlet, you can also use the foreach

Example 26. Using the foreach scripting keyword to make a looping statement easier to read

$computers = GetContent servers.txt

foreach($computer in $computers)

{

## Get the information about the operating system from WMI

$system = GetWmiObject Win32_OperatingSystem Computer $computer

## Determine if it is running Windows XP

if($system.Version eq "5.1.2600")

{

"$computer is running Windows XP" } }

If several aspects of the data change per task (for example, both the WMI class and the computer name for computers in a large report), create a CSV file with a row for each task. Use the ImportCsv cmdlet to import that data into PowerShell, and then use properties of the resulting objects as multiple sources of related data.

Example 27. Using information from a CSV to automate dataintensive tasks

PS >GetContent WmiReport.csv ComputerName,Class LEEDESK,Win32_OperatingSystem LEEDESK,Win32_Bios PS >$data = ImportCsv WmiReport.csv PS >$data

ComputerName Class

LEEDESK Win32_OperatingSystem LEEDESK Win32_Bios

PS >$data | >> ForeachObject { GetWmiObject $_.Class Computer $_.ComputerName } >>

SystemDirectory : C:\WINDOWS\system32 Organization :

Example 27. Using information from a CSV to automate dataintensive tasks (continued)

BuildNumber : 2600 Version : 5.1.2600

SMBIOSBIOSVersion : ASUS A7N8X Deluxe ACPI BIOS Rev 1009

Manufacturer
: Phoenix Technologies, LTD

Name
: Phoenix AwardBIOS v6.00PG

SerialNumber
: xxxxxxxxxxx

Version
: Nvidia 42302e31

Discussion

One of the major benefits of PowerShell is its capability to automate repetitive tasks. Sometimes, these repetitive tasks are actionintensive (such as system maintenance through registry and file cleanup) and consist of complex sequences of commands that will always be invoked together. In those situations, you can write a script to combine these operations to save time and reduce errors.

Other times, you need only to accomplish a single task (for example, retrieving the results of a WMI query) but need to invoke that task repeatedly for a large amount of data. In those situations, PowerShell’s scripting statements, pipeline support, and data management cmdlets help automate those tasks.

One of the options given by the solution is the ImportCsv cmdlet. The ImportCsv cmdlet reads a CSV file and, for each row, automatically creates an object with prop a CSV that contains a ComputerName and Class header.

Example 28. The ImportCsv cmdlet creating objects with ComputerName and Class properties

PS >$data = ImportCsv WmiReport.csv PS >$data

ComputerName Class

LEEDESK Win32_OperatingSystem LEEDESK Win32_Bios

PS > PS >$data[0].ComputerName LEEDESK

As the solution illustrates, you can use the ForeachObject cmdlet to provide data from these objects to repetitive cmdlet calls. It does this by specifying each parameter name, followed by the data (taken from a property of the current CSV object) that applies to it.

While this is the most general solution, many cmdlet parameters can automatically retrieve their value from incoming objects if any property of that object has the same name. This can let you to omit the ForeachObject and property mapping steps altogether. Parameters that support this feature are said to support Value from pipeline by property name. The MoveItem cmdlet is one example of a cmdlet with parameters that support this, as shown by the Accept pipeline input

Example 29. Help content of the MoveItem showing a parameter that accepts value from pipeline by property name

PS >GetHelp MoveItem Full (...) PARAMETERS

path <string[]> Specifies the path to the current location of the items. The default is the current directory. Wildcards are permitted.

Required? true Position? 1 Default value <current location> Accept pipeline input? true (ByValue, ByPropertyName) Accept wildcard characters? true

destination <string> Specifies the path to the location where the items are being moved. The default is the current directory. Wildcards are permitted, but the result must specify a single location.

To rename the item being moved, specify a new name in the value of Destination.

Required? false Position? 2 Default value <current location> Accept pipeline input? true (ByPropertyName) Accept wildcard characters? True

(...)

If you purposefully name the columns in the CSV to correspond to parameters that take their value from pipeline by property name, PowerShell can do some (or all) of items in bulk.

Example 210. Using the ImportCsv cmdlet to automate a cmdlet that accepts value from pipeline by property name

PS >GetContent ItemMoves.csv Path,Destination test.txt,Test1Directory test2.txt,Test2Directory PS >dir test.txt,test2.txt | Select Name

Name

Example 210. Using the ImportCsv cmdlet to automate a cmdlet that accepts value from pipeline by property name (continued)

test.txt test2.txt

PS >ImportCsv ItemMoves.csv | MoveItem PS >dir Test1Directory | Select Name

Name

test.txt

PS >dir Test2Directory | Select Name

Name

test2.txt

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say