简体   繁体   English

如何将ManagementObjectCollection对象转换为Powershell可读的格式?

[英]How to convert a ManagementObjectCollection object into a format readable by Powershell?

I am trying to retrieve logical disk information from remote machines using Daniele's custom get-wmiobject function due to my need for a timeout parameter. 由于我需要超时参数,因此我尝试使用Daniele的自定义get-wmiobject函数从远程计算机检索逻辑磁盘信息。

The function works perfectly fine, but I also want to store the $results in a variable and loop through it to find drives whose free space has dropped below a certain threshold. 该函数运行正常,但我也想将$ results存储在变量中并循环遍历,以找到其可用空间已降至某个阈值以下的驱动器。

Here is my portion of the code. 这是我的代码部分。 I added a $where parameter to Daniele's function to be attached to the query. 我在Daniele的函数中添加了一个$ where参数以附加到查询中。 His function is otherwise the same. 否则他的功能是相同的。

Function get-wmicustom([string]$computername,[string]$namespace,[string]$class,[int]$timeout=15,[string]$where)
{
    $ConnectionOptions = new-object System.Management.ConnectionOptions
    $EnumerationOptions = new-object System.Management.EnumerationOptions

    $timeoutseconds = new-timespan -seconds $timeout
    $EnumerationOptions.set_timeout($timeoutseconds)

    $assembledpath = "\\" + $computername + "\" + $namespace
    write-host $assembledpath -foregroundcolor green

    $Scope = new-object System.Management.ManagementScope $assembledpath, $ConnectionOptions
    $Scope.Connect()

    $querystring = "SELECT * FROM " + $class + " " + $where
    write-host $querystring

    $query = new-object System.Management.ObjectQuery $querystring
    $searcher = new-object System.Management.ManagementObjectSearcher
    $searcher.set_options($EnumerationOptions)
    $searcher.Query = $querystring
    $searcher.Scope = $Scope

    trap { $_ } $result = $searcher.Get()

    return $result
}

$servers = get-content -Path "c:\ServerMonitoring\servers.txt"
$threshold = 5

foreach($server in $servers)
{
    write-host "Checking server $server..."

    #$drive_information = get-wmiobject -ComputerName $server -query "select Name, FreeSpace from Win32_logicaldisk where MediaType = 12"
    $drive_information = get-wmicustom -ComputerName $server -NameSpace "root\cimv2" -Class Win32_logicaldisk -TimeOut 3 -Where "WHERE MediaType = 12"

    write-host "Drive information collected from $server..."

    foreach($drive in $drive_information)
    {
        $drive_freespace_gb = [Math]::Round($drive.FreeSpace / 1024 / 1024 / 1024, 2)
        $drive_name = $drive.Name

        write-host "Checking drive $drive_name"

        if ($drive_freespace_gb -le $threshold -and $drive_name -notlike "*T:*")
        {
            write-host "Drive $drive_name on $server has only $drive_freespace_gb GB left."
            $wait = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
        }
    }
}

When I run this script from the command line, it gives this error: 当我从命令行运行此脚本时,它出现以下错误:

The '=' operator failed: Invalid Class. '='运算符失败:无效的类。

I understand that this might be due to the $results being returned here is a ManagementObjectCollection .Net object. 我了解这可能是由于在这里返回的$ results是一个ManagementObjectCollection .Net对象。 So I tried converting this to a string by attaching the ToString() method to the Get() method. 因此,我尝试通过将ToString()方法附加到Get()方法将其转换为字符串。 This stopped the error, but the ToString() method simply returns the name of the class the object belongs to, not the actual data. 这停止了​​错误,但是ToString()方法仅返回对象所属的类的名称,而不是实际数据。 I've also tried Get().GetEnumerator().ToString() with a similar result. 我也尝试过Get()。GetEnumerator()。ToString(),结果相似。

How do I go about converting the results into a format readable by Powershell? 如何将结果转换为Powershell可读的格式?

语法可以是:

$SearchKBsResult | select <property you want> | foreach {$_.property you want}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM