Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按组织单位获取机器正常运行时间的Powershell脚本_Powershell - Fatal编程技术网

按组织单位获取机器正常运行时间的Powershell脚本

按组织单位获取机器正常运行时间的Powershell脚本,powershell,Powershell,我已经找到了一些脚本,但是没有任何有效的,并且尝试将它们结合起来,失败得很惨。我找不到任何东西可以解释如何使我找到的那些做我想要的,所以这里是我的尝试,但它不工作,所以我想知道是否有人可以帮助我让它工作?我删除了标识域信息 $wmi = Get-WmiObject -ComputerName $pc -Class Win32_OperatingSystem 因此,您有几个步骤: 获取机器列表 $list = Get-ADComputer -Filter 'Name -like "*"'

我已经找到了一些脚本,但是没有任何有效的,并且尝试将它们结合起来,失败得很惨。我找不到任何东西可以解释如何使我找到的那些做我想要的,所以这里是我的尝试,但它不工作,所以我想知道是否有人可以帮助我让它工作?我删除了标识域信息

    $wmi = Get-WmiObject -ComputerName $pc -Class Win32_OperatingSystem

因此,您有几个步骤:

  • 获取机器列表

    $list = Get-ADComputer -Filter 'Name -like "*"' -SearchBase 'OU=Computers,DC=example,DC=com'
    
  • 迭代列表

    $results = foreach ($pc in $list.Name)
    {
    
  • 抓取
    WMI
    信息

        $wmi = Get-WmiObject -ComputerName $pc -Class Win32_OperatingSystem
    
  • 计算正常运行时间

        $uptime = $wmi.ConvertToDateTime($wmi.LocalDateTime) - $wmi.ConvertToDateTime($wmi.LastBootUpTime)
    
  • 在可以使用的集合中输出信息

        [pscustomobject]@{
            ComputerName = $pc
            Uptime       = $uptime.Hours
        }
    }
    
  • 减去两个
    datetime
    对象将得到一个
    timespan
    ,因此需要计算出所需信息的粒度(秒、分钟等)。此过程的最终结果是
    $results
    中的对象数组,其中包含
    ComputerName
    Uptime