다음을 통해 공유


8장 - PowerShell 원격 관리

PowerShell은 원격 컴퓨터에 대해 명령을 실행하는 여러 가지 방법을 제공합니다. 마지막 챕터에서는 CIM cmdlet을 사용하여 WMI를 원격으로 쿼리하는 방법을 살펴보았다. PowerShell에는 기본 제공 ComputerName 매개 변수를 특징으로 하는 몇 가지 cmdlet도 포함되어 있습니다.

다음 예제와 같이 Get-Command 매개 변수와 함께 사용하여 ComputerName 매개 변수를 포함하는 cmdlet을 식별할 수 있습니다.

Get-Command -ParameterName ComputerName
CommandType Name              Version Source                         
----------- ----              ------- ------                         
Cmdlet      Add-Computer      3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Clear-EventLog    3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Connect-PSSession 3.0.0.0 Microsoft.PowerShell.Core      
Cmdlet      Enter-PSSession   3.0.0.0 Microsoft.PowerShell.Core      
Cmdlet      Get-EventLog      3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Get-HotFix        3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Get-Process       3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Get-PSSession     3.0.0.0 Microsoft.PowerShell.Core      
Cmdlet      Get-Service       3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Get-WmiObject     3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Invoke-Command    3.0.0.0 Microsoft.PowerShell.Core      
Cmdlet      Invoke-WmiMethod  3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Limit-EventLog    3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      New-EventLog      3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      New-PSSession     3.0.0.0 Microsoft.PowerShell.Core      
Cmdlet      Receive-Job       3.0.0.0 Microsoft.PowerShell.Core      
Cmdlet      Receive-PSSession 3.0.0.0 Microsoft.PowerShell.Core      
Cmdlet      Register-WmiEvent 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Remove-Computer   3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Remove-EventLog   3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Remove-PSSession  3.0.0.0 Microsoft.PowerShell.Core      
Cmdlet      Remove-WmiObject  3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Rename-Computer   3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Restart-Computer  3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Send-MailMessage  3.1.0.0 Microsoft.PowerShell.Utility   
Cmdlet      Set-Service       3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Set-WmiInstance   3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Show-EventLog     3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Stop-Computer     3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Test-Connection   3.1.0.0 Microsoft.PowerShell.Management
Cmdlet      Write-EventLog    3.1.0.0 Microsoft.PowerShell.Management

Get-ProcessGet-HotFix와 같은 명령은 ComputerName 매개 변수를 포함하지만, 이 방법은 원격 시스템에 대해 명령을 실행하는 데 있어 Microsoft가 권장하는 장기 계획이 아닙니다. ComputerName 매개 변수가 있는 명령을 찾더라도 자격 증명 매개 변수가 부족하여 대체 자격 증명을 지정하기가 어려운 경우가 많습니다. 네트워크 방화벽이 시스템과 원격 컴퓨터 간의 요청을 차단할 수 있으므로 관리자 권한 세션에서 PowerShell을 실행해도 성공이 보장되지는 않습니다.

이 챕터에 설명된 PowerShell 원격 명령을 사용하려면 원격 컴퓨터에서 PowerShell 원격을 사용하도록 설정해야 합니다. cmdlet을 Enable-PSRemoting 실행하여 사용하도록 설정할 수 있습니다.

Enable-PSRemoting
WinRM has been updated to receive requests.
WinRM service type changed successfully.
WinRM service started.

WinRM has been updated for remote management.
WinRM firewall exception enabled.

일대일 원격 제어

대화형 원격 세션이 필요한 경우, 일대일 원격이 적합한 선택입니다. 이 유형의 원격 제어는 Enter-PSSession cmdlet을 통해 제공됩니다.

변수에 도메인 관리자 자격 증명을 $Cred 저장합니다. 이 방법을 사용하면 현재 PowerShell 세션이 활성 상태로 유지되는 한 자격 증명을 한 번 입력하고 명령별로 다시 사용할 수 있습니다.

$Cred = Get-Credential

dc01이라는 도메인 컨트롤러에 대한 일대일 PowerShell 원격 세션을 설정합니다.

Enter-PSSession -ComputerName dc01 -Credential $Cred

PowerShell 프롬프트 앞에 [dc01]가 있습니다. 이 접두사는 dc01이라는 원격 컴퓨터와 대화형 세션에 있음을 나타냅니다. 이제 실행하는 모든 명령은 로컬 컴퓨터가 아닌 dc01에서 실행됩니다.

[dc01]: PS C:\Users\Administrator\Documents>

원격 컴퓨터에 설치된 PowerShell 명령 및 모듈에만 액세스할 수 있습니다. 다른 모듈을 로컬로 설치한 경우 원격 세션에서 사용할 수 없습니다.

