2015-02-23 64 views
0

有人可以请我解决以下问题或更好的方法来接近它 - 我所要做的就是返回有关所选服务器的一些信息,并包含AD Description Field:Powershell - WMI&AD对象

$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

删除变量$ ad之前的[wmi]。 – notjustme 2015-02-23 11:07:30

+0

耶稣....也许我盯着那太久了! – Richard 2015-02-23 11:25:16

回答

1

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

在你回来的同一行上,并带有描述的对象属性。该行中的两个小改动将使其余代码按预期工作。

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

旁注

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

+0

啊,很好,我甚至没有注意到。 – notjustme 2015-02-23 13:16:58

+0

感谢您的反馈意见以及代码块的建议。干杯马特 – Richard 2015-02-23 14:20:14