PowerShell 查询 SCCM信息

来源:互联网 发布:怎么查询淘宝行业数据 编辑:程序博客网 时间:2024/04/27 18:50

Exchange, SCOM, SCVMM都提供了PowerShell cmdlet来进行命令行管理, 但是SCCM 没有。 但是我们有SCCM SDK, SCCM也通过

WMI 提供了大量类, 我们还有.net 库. 下面是一个例子, 在 SCCM 2007, SMS 2003上都可以运行.

 

将以下脚本copy paste到一个后缀 ps1  的文本文件中, 就可以直接在PowerShell中执行了。执行方法:

 

在PowerShell命令提示符下:  ./ xxxx.ps1   主站点服务器名称   站点名称      如果不加这两个参数,只显示语法,不实际执行.
-----------------------------------------------------------------------------------------------------------------
param($SiteSvr,$SiteCode)

 

 #Echo syntax if script is run with no parameters

     if ($SiteSvr -eq $null) {

        Write-Host ""
        Write-Host "SYNTAX:";
        Write-Host "This script has 2 required parameters.";
        Write-Host ""
        Write-Host "-SiteSvr: Name of ConfigMgr 2007 Site Server";
        Write-Host "Ex: sccmsvr.fqdn.local";
        Write-Host ""
        Write-Host "-SiteCode: Three character SCCM Site Code";
        Write-Host "Ex: ‘NYC";
        Write-Host ""

 exit;      }


$query = New-Object System.Management.ObjectQuery

$query.QueryString = "Select * from SMS_R_System"

$s = New-Object System.Management.ManagementObjectSearcher($query)

$s.Scope.Path = "//" + $SiteSvr + "/root/sms/site_" + "$SiteCode"


$s.Get() | Select NetbiosName, SystemGroupName, IPAddresses, ClientVersion, OperatingSystemNameandVersion
---------------------------------------------------------------------------------------------------------------

 

很简单,就是连接到主站点服务器执行 "Select * from sms_R_System"查询.  当然可以换成其它WQL语句, 需要注意的是WQL无

法使用join, 所以不得不从 SMS_R_System 先找出ResourceID, 然后用ResourceID来搜索其它信息, 例如:

将上面的  $s.Get() | Select NetbiosName,......  这里改一下 $s.Get() | Select NetbiosName,  ResourceID.... 得到ResourceID, 例如 35920, 使用如下查询语句和输出语句, 得到在这个计算机上安装的软件.

 "Select * from SMS_G_system_Add_Remove_Programs where Resourceid = '35920' and DisplayName not like '%KB%' and DisplayName not like '%driver%' "

 

$s.Get() | Select DisplayName, Publisher, InstallDate | Format-table


 

原创粉丝点击