애드센스4



PowerShell로 원격 컴퓨터 정보를 엑셀에 삽입하기 시스템TIP

'Excel로 Windows PowerShell 데이터 내보내기'라는 제목으로 테크넷 매거진에 실린 흥미로운 기사

http://technet.microsoft.com/ko-kr/magazine/dd297620.aspx

엑셀 처리 부분은 Perl 코드와 별반 차이가 없고, 로컬 컴퓨터의 프로세스 목록을 얻어내는 것은 별거 아니지만, PowerShell을 이용해 WMI로 원격 컴퓨터의 프로세스 정보를 얻어낼 수 있다는 점이 흥미롭다.

Perl로 하는 예제 코드 - http://talhatariq.wordpress.com/2006/06/20/a-perl-script-to-list-process-info-through-wmi-local-remote/ - 와 비교해보면 관리자 입장에서 PowerShell이 훨씬 빠르게 작업할 수 있다는 걸 알 수 있다. 물론 본문 하단에 더 짧은 코드가 있긴 하지만... 대신 PowerShell은 VBScript나 Perl에 비해 실행 속도는 느린 듯...

참고:
1. PowerShell 로 워드 문서 만들기 - http://www.microsoft.com/technet/scriptcenter/resources/qanda/may09/hey0513.mspx
2. PowerShell 로 MySQL 사용하기 - http://programming.torensma.net/2009/01/connect_powershell_to_mysql/
3. PowerShell 로 브라우저 컨트롤하기 - http://msdn.microsoft.com/ko-kr/magazine/cc337896.aspx

참고: PowerShell로 원격 서버에 접속하여, 프로세스 리스트를 추출한 후, 프로세스 상태에 따라 Sort 하여 보고서를 만드는 소스
$Number_Of_Rows = 1
$Number_Of_Columns = 4

$objWord = New-Object -comobject word.application
$objWord.Visible = $true
$objDoc = $objWord.Documents.Add()
$objRange = $objDoc.Range()
$objDoc.Tables.Add($objRange,$Number_Of_Rows,$Number_Of_Columns)
$objTable = $objDoc.Tables.item(1)

$objTable.Cell(1,1).Range.Text = "Num"
$objTable.Cell(1,2).Range.Text = "Service Name"
$objTable.Cell(1,3).Range.Text = "Display Name"
$objTable.Cell(1,4).Range.Text = "Service State"

$x = 2
$y = 1
$strComputer = "[target server name]"
Get-WmiObject -query "Select * from Win32_Service" -computername $strComputer -credential [DOMAIN\user] | sort State |
Foreach-Object {
 $objTable.Rows.Add()
 $objTable.Cell($x,1).Range.Text = $y
 $objTable.Cell($x,2).Range.Text = $_.name
 $objTable.Cell($x,3).Range.Text=$_.DisplayName
 $objTable.Cell($x,4).Range.Text=$_.State
 $y++
 $x++
} #end Foreach-Object

$objTable.AutoFormat(13)


참고: VBScript로 위와 동일한 일을 수행하는 예제 (비교해보면 PowerShell의 파이프가 가진 장점이 두드러진다)
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add
Set objWorksheet = objWorkbook.Worksheets(1)

x = 2
strComputer = "[Target Server]"
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSWbemServices = objSWbemLocator.ConnectServer _
    (strComputer, "root\cimv2", "[User]", "[Password]")

Set colServices = objSWbemServices.ExecQuery _
    ("Select * From Win32_Service")

For Each objService in colServices
    objExcel.Cells(x, 2) = objService.Name
    objExcel.Cells(x, 3) = objService.Description
    objExcel.Cells(x, 4) = objService.State
    x = x + 1
Next

Set objRange = objWorksheet.UsedRange
Set objRange2 = objExcel.Range("D2")
objRange.Sort(objRange2)

x = 2
For Each objService in colServices
    objExcel.Cells(x, 1) = x - 1
    x = x + 1
Next

objExcel.Cells(1,1) = "Num"
objExcel.Cells(1,2) = "Service Name"
objExcel.Cells(1,3) = "Display Name"
objExcel.Cells(1,4) = "Service State"

objExcel.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit
objExcel.Cells(1, 1).CurrentRegion.AutoFormat(8)

중요 검색 키워드: Perl WMI


트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://swbae.egloos.com/tb/1905811 [도움말]

덧글

댓글 입력 영역


애드센스7