简体   繁体   English

使用 Powershell 从远程服务器获取磁盘空间信息并添加到阵列

[英]Using Powershell to grab disk space info from remote servers and add to array

I've been trying to get some scripts together to automate some of the manual tasks that we still do reporting on.我一直在尝试将一些脚本放在一起,以自动执行我们仍在报告的一些手动任务。 This one I'm trying to coax into connecting to each of the remote servers specified (I can AD link and filter it later), pull disk information, do a basic calculation, some formatting, and then stick it in an array to pull later.这个我试图哄骗连接到指定的每个远程服务器(我可以 AD 链接并稍后过滤它),提取磁盘信息,进行基本计算,一些格式化,然后将其粘贴在一个数组中以供稍后提取.

I'm currently stuck with errors stating that I'm "attempting to divide by 0", and my array returns no data (I'm assuming because of the above". There has to be something small I'm missing. Well, hopefully small. Here's where I've gotten to:我目前遇到错误,指出我正在“尝试除以 0”,并且我的数组不返回任何数据(我假设是因为上述原因)。必须有一些小东西我丢失了。好吧,希望很小。这是我到达的地方:

#Variable listing servers to check. Can convert to a csv, or direct connection to AD 
   using OU's.
   $ServersToScan = @('x, y, z')

   #Blank Array for Report
   $finalReport = @()

   #Threshold Definition %
   $Critical = 5
   $Warning = 15

#Action for each server
foreach ($i in $ServersToScan)
{
Enter-PSSession -ComputerName $i

#Fixed Disk Info Gather
$diskObj = Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 }

#Iterate each disk information - rewrite as foreach ($x in $diskObj) - rewritten 3/31/22
foreach ($diskObj in $diskObj)
    {
        # Calculate the free space percentage
        $percentFree = [int](($_.FreeSpace / $_.Size) * 100)

        # Determine the "Status"
        if ($percentFree -gt $Warning) {
            $Status = 'Normal'
        }
        elseif ($percentFree -gt $Critical) {
            $Status = 'Warning'
        }
        elseif ($percentFree -le $Critical) {
            $Status = 'Critical'
        }

        # Compose the properties of the object to add to the report
        $tempObj = [ordered]@{
            'Computer Name'    = $i
            'Drive Letter'     = $_.DeviceID
            'Drive Name'       = $_.VolumeName
            'Total Space (GB)' = [int]($_.Size / 1gb)
            'Free Space (GB)'  = [int]($_.FreeSpace / 1gb)
            'Free Space (%)'   = "{0}{1}" -f [int]$percentFree, '%'
            'Status'           = $Status
        }

        # Add the object to the final report
        $finalReport += New-Object psobject -property $tempObj
    }

Exit-PSSession
}

return $finalReport

Any insight would be great - thank you very much!!任何见解都会很棒 - 非常感谢!

There are 2 main problems, the first one, as Jeff Zeitlin pointed out, the automatic variable $_ ( $PSItem ) has no effect in a foreach loop, it is effectively $null :有两个主要问题,第一个,正如Jeff Zeitlin指出的那样, 自动变量$_ ( $PSItem )foreach循环中没有效果,它实际上是$null

[int](($null / $null) * 100)

# => RuntimeException: Attempted to divide by zero.

The second problem is your use of Enter-PSSession , which is used exclusively in interactive sessions .第二个问题是您使用Enter-PSSession ,它专门用于交互式会话 For an unattended script you would use Invoke-Command instead, however, in this case we could also rely on Get-CimInstance -ComputerName to query the remote computers ( note that this operation is performed in parallel and does not require a loop over the $serversToScan array ).对于无人参与的脚本,您可以改用Invoke-Command ,但是,在这种情况下,我们还可以依赖Get-CimInstance -ComputerName来查询远程计算机(请注意,此操作是并行执行的,不需要循环遍历$serversToScan数组)。

$ServersToScan = 'x, y, z'

#Threshold Definition %
$Critical = 0.5
$Warning = 0.15

$params = @{
    ClassName    = 'Win32_LogicalDisk'
    Filter       = "DriveType = '3'"
    ComputerName = $ServersToScan
}

$finalReport = Get-CimInstance @params | ForEach-Object {
    $free = $_.FreeSpace / $_.Size
    $status = switch($free) {
        { $_ -gt $Warning  } { 'Normal'; break }
        { $_ -gt $Critical } { 'Warning'; break }
        Default { 'Critical' }
    }
    
    [pscustomobject]@{
        'Computer Name'    = $_.PSComputerName
        'Drive Letter'     = $_.DeviceID
        'Drive Name'       = $_.VolumeName
        'Total Space (GB)' = $_.Size / 1Gb
        'Free Space (GB)'  = $_.FreeSpace / 1Gb
        'Free Space (%)'   = $free.ToString('P0')
        'Status'           = $status
    }
}

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

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