使用 powershell 从远程服务器收集数据

Data collection from remote servers using powershell

我正在尝试使用 powershell 从 windows 2008 - 2016 服务器列表中获取驱动器信息。我有获取信息的代码,但只能从我的本地系统获取,不能远程获取,也不能从服务器名称的 txt 文件获取。下面是我的代码。

$Computers = @()

Function Get-DriveInfo {

  Get-WmiObject Win32_DiskDrive | ForEach-Object {
  $disk = $_
  $partitions = "ASSOCIATORS OF " +
                "{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " +
                "WHERE AssocClass = Win32_DiskDriveToDiskPartition"
  Get-WmiObject -Query $partitions | ForEach-Object {
    $partition = $_
    $drives = "ASSOCIATORS OF " +
              "{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " +
              "WHERE AssocClass = Win32_LogicalDiskToPartition"
    Get-WmiObject -Query $drives | ForEach-Object {
      New-Object -Type PSCustomObject -Property @{
        Disk        = $disk.DeviceID
        DiskSize    = '{0:d} GB' -f [int]($disk.Size / 1GB)
        DiskModel   = $disk.Model # Unique for Physical, VM, and SAN
        Partition   = $partition.Name
        RawSize     = '{0:d} GB' -f [int]($partition.Size / 1GB)
        DriveLetter = $_.DeviceID
        VolumeName  = $_.VolumeName
        Size        = '{0:d} GB' -f [int]($_.Size / 1GB)
        FreeSpace   = '{0:d} GB' -f [int]($_.FreeSpace / 1GB)
      }
    }
  }
}
}


$Computers += Get-DriveInfo $ComputerName
Write-Output $Computers

我还有一个只列出服务器名称的 txt 文件

server1
server2
server3

我有 800 多台服务器可以执行此操作。获得数据后,我不确定将结果转储到哪种文件最好。我被告知使用 json 文件可能是个好主意,但我以前没有使用过 json。本质上,我试图制作一个可搜索的对象,以允许我按以下顺序显示结果:

DiskModel
Partition
FreeSpace
Size

如果我的理解正确,您希望能够将此函数指向计算机列表并获得 return,其中显示了它 运行 所反对的计算机名称。我已经更新了您的功能,并将举例说明如何使用它。

创建一个 CSV 文件,其中列 header 设置为 "ComputerName"。用您喜欢的数据填充该列。然后做类似的事情:

$Computers = Import-CSV -Path .\Computers.csv
Get-DriveInfo -Computername $Computers

如果您只是想试一试更新后的功能,请尝试以下操作:

get-driveinfo -Computername TestComputer1,TestComputer2

应该会为您提供所需的输出。至于将它存储在哪里,这实际上取决于您的用例。您可以通过管道传输到 Export-CSV 并制作电子表格。或者真的很喜欢并考虑将这些值放入 SQL 数据库中。

更新函数:

Function Get-DriveInfo {
[CmdletBinding()]
Param (
    # Enter a ComputerName
    [Parameter(Mandatory=$false,
               Position=0,
               ValueFromPipeline=$true,
               ValueFromPipelineByPropertyName=$true)] 
    [string[]]$ComputerName = "localhost")

Foreach ($Computer in $ComputerName) {
Invoke-Command -ComputerName $Computer -ScriptBlock {
Get-WmiObject Win32_DiskDrive | ForEach-Object {
 $disk = $_
 $partitions = "ASSOCIATORS OF " +
            "{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " +
            "WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Get-WmiObject -Query $partitions | ForEach-Object {
$partition = $_
$drives = "ASSOCIATORS OF " +
          "{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " +
          "WHERE AssocClass = Win32_LogicalDiskToPartition"
Get-WmiObject -Query $drives | ForEach-Object {
  $obj = New-Object -Type PSCustomObject -Property @{
    Disk        = $disk.DeviceID
    DiskSize    = '{0:d} GB' -f [int]($disk.Size / 1GB)
    DiskModel   = $disk.Model # Unique for Physical, VM, and SAN
    Partition   = $partition.Name
    RawSize     = '{0:d} GB' -f [int]($partition.Size / 1GB)
    DriveLetter = $_.DeviceID
    VolumeName  = $_.VolumeName
    Size        = '{0:d} GB' -f [int]($_.Size / 1GB)
    FreeSpace   = '{0:d} GB' -f [int]($_.FreeSpace / 1GB)
  }
write-output $obj
}
}
}
}
}
}