2017-07-28 14 views
0

私は、Windows 2008-2016サーバのリストからpowershellを使用してドライブ情報を取得しようとしています。情報を取得するためのコードがありますが、リモートではなく、サーバー名の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 

また、私はちょうどサーバー名

server1 
server2 
server3 

私はこれを行うに800+のサーバーを持っているを示していますtxtファイルを持っています。データを取得したら、結果をダンプするための最良のファイルの種類が不明です。私はjsonファイルを使うのは良いアイデアかもしれないと言われましたが、以前はjsonを使用していませんでした。基本的に私は私がこの順に結果を表示できるように検索可能オブジェクトを作成しようとしています:

DiskModel 
Partition 
FreeSpace 
Size 
+0

'は、Get-WmiObject'に –

答えて

0

私が正しく理解していた場合、あなたがコンピュータのリストで、この機能を指し示すことができるようにしたいと実行されたコンピュータ名を示すreturnを取得します。私はあなたの関数を更新し、それを使用する方法の例を示します。

列ヘッダーを "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の後に、このコードを追加: '$ユーザー名= "管理者"' '$ パスワード=「 ' $クレド=新オブジェクト-Force' 1234'' '$パス=をConvertTo-SecureString -AsPlainText $パスワードSystem.Management.Automation.PSCredential -ArgumentList $ UserName、$ Pass' -scriptblockの前に '-port 1234 - Credential $ Cred'を追加しました。 悪質なユーザー名またはパスワードのエラーが表示されています。これはおそらくadmin acctがローカルアカウントであるためです。ローカル管理者としてコードをログインさせる方法が不明です。 – Keith

関連する問題