2017-11-09 19 views
1

私持っている私は、ディスクに関する情報を収集することを可能にする、次のPowerShellスクリプトスクリプトが起動されたドメインのWindowsサーバ上の&ボリューム:別のコマンドの中にコマンドを組み込む方法は?

 
SRV01 Connection Error 
SRV02 Connection Error 

PSComputerName DeviceID  Size /GB Free /GB Free % 
-------------- -------- ------------ --------- ------- 
SERVER03  C:    125,51 105,59 84,1 
SERVER04  C:    24,83  7,38 29,7 
SERVER05  E:    14,65  7,36 50,2 
SERVER06  C:    49,66  29,28  59 

:私は次のような結果を得る

$ErrorActionPreference = 'SilentlyContinue' 

Get-ADComputer -Filter 'OperatingSystem -like "*Server*"' -Properties * | 
    Select-Object Name | 
    ForEach-Object { 
     if (Test-Connection $_.Name -Count 1) { 
      Get-WmiObject -Class Win32_LogicalDisk -ComputerName $_.Name -Filter "DriveType=3" | 
       Select-Object PSComputerName, DeviceID, 
        @{Name="Size /GB";Expression={[math]::Round($($_.Size/1GB), 2)}}, 
        @{Name="Free /GB";Expression={[math]::Round($($_.Freespace/1GB), 2)}}, 
        @{Name="Free %";Expression={[math]::Round($($_.Freespace/$_.Size)*100, 1)}} 
     } else { 
      Write-Nost $_.Name " Connection Error" 
     } 
    } | 
    sort PSComputerName | 
    Format-Table -AutoSize 

私は各サーバのためにOSと共に追加の列を追加したいと思う。 この列を「PSComputerName」列の後の2番目の位置にしたいとします。どうすればこの結果を得ることができますか? Get-WmiObject -Class Win32_LogicalDisk ...Get-WmiObject Win32_OperatingSystem | Select-Object captionを追加して入れ子になったコマンドを使うと思いますが、使用する構文と別のコマンドでコマンドを組み込む方法がわかりません。

答えて

0

-properties *を使用しないでください。このスクリプトでは、必要としない1つの読み込まれたプロパティをすべて取得します。

Get-ADComputeroperatingsystemです。

テストされていない:

Get-ADComputer -Filter 'OperatingSystem -like "*Server*"' -Properties OperatingSystem | ForEach-Object { 
    $OS = $_.OperatingSystem 
    If (Test-Connection $_.Name -Count 1 -Quiet){ 
     Get-WmiObject -Class win32_logicalDisk -ComputerName $_.Name -Filter "DriveType=3" | 
     Select-Object pscomputername, @{Name="OS";Expression={$OS}} ,DeviceID,    
      @{Name="Size /GB";Expression={[math]::Round($($_.size/1GB), 2)}}, 
      @{Name="Free /GB";Expression={[math]::Round($($_.freespace/1GB), 2)}}, 
      @{Name="Free %";Expression={[math]::Round($($_.Freespace/$_.Size)*100, 1)}} 
    } 
    else { 
     Write-host $_.Name " Connection Error" 
    } 
} | sort pscomputername | Format-Table -AutoSize 
関連する問題