Example of Developing a Custom PowerShell Script for Use with the WCI Data Type
In this example, the objective is to collect scheduled tasks information from Windows clients. On newer
systems, Windows conveniently provides the
schtasks.exe
utility to report on scheduled tasks created
either through the Task Scheduler user interface or through use of the
AT
command.
n
Running
schtasks
by itself returns only basic data about tasks.
n
Adding the
/query /v
switches provides additional information, but the formatting is difficult for
automated processing.
n
The
schtasks /query /?
command provides additional possibilities.
n
The option set of
schtasks /query /v /fo:csv
is selected as the source for the data for the
collection script. These options give full details for all tasks in a comma-separated value result set.
PowerShell makes working with tabular result sets from commands easy. A first step for this script is to
run a command similar to:
$schtasks = schtasks /query /v /fo:csv
Since the data returned from
schtasks
includes multiple rows, PowerShell makes the
$schtasks
variable into an array. As such,
$schtasks[0]
represents the first row returned from the command.
Viewing the result set by looking at
$schtasks[n]
shows that that the first line,
$schtasks[0]
, is blank;
$schtasks[1]
contains column names, and
$schtasks[2]
is the first row of task data. The goal, then, is
to parse this data into a structure compatible with VCM’s XML format for return to the Collector.
The Scheduled Tasks script uses the split method of PowerShell strings to separate the columns of the
$schtasks
rows into separate values in arrays. The column names row provides the names to use for
attributes, and the corresponding data from the scheduled task rows provide the values to use for these
attributes.
Once parsed, the XML returned by the script should look something like:
<schtasks>
<taskname1>
<attribute1>Value1</attribute1>
<attribute2>Value2</attribute2>
…
</taskname1>
<taskname2>
<attribute1>Value1</attribute1>
<attribute2>Value2</attribute2>
…
</taskname2>
…
</schtasks>
The
<schtasks>
top-level name is an arbitrary name picked to distinguish the results of this script from
others. A couple of additional challenges must also be overcome with this data, related to column names
returned by the
schtasks
command, and the fact that the
schtasks
command does not include any
unique and repeatable identifier for specific task entries. Details about these challenges are described next.
vCenter Configuration Manager Installation and Getting Started Guide
78
VMware, Inc.
Summary of Contents for VCENTER CONFIGURATION MANAGER 5.3
Page 8: ...vCenter Configuration Manager Installation and Getting Started Guide 8 VMware Inc...
Page 46: ...vCenter Configuration Manager Installation and Getting Started Guide 46 VMware Inc...
Page 158: ...vCenter Configuration Manager Installation and Getting Started Guide 158 VMware Inc...
Page 178: ...vCenter Configuration Manager Installation and Getting Started Guide 178 VMware Inc...
Page 194: ...vCenter Configuration Manager Installation and Getting Started Guide 194 VMware Inc...
Page 204: ...vCenter Configuration Manager Installation and Getting Started Guide 204 VMware Inc...
Page 208: ...vCenter Configuration Manager Installation and Getting Started Guide 208 VMware Inc...
Page 234: ...vCenter Configuration Manager Installation and Getting Started Guide 234 VMware Inc...
Page 264: ...vCenter Configuration Manager Installation and Getting Started Guide 264 VMware Inc...
Page 274: ...274 VMware Inc vCenter Configuration Manager Installation and Getting Started Guide...