2014-12-26 3 views
1

다음 스크립트를 사용하여 원격 서버에서 디스크 사용 통계를 수집합니다. 스크립트가 읽는. \ ServerIPs.txt 파일에서 IP가 아닌 호스트 이름을 표시하도록 출력을 얻으려고합니다. get-wmiobject Win32_LogicalDisk에서 "SystemName"검색을 시도했지만 표시 할 이름을 가져올 수 없습니다. 호스트 이름이 호스트 IP 주소가 아닌 보고서에 표시되도록하려면 무엇을 추가해야합니까?Powershell 디스크 사용 보고서

$erroractionpreference = "SilentlyContinue" 
$a = New-Object -comobject Excel.Application 
$a.visible = $True 

$b = $a.Workbooks.Add() 
$c = $b.Worksheets.Item(1) 

$c.Cells.Item(1,1) = "Server Name" 
$c.Cells.Item(1,2) = "Drive" 
$c.Cells.Item(1,3) = "Total Size (GB)" 
$c.Cells.Item(1,4) = "Free Space (GB)" 
$c.Cells.Item(1,5) = "Free Space (%)" 

$d = $c.UsedRange 
$d.Interior.ColorIndex = 19 
$d.Font.ColorIndex = 11 
$d.Font.Bold = $True 

$intRow = 2 

$colComputers = get-content ".\ServerIPs.txt" 
foreach ($strComputer in $colComputers) 
{ 
    $colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter "DriveType = 3" 
    foreach ($objdisk in $colDisks) 
    { 
    $c.Cells.Item($intRow, 1) = $strComputer.ToUpper() 
    $c.Cells.Item($intRow, 2) = $objDisk.DeviceID 
    $c.Cells.Item($intRow, 3) = "{0:N0}" -f ($objDisk.Size/1GB) 
    $c.Cells.Item($intRow, 4) = "{0:N0}" -f ($objDisk.FreeSpace/1GB) 
    $c.Cells.Item($intRow, 5) = "{0:P0}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size) 
    $intRow = $intRow + 1 
    } 
} 
+1

당신이를 얻기 위해 노력했다 무엇 name을 검색하기 위해서 어디에서나'$ objDisk.SystemName'이 보이지 않습니다'Get-WmiObject' 호출에'-Property DeviceID, SystemName, Size, FreeSpace'를 추가해보십시오. –

+0

당신은'[System. NetDns] :: GetHostByAddress ("xxxx") 어떤 이유로 wmi가 작동하지 않는 경우 dns에서 호스트 이름을 얻으려고합니다. – Paul

+0

둘 중 하나 또는 둘 다 대답 수 있습니다 – Matt

답변

1

도움 주셔서 감사합니다. @BaconBits. 또한 출력에 날짜 열을 추가했습니다.

$erroractionpreference = "SilentlyContinue" 
$a = New-Object -comobject Excel.Application 
$a.visible = $True 

$b = $a.Workbooks.Add() 
$c = $b.Worksheets.Item(1) 

$c.Cells.Item(1,1) = "Server Name" 
$c.Cells.Item(1,2) = "Drive" 
$c.Cells.Item(1,3) = "Total Size (GB)" 
$c.Cells.Item(1,4) = "Free Space (GB)" 
$c.Cells.Item(1,5) = "Free Space (%)" 
$c.Cells.Item(1,6) = "Date" 

$d = $c.UsedRange 
$d.Interior.ColorIndex = 19 
$d.Font.ColorIndex = 11 
$d.Font.Bold = $True 

$intRow = 2 

$colComputers = get-content ".\ServerIPs.txt" 
foreach ($strComputer in $colComputers) 
{ 
    $colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter "DriveType = 3" 
    foreach ($objdisk in $colDisks) 
    { 
    $c.Cells.Item($intRow, 1) = $objDisk.SystemName 
    $c.Cells.Item($intRow, 2) = $objDisk.DeviceID 
    $c.Cells.Item($intRow, 3) = "{0:N0}" -f ($objDisk.Size/1GB) 
    $c.Cells.Item($intRow, 4) = "{0:N0}" -f ($objDisk.FreeSpace/1GB) 
    $c.Cells.Item($intRow, 5) = "{0:P0}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size) 
    $c.Cells.Item($intRow, 6) = Get-Date 
    $intRow = $intRow + 1 
    } 
} 
0

많은 사람들이 다른 답변에 표시된 코드를 사용할 수도 있기 때문에 Excel을 적절하고 완벽하게 닫으려면 COM 참조를 해제해야합니다. 내 자신의 테스트에서 Excel 용 변수를 제거하면 Excel.exe를 열어 둘 수있는 참조가 남아 있지 않음을 확인할 수 있습니다 (예 : ISE에서 디버깅하는 경우).

위 작업을 수행하지 않으면 작업 관리자를 보면 Excel이 여전히 실행중인 경우가 있습니다. 일부 경우에는 많은 복사본이 표시 될 수 있습니다.

이는 COM 객체가 "런타임 호출 가능 래퍼"에 싸여 방법과 관련이있다 여기

사용해야 골격 코드입니다.

$excel = New-Object -ComObject Excel.Application 
$excel.Visible = $true 
$workbook = $excel.Workbooks.Add() 
# or $workbook = $excel.Workbooks.Open($xlsxPath) 

# do work with Excel... 

$workbook.SaveAs($xlsxPath) 
$excel.Quit() 
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) 
# no $ needed on variable name in Remove-Variable call 
Remove-Variable excel 
관련 문제