Powershell - WMI&广告对象

时间:2015-02-23 10:45:24

标签: powershell powershell-v3.0

有人可以告诉我以下问题的解决方案或更好的方法吗 - 我要做的就是返回有关所选服务器的一些信息并包含AD Description字段:

$cred = Get-Credential -Credential "test_user"
$servername = "servername"

[wmi]$os = Get-WmiObject -Class win32_operatingsystem -ComputerName $servername -Credential $cred
[wmi]$cs = Get-WmiObject -Class win32_computersystem -ComputerName $servername -Credential $cred
###[wmi]$ad = Get-ADComputer $servername -Properties Description -Credential $cred | Select-Object -Property description

 [hashtable]$osProperties = @{
    'Description' = $ad;
    'OSVersion'=$os.version;
    'OSBuild'=$os.buildnumber;
    'SPVersion'=$os.servicepackmajorversion;
    'Model'=$cs.model;
    'Manufacturer'=$cs.manufacturer;
    'RAM'=$cs.totalphysicalmemory / 1GB -as [int];
    'Sockets'=$cs.numberofprocessors;
    'Cores'=$cs.numberoflogicalprocessors;
    'SystemType'=$cs.SystemType}

$osproperties

返回:

Manufacturer                   VMware, Inc.                                                                                                                                                                          
RAM                            4                                                                                                                                                                                     
OSVersion                      6.1.7601                                                                                                                                                                              
SystemType                     x64-based PC                                                                                                                                                                          
SPVersion                      1                                                                                                                                                                                     
Cores                          2                                                                                                                                                                                     
Model                          VMware Virtual Platform                                                                                                                                                               
OSBuild                        7601                                                                                                                                                                                  
Sockets                        2

但如果我取消选择行get-adcomputer,我会收到以下错误:

Cannot convert value "@{description=PROD - Portsmouth - VM - W2K8R2 Monitoring Server}" to type "System.Management.ManagementObject". Error: "Cannot convert the "@{description=PROD - Portsmouth - VM - W2K8R2 
Monitoring Server}" value of type "Selected.Microsoft.ActiveDirectory.Management.ADComputer" to type "System.Management.ManagementObject"."
At line:12 char:2
+  [wmi]$ad = Get-ADComputer $servername -Properties Description -Credential $cred ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

1 个答案:

答案 0 :(得分:1)

我看到了2个问题。首先是notjustme指出的那个。您正在尝试将Get-AdComputer的输出转换为WMI对象...因此出错。

在您返回的同一行上,使用描述属性对象。该行中的两个小变化将使其余代码按预期工作。

$ad = Get-ADComputer $servername -Properties Description -Credential $cred | 
    Select-Object -ExpandProperty description

旁注

查看您的问题,看看我为格式化代码块做了些什么。使用文本框上方的“{}”按钮创建缩进,可以节省大量时间。