Powershell - WMI 和 AD 对象
Powershell - WMI & AD Objects
谁能告诉我以下问题的解决方案或更好的方法 - 我想做的是 return 一些关于所选服务器的信息并包含 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
哪个 returns:
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
我发现您有 2 个问题。第一个是 notjustme 指出的那个。您正在尝试将 Get-AdComputer
的输出转换为 WMI 对象...因此出现错误。
在您返回的同一行中,对象的描述为 属性。该行中的两个小更改将使其余代码按预期工作。
$ad = Get-ADComputer $servername -Properties Description -Credential $cred |
Select-Object -<b>Expand</b>Property description
旁注
看看你的问题,看看我做了什么来格式化你的代码块。使用文本框上方的“{}”按钮来创建缩进,将为您节省大量时间。
谁能告诉我以下问题的解决方案或更好的方法 - 我想做的是 return 一些关于所选服务器的信息并包含 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
哪个 returns:
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
我发现您有 2 个问题。第一个是 notjustme 指出的那个。您正在尝试将 Get-AdComputer
的输出转换为 WMI 对象...因此出现错误。
在您返回的同一行中,对象的描述为 属性。该行中的两个小更改将使其余代码按预期工作。
$ad = Get-ADComputer $servername -Properties Description -Credential $cred |
Select-Object -<b>Expand</b>Property description
旁注
看看你的问题,看看我做了什么来格式化你的代码块。使用文本框上方的“{}”按钮来创建缩进,将为您节省大量时间。