일대일 대화형 원격 세션을 통해 연결하면 마치 원격 머신에 직접 앉아 있는 것처럼 느껴집니다.

[dc01]: Get-Process | Get-Member
   TypeName: System.Diagnostics.Process

Name                       MemberType     Definition
----                       ----------     ----------
Handles                    AliasProperty  Handles = Handlecount
Name                       AliasProperty  Name = ProcessName
NPM                        AliasProperty  NPM = NonpagedSystemMemorySize64
PM                         AliasProperty  PM = PagedMemorySize64
SI                         AliasProperty  SI = SessionId
VM                         AliasProperty  VM = VirtualMemorySize64
WS                         AliasProperty  WS = WorkingSet64
Disposed                   Event          System.EventHandler Disposed(Sy...
ErrorDataReceived          Event          System.Diagnostics.DataReceived...
Exited                     Event          System.EventHandler Exited(Syst...
OutputDataReceived         Event          System.Diagnostics.DataReceived...
BeginErrorReadLine         Method         void BeginErrorReadLine()
BeginOutputReadLine        Method         void BeginOutputReadLine()
CancelErrorRead            Method         void CancelErrorRead()
CancelOutputRead           Method         void CancelOutputRead()
Close                      Method         void Close()
CloseMainWindow            Method         bool CloseMainWindow()
CreateObjRef               Method         System.Runtime.Remoting.ObjRef ...
Dispose                    Method         void Dispose(), void IDisposabl...
Equals                     Method         bool Equals(System.Object obj)
GetHashCode                Method         int GetHashCode()
GetLifetimeService         Method         System.Object GetLifetimeService()
GetType                    Method         type GetType()
InitializeLifetimeService  Method         System.Object InitializeLifetim...
Kill                       Method         void Kill()
Refresh                    Method         void Refresh()
Start                      Method         bool Start()
ToString                   Method         string ToString()
WaitForExit                Method         bool WaitForExit(int millisecon...
WaitForInputIdle           Method         bool WaitForInputIdle(int milli...
__NounName                 NoteProperty   string __NounName=Process
BasePriority               Property       int BasePriority {get;}
Container                  Property       System.ComponentModel.IContaine...
EnableRaisingEvents        Property       bool EnableRaisingEvents {get;s...
ExitCode                   Property       int ExitCode {get;}
ExitTime                   Property       datetime ExitTime {get;}
Handle                     Property       System.IntPtr Handle {get;}
HandleCount                Property       int HandleCount {get;}
HasExited                  Property       bool HasExited {get;}
Id                         Property       int Id {get;}
MachineName                Property       string MachineName {get;}
MainModule                 Property       System.Diagnostics.ProcessModul...
MainWindowHandle           Property       System.IntPtr MainWindowHandle ...
MainWindowTitle            Property       string MainWindowTitle {get;}
MaxWorkingSet              Property       System.IntPtr MaxWorkingSet {ge...
MinWorkingSet              Property       System.IntPtr MinWorkingSet {ge...
Modules                    Property       System.Diagnostics.ProcessModul...
NonpagedSystemMemorySize   Property       int NonpagedSystemMemorySize {g...
NonpagedSystemMemorySize64 Property       long NonpagedSystemMemorySize64...
PagedMemorySize            Property       int PagedMemorySize {get;}
PagedMemorySize64          Property       long PagedMemorySize64 {get;}
PagedSystemMemorySize      Property       int PagedSystemMemorySize {get;}
PagedSystemMemorySize64    Property       long PagedSystemMemorySize64 {g...
PeakPagedMemorySize        Property       int PeakPagedMemorySize {get;}
PeakPagedMemorySize64      Property       long PeakPagedMemorySize64 {get;}
PeakVirtualMemorySize      Property       int PeakVirtualMemorySize {get;}
PeakVirtualMemorySize64    Property       long PeakVirtualMemorySize64 {g...
PeakWorkingSet             Property       int PeakWorkingSet {get;}
PeakWorkingSet64           Property       long PeakWorkingSet64 {get;}
PriorityBoostEnabled       Property       bool PriorityBoostEnabled {get;...
PriorityClass              Property       System.Diagnostics.ProcessPrior...
PrivateMemorySize          Property       int PrivateMemorySize {get;}
PrivateMemorySize64        Property       long PrivateMemorySize64 {get;}
PrivilegedProcessorTime    Property       timespan PrivilegedProcessorTim...
ProcessName                Property       string ProcessName {get;}
ProcessorAffinity          Property       System.IntPtr ProcessorAffinity...
Responding                 Property       bool Responding {get;}
SafeHandle                 Property       Microsoft.Win32.SafeHandles.Saf...
SessionId                  Property       int SessionId {get;}
Site                       Property       System.ComponentModel.ISite Sit...
StandardError              Property       System.IO.StreamReader Standard...
StandardInput              Property       System.IO.StreamWriter Standard...
StandardOutput             Property       System.IO.StreamReader Standard...
StartInfo                  Property       System.Diagnostics.ProcessStart...
StartTime                  Property       datetime StartTime {get;}
SynchronizingObject        Property       System.ComponentModel.ISynchron...
Threads                    Property       System.Diagnostics.ProcessThrea...
TotalProcessorTime         Property       timespan TotalProcessorTime {get;}
UserProcessorTime          Property       timespan UserProcessorTime {get;}
VirtualMemorySize          Property       int VirtualMemorySize {get;}
VirtualMemorySize64        Property       long VirtualMemorySize64 {get;}
WorkingSet                 Property       int WorkingSet {get;}
WorkingSet64               Property       long WorkingSet64 {get;}
PSConfiguration            PropertySet    PSConfiguration {Name, Id, Prio...
PSResources                PropertySet    PSResources {Name, Id, Handleco...
Company                    ScriptProperty System.Object Company {get=$thi...
CPU                        ScriptProperty System.Object CPU {get=$this.To...
Description                ScriptProperty System.Object Description {get=...
FileVersion                ScriptProperty System.Object FileVersion {get=...
Path                       ScriptProperty System.Object Path {get=$this.M...
Product                    ScriptProperty System.Object Product {get=$thi...
ProductVersion             ScriptProperty System.Object ProductVersion {g...

원격 컴퓨터 작업을 마치면 cmdlet을 Exit-PSSession 실행하여 원격 세션을 종료합니다.

[dc01]:  Exit-PSSession

일대다 원격 통신

경우에 따라 원격 컴퓨터에서 대화형으로 작업을 수행해야 할 수도 있지만 여러 원격 시스템에서 동시에 명령을 실행할 때 PowerShell 원격 작업이 더 강력해집니다. cmdlet을 Invoke-Command 사용하여 하나 이상의 원격 컴퓨터에서 동시에 명령을 실행합니다.

다음 예제에서는 Windows 시간 서비스의 상태에 대해 세 개의 서버를 쿼리합니다. cmdlet은 Get-Service 스크립트 블록 Invoke-Command내에 배치됩니다. 즉, 각 원격 컴퓨터에서 실행됩니다.

Invoke-Command -ComputerName dc01, sql02, web01 {
    Get-Service -Name W32time
} -Credential $Cred

결과는 역직렬화된 개체로 로컬 세션에 반환됩니다.

Status   Name        DisplayName       PSComputerName
------   ----        -----------       --------------
Running  W32time     Windows Time      web01
Start... W32time     Windows Time      dc01
Running  W32time     Windows Time      sql02

반환된 개체가 역직렬화되는지 확인하려면 출력을 Get-Member에 파이프하세요.

Invoke-Command -ComputerName dc01, sql02, web01 {
    Get-Service -Name W32time
} -Credential $Cred | Get-Member
   TypeName: Deserialized.System.ServiceProcess.ServiceController

Name                MemberType   Definition
----                ----------   ----------
GetType             Method       type GetType()
ToString            Method       string ToString(), string ToString(strin...
Name                NoteProperty string Name=W32time
PSComputerName      NoteProperty string PSComputerName=dc01
PSShowComputerName  NoteProperty bool PSShowComputerName=True
RequiredServices    NoteProperty Deserialized.System.ServiceProcess.Servi...
RunspaceId          NoteProperty guid RunspaceId=5ed06925-8037-43ef-9072-...
CanPauseAndContinue Property     System.Boolean {get;set;}
CanShutdown         Property     System.Boolean {get;set;}
CanStop             Property     System.Boolean {get;set;}
Container           Property      {get;set;}
DependentServices   Property     Deserialized.System.ServiceProcess.Servi...
DisplayName         Property     System.String {get;set;}
MachineName         Property     System.String {get;set;}
ServiceHandle       Property     System.String {get;set;}
ServiceName         Property     System.String {get;set;}
ServicesDependedOn  Property     Deserialized.System.ServiceProcess.Servi...
ServiceType         Property     System.String {get;set;}
Site                Property      {get;set;}
StartType           Property     System.String {get;set;}
Status              Property     System.String {get;set;}

대부분의 메서드는 역직렬화된 개체에서 누락됩니다. 이러한 개체가 라이브가 아니므로 메서드가 없습니다. 원격 컴퓨터에 대해 명령을 실행할 때 개체 상태의 불활성 스냅샷입니다. 예를 들어 더 이상 필요한 메서드에 액세스할 수 없으므로 역직렬화된 개체를 사용하여 서비스를 시작하거나 중지할 수 없습니다.

그렇다고 해서 .와 같은 Stop()Invoke-Command메서드를 사용할 수 없다는 의미는 아닙니다. 키는 원격 세션 내에서 메서드를 호출해야 한다는 것입니다.

보여 주려면 메서드를 원격으로 호출하여 Stop() 세 개의 원격 서버에서 Windows 시간 서비스를 중지합니다.

Invoke-Command -ComputerName dc01, sql02, web01 {
    (Get-Service -Name W32time).Stop()
} -Credential $Cred

Invoke-Command -ComputerName dc01, sql02, web01 {
    Get-Service -Name W32time
} -Credential $Cred
Status   Name        DisplayName       PSComputerName
------   ----        -----------       --------------
Stopped  W32time     Windows Time      web01
Stopped  W32time     Windows Time      dc01
Stopped  W32time     Windows Time      sql02

이전 챕터에서 설명한 것처럼 작업을 수행하는 데 사용할 수 있는 cmdlet이 있는 경우 메서드를 직접 호출하는 대신 사용하는 것이 좋습니다. 예를 들어, 서비스를 중지할 때 Stop-Service cmdlet을 사용하고 Stop() 메서드는 사용하지 마십시오.

이전 예제에서 메서드는 Stop() 점을 만드는 데 사용됩니다. 일부 사람들은 PowerShell 원격 기능에서 메서드를 사용할 수 없다고 잘못 믿습니다. 로컬 세션으로 반환된 역직렬화된 개체에 대해 메서드를 호출할 수는 없지만 원격 세션 내에서 호출할 수 있습니다.

PowerShell 세션

이전 섹션의 마지막 예제에서는 cmdlet을 사용하여 Invoke-Command 두 개의 명령을 실행했습니다. 이 시나리오로 인해 두 개의 별도 세션이 설정되고 중단되었습니다. 각 명령에 대해 하나씩.

CIM 세션과 마찬가지로 영구 PowerShell 세션을 사용하면 각 명령에 대해 새 세션을 만드는 오버헤드 없이 원격 컴퓨터에 대해 여러 명령을 실행할 수 있습니다.

이 챕터 DC01, SQL02 및 WEB01에서 작업 중인 세 컴퓨터 각각에 대한 PowerShell 세션을 만듭니다.

$Session = New-PSSession -ComputerName dc01, sql02, web01 -Credential $Cred

이제 변수를 $Session 사용하여 해당 메서드를 호출하여 Windows 시간 서비스를 시작한 다음 서비스 상태를 확인합니다.

Invoke-Command -Session $Session {(Get-Service -Name W32time).Start()}
Invoke-Command -Session $Session {Get-Service -Name W32time}
Status   Name        DisplayName       PSComputerName
------   ----        -----------       --------------
Running  W32time     Windows Time      web01
Start... W32time     Windows Time      dc01
Running  W32time     Windows Time      sql02

대체 자격 증명을 사용하여 세션을 만든 후에는 각 명령에 대해 해당 자격 증명을 다시 지정할 필요가 없습니다.

사용을 마치면 세션을 삭제해야 합니다.

Get-PSSession | Remove-PSSession

요약

이 장에서는 단일 원격 컴퓨터에서 명령을 직접 실행하고 일대다 원격을 사용하여 여러 시스템에서 명령을 실행하는 등 PowerShell 원격의 기본 사항을 알아보았습니다. 또한 동일한 원격 컴퓨터에 대해 여러 명령을 실행할 때 영구 PowerShell 세션을 사용할 때의 이점에 대해서도 살펴보아야 합니다.

검토

  1. PowerShell 원격을 사용하도록 설정하려면 어떻게 해야 합니까?
  2. 원격 컴퓨터로 대화형 세션을 시작하는 데 사용하는 PowerShell 명령은 무엇인가요?
  3. 각 명령으로 컴퓨터 이름을 지정하는 대신 PowerShell 원격 세션을 사용하면 어떤 이점이 있나요?
  4. 일대일 대화형 원격 시나리오에서 PowerShell 세션을 사용할 수 있나요?
  5. cmdlet에서 반환된 개체와 원격 컴퓨터 Invoke-Command에서 동일한 cmdlet을 실행할 때 반환되는 개체 간의 차이점은 무엇인가요?

참고문헌

다음 단계

9장에서는 재사용 가능한 PowerShell 함수를 작성하는 방법을 알아봅니다. 함수 디자인, 매개 변수, 파이프라인 입력, 오류 처리 및 한 라이너 및 스크립트를 신뢰할 수 있는 도구로 전환하기 위한 모범 사례를 살펴봅니다.