Here’s a WMI script using PowerShell that will perform a remote query to a Windows Host (or lists of hosts) and return processor data from the WMI win32_processor class. As with previous sample scripts, we have built-in help and you can process any number of host names either via the command-line or using an external text file.  If other data is needed, the script can be easily modified to utilize other WMI clases.

This script also demonstrates how to pass user credentials via WMI and can handle either local or domain account logins. Also note that the user password is collected via interactive input using the -AsSecureString parameter to make sure that it is properly obscured from view.

One word of caution for using domain account login credentials: If processing a list of hostnames, make certain that you have keyed in your password correctly. An incorrect password attempted on multiple hosts could (or at least should) trigger a domain account-lockout as it will appear that someone is attempting to break-in to a host.

?View Code POWERSHELL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
$psScriptName = "get-wmidata.ps1"
$psScriptAuth = "Kahuna at PoshTips.com"
$psScriptDate = "11/24/2009"
##############################################################################
## Get-Wmidata.ps1 - powershell script template
##
## Original Script by Kahuna at PoshTips.com - 11/24/2009
##
## Maintenance (most recent updates at top):
## Date        By   Comments
## ----------  ---  ----------------------------------------------------------
## 11/24/2009  KPS  new script
##
##############################################################################
$scriptUsage = @"
==============================================================================
 SCRIPT  : $psScriptName
 AUTHOR  : $psScriptAuth
 VERSION : $psScriptDate
==============================================================================
 PURPOSE:
    powershell script do WMI query on remote host
 
 USAGE:
 
    Get-Wmidata [hostname(s) | filename(s)] -User=[-?|-H]
 
    - From the PowerShell command-line, invoke the script, passing any combination
      of hostnames and filenames (text file(s) containing a hostname listing
    - Any combinations and number of arguments can be used
    - if an argument is a filename:
         - each line in the file will processed as a hostname
         - any lines starting with `# will be treated as comments and ignored
    - if an argument is not a filename:
         - the argument will processed as a hostname
  FLAGS:
    -USER  = A local user account or fully-qualified (domain) user account
    -? or -H  = Display this help
 
  EXAMPLES:
              ./Get-Wmidata server1 hostlist.txt -USER=administrator
 
              ./Get-Wmidata server1 hostlist.txt -USER=poshtips\kahuna
 
==============================================================================
"@
 
Set-PSDebug -Trace 0
$script:recCount = 0
$script:usr = ""
$script:pwd = ""
$script:DOMACCT = 1
$script:LOCACCT = 2
$script:acctType = 0
$script:credentials = ""
 
function ShowUsage([string]$message=""){
    $message
    $scriptUsage
    exit
    }
 
function CurrentAcctType {
    switch ($script:acctType){
        $script:LOCACCT
            {return "LOCAL"; break;}
        $script:DOMACCT
            {return "DOMAIN"; break;}
        default
            {return "????"; break;}
        }
    }
 
function GetPassword(){
    $prompt = ([string]::format("Enter the password for {0} account ""{1}"" ",(CurrentAcctType),$script:usr))
    $script:pwd = read-host -assecurestring $prompt
    if ($script:acctType -eq $script:DOMACCT) {
        $script:credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $script:usr, $script:pwd
        }
    }
 
function GetWmiData($hostname) {
    if ($script:acctType -eq $script:LOCACCT) {
        $localusername = [string]::Format("{0}\\{1}",$hostname,$script:usr)
        $script:credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $localusername, $script:pwd
        }
    $x = gwmi win32_processor -computer $hostname -credential $script:credentials
    if ($x) {
        return([string]::Format("{0},{1},{2},{3},{4}",
          ++$script:recCount,$hostname,$x.count,$x[0].maxclockspeed,$x[0].extclock))
        }
    else {
        return (",,,")
        }
    }
 
######################################################
## MAIN
######################################################
if ($args.count -ge 2)
    {
    # Process command-line flags
    foreach ($item in $args)
        {
        if ($item -like "-USER=*"){
            $script:usr = ($item.split("=")[1])
            if ($script:usr -like "*\*"){$script:acctType = $script:DOMACCT}
            else {$script:acctType = $script:LOCACCT}
            }
        elseif ($item -like "-*")
            {
            switch ($item){
                "-H" {ShowUsage;}
                "-?" {ShowUsage;}
                default {
                    if ($item -like "-USER=*") {
                        ShowUsage "ERROR: $item is invalid!";
                        }
                    }
                }
            }
        }
    if ($script:usr -eq ""){
        ShowUsage "ERROR: -USER flag is required"
        }
 
    GetPassword
 
    # Write the CSV header
    "LN,Host,CPUCnt,CPUSpeed,ClockSpeed"
    # Process non-flag arguments
    foreach ($item in $args) {
        if ($item -notlike "-*") {
 
            # if argument is filename: process file contents
            if (test-path($item)){
                foreach ($hostname in get-content $item){
                    $hostname = $hostname.trim()
                    if ($hostname -notlike "#*") {
                        GetWmiData $hostname
                        }
                    }
                }
 
            # else treat as hostname
            else{
                GetWmiData $item
                }
            }
        }
    }
else {
    ShowUsage "ERROR: This script requires arguments"
    }

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

   
© 2011 Posh TipsSuffusion theme by Sayontan Sinha

Page optimized by WP Minify WordPress Plugin