2014. 9. 24. 16:55

  • Powershell의 cmdlet은 명령 프롬프트에 비하여 기능이 좋은 것 중에 하나가 파이프라인을 사용하는 것이다. 파이프라인은 앞의 결과를 입력 값으로 받아서 처리하는 것을 말한다.
  • 보통 우리는 다른 컴퓨터의 자원의 정보를 보기 위해서는 [Get-Process -ComputerName pc1]으로 사용한다. 여러 대의 컴퓨터를 지정하기 위해서는 -ComputerName pc1, pc2, pc2로 하든가, 아니면 -ComputerName (Get-Content c:\computers.txt)로 사용한다.
  • 그리고 어떠한 프로세스를 중지하기 위해서 Stop-Process -Name notepad 라고 하고, 여러 개의 프로세스를 중지하려면 Stop-Process -Name notepad, mspaint, wordpad로 하거나 Stop-Process -Name (GC c:\list.txt)로 하면 된다.
  • 만약 회사의 모든 컴퓨터에 대하여 조사를 하고자 한다면 어떻게 할까? 그것은 바로 Active Directory에 소속 컴퓨터의 목록을 이용하면 된다. 그러면 기본적으로 AD의 컴퓨터 목록을 먼저 불러 오고 그 결과를 Get-Process의 입력으로 처리하면 된다. 이 때 사용하는 것이 바로 Pipeline이다.
  • 다음과 같이 일반적인 파이프라인 작업을 해 보자.
    • Get-Process | Select-Object -Property vm, cpu | Format-Table -Autosize
    • Get-Process | Sort-Object -Property cpu -Descending
    • Get-Process | Sort-Object -Property cpu -Descending | Select-Object -First 5
    • Get-Service | Where-Object {$_.status -eq “stopped”}
    • Get-Process -Name notepad, wordpad | Stop-Process
      (일단 2개의 프로세스가 실행 중인지를 먼저 확인한 후 중지시키는 것이다.)
    • gwmi win32_bios -ComputerName (Get-ADcomputer -Filter * | Select-Object -ExpandProperty name)
      (이것은 좀 특이한 경우이다. -ComputerName에 괄호를 하고 Get-Content c:\computers.txt로 사용하지 않고 AD의 컴퓨터 이름을 가지고 온 경우이다.)
  • 이제는 수많은 컴퓨터 이름을 활용해 보자. 즉, AD의 구성원 목록을 이용하자는 것이다. Get-Process, Stop-Process 등을 사용할 때 앞에서 먼저 컴퓨터 목록을 확인하여 이용하고자 한다. 이럴 때는 반드시 도움말을 참고하는 습관을 들여야 한다. 즉, 다음과 같이 한 번 도움말을 참고하여 본다.
    • Help Get-Process -Parameter ComputerName        
    • Help Get-Service -Parameter Name
    • Help Stop-Service -Parameter Name
    • Help Stop-Process -Parameter Name
    • Help Gwmi -Parameter ComputerName
  • 위의 결과를 보면 다음과 같다.

Pipeline의 입력을 받는 cmdlet

매개변수

파이프라인 입력 적용 여부

Get-Process

ComputerName

ByPropertyName

Get-Service / Stop-Service

Name

ByValue, ByPropertyName

Stop-Process

Name

ByPropertyName

Get-WmiObject

ComputerName

false

  • 여기서 보면 Gwmi의 -ComputerName은 다른 cmdlet의 결과값을 입력 값으로 받아서 처리할 수 없음을 알 수 있다.
    그리고 Get-Service 및 Stop-Service의 -Name은 ByValue(일반 문자열 값) 및 ByPropertyName(앞 명령어의 Property 이름과 동일한 값)을 사용해야 한다.
    마지막으로 Get-Process 및 Stop-Process의 -Name은 반드시 앞 명령어의 Property 이름과 동일한 것만을 사용해야 한다는 것이다. 이것을 주의해야 한다.
  • 만약 Get-Adcomputer -Filter *의 결과를 보면 Name이라는 Property(속성)은 있지만 뒤에서 사용할 Get-Process의 -ComputerName과 다르다. 이럴 때는 꼭 기존의 Name을 이용하여 ComputerName을 생성해주어서 처리해야 한다.
  • 아래 예제를 한 번 본다.
    • Get-Adcomputer -Filter * (성공)
    • Get-Adcomputer -Filter * | Get-Process (실패)
    • Help Get-Process -Parameter computername (파이프라인 입력 적용여부:ByPropertyName)
    • Get-Adcomputer -Filter * | Select-Object @{Label=”ComputerName”;Expression={$_.name}}(computername이라는 object에 컴퓨터 목록이 나타남)
    • Get-Adcomputer -Filter * | Select-Object @{Label=”ComputerName”;Expression={$_.name}} | Get-Process (성공)
    • Get-Adcomputer -Filter * | Select-Object @{Label=”ComputerName”;Expression={$_.name}} | Get-WmiObject Win32_BIOS (실패)
      (이것이 실패한 이유는 Get-WmiObject의 -ComputerName은 파이프라인을 사용할 수 없기 때문이다.)
    • gwmi win32_bios -ComputerName (Get-ADcomputer -Filter * | Select-Object -ExpandProperty name) (성공)
출처 : http://cloudsns.wordpress.com/category/microsoft/powershell/page/9/

Posted by pegasuss