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

时间:2017-07-28 17:38:02

标签: json powershell remote-server powershell-remoting

我正在尝试使用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

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望能够将此功能指向计算机列表并返回显示其运行的计算机名称。我已经更新了你的功能,并会给你一个如何使用它的例子。

创建CSV文件,列标题设置为“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
}
}
}
}
}
}