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

问题描述:

我试图从使用PowerShell的Windows 2008 - 2016服务器列表中获取驱动器信息。我已经获得了获取信息的代码,但只能从本地系统获取信息,而不是远程获取信息,而不是从服务器名称的txt文件获取。以下是我的代码。使用powershell从远程服务器收集数据

$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 
+0

'帮助获取-WmiObject' –

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

创建一个列标题设置为“ComputerName”的CSV文件。无论你喜欢,填写数据列。然后这样做:

$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 
} 
} 
} 
} 
} 
} 
+0

感谢,看起来像它可以工作,但在试图连接到远程服务器,我得到一个错误。我在Param之后添加了以下代码:$ $ UserName =“Admin”' '$ Password ='1234'' System.Management.Automation.PSCredential -ArgumentList $ UserName,$ Pass' 然后,我在-scriptblock之前添加了“-port 1234 - Credential $ Cred”。 我收到错误的用户名或密码错误。这可能是由于管理员帐户是本地帐户。不知道如何使代码作为本地管理员登录。 – Keith