得到错误wmi powershell op_Division:String

时间:2014-09-17 15:39:48

标签: powershell wmi-query

im trayng运行此脚本以发送包含服务器信息的邮件但我收到此错误:

Method invocation failed because [System.Object[]] does not contain a method named 'op_Division'.
At C:\Users\admin-ran\Desktop\Startup-SendMailTEST.ps1:22 char:1
+ $compinfo = New-Object PSObject -property @{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Division:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

$compinfo = @()
$computerSystem = get-wmiobject Win32_ComputerSystem 
$computerBIOS = get-wmiobject Win32_BIOS 
$computerOS = get-wmiobject Win32_OperatingSystem 
$computerCPU = get-wmiobject Win32_Processor 
$computerHDD = Get-WmiObject Win32_LogicalDisk -Filter drivetype=3 
$colItems = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IpEnabled = TRUE"
# Build objects
$compinfo = New-Object PSObject -property @{ 
'PCName' = $computerSystem.Name 
'Manufacturer' = $computerSystem.Manufacturer 
'Model' = $computerSystem.Model 
'SerialNumber' = $computerBIOS.SerialNumber 
'RAM' = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) 
'HDDSize' = "{0:N2}" -f ($computerHDD.Size/1GB) 
'HDDFree' = "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) 
'CPU' = $computerCPU.Name 
'OS' = $computerOS.caption 
'SP' = $computerOS.ServicePackMajorVersion 
'User' = $computerSystem.UserName 
'BootTime' = $computerOS.ConvertToDateTime($computerOS.LastBootUpTime) 
'IP_Address' = [string]$colItems.IpAddress 
'MAC_Address' = [string]$colItems.MacAddress 
'Default_Gateway' = [string]$colItems.DefaultIpGateway 
'DNS_Domain' = $colItems.DNSDomain 
'DHCP_Enabled' = $colItems.DHCPEnabled 
}

$compinfo | select -Property HDDFree ,HDDSize ,Ram ,OS ,CPU ,SP ,IP_Address,Mac_Address ,BootTime ,DHCP_Enabled

1 个答案:

答案 0 :(得分:2)

我打赌你有多个硬盘,所以$ computerHDD是一个数组,所以$ computerHDD.size和$ computerHDD.FreeSpace,所以你需要做一个ForEach($ HDD in $ computerHDD并在那里插入一个循环(或沿着这些行的东西)。

以下是我将脚本更新为:

$compinfo = @()
$computerSystem = get-wmiobject Win32_ComputerSystem 
$computerBIOS = get-wmiobject Win32_BIOS 
$computerOS = get-wmiobject Win32_OperatingSystem 
$computerCPU = get-wmiobject Win32_Processor 
$computerHDD = Get-WmiObject Win32_LogicalDisk -Filter drivetype=3 
$colItems = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IpEnabled = TRUE"
# Build objects
ForEach($HDD in $computerHDD){
    $compinfo += New-Object PSObject -property @{ 
        PCName = $computerSystem.Name 
        Manufacturer = $computerSystem.Manufacturer 
        Model = $computerSystem.Model 
        SerialNumber = $computerBIOS.SerialNumber 
        RAM = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) 
        HDDSize = "{0:N2}" -f ($HDD.Size/1GB) 
        HDDFree = "{0:P2}" -f ($HDD.FreeSpace/$HDD.Size) 
        CPU = $computerCPU.Name 
        OS = $computerOS.caption 
        SP = $computerOS.ServicePackMajorVersion 
        User = $computerSystem.UserName 
        BootTime = $computerOS.ConvertToDateTime($computerOS.LastBootUpTime) 
        IP_Address = [string]$colItems.IpAddress 
        MAC_Address = [string]$colItems.MacAddress 
        Default_Gateway = [string]$colItems.DefaultIpGateway 
        DNS_Domain = $colItems.DNSDomain 
        DHCP_Enabled = $colItems.DHCPEnabled 
    }
}
$compinfo | select -Property HDDFree ,HDDSize ,Ram ,OS ,CPU ,SP ,IP_Address,Mac_Address ,BootTime ,DHCP_Enabled

我在我的机器上运行它并且运行没有错误。