Computer >> 컴퓨터 >  >> 시스템 >> Windows Server

Enter-PSSession 완벽 가이드: 대화형 PowerShell 원격 세션으로 원격 명령 실행하기

Enter-PSSession cmdlet을 사용하면 원격 컴퓨터와 영구적인 대화형 PowerShell 세션을 설정할 수 있습니다. 명령 프롬프트에 입력하는 모든 명령은 원격 컴퓨터에서 실행됩니다. 이 글에서는 Enter-PSSession의 핵심 기능과 Windows 10/11, Windows Server 2022/2019/2016 환경의 컴퓨터를 원격으로 관리하는 방법을 자세히 알아보겠습니다.

Enter-PSSession의 동작 원리

Enter-PSSession cmdlet은 PowerShell Remoting(PSRemoting) 스택을 기반으로 작동합니다. PSRemoting은 WS-Management(Web Services for Management) 프로토콜과 WinRM 서비스(Windows Remote Management)를 기반으로 하며, 컴퓨터 간 트래픽은 프로토콜 수준에서 암호화됩니다. 필요에 따라 PSRemoting WinRM 트래픽에 SSL 암호화를 추가로 활성화할 수도 있습니다. 또한 NTLM, Kerberos 등 다양한 인증 방법을 지원합니다.

기본 사용법: 원격 세션 연결하기

가장 간단한 방법은 연결할 컴퓨터 이름(ComputerName 옵션)만 지정하는 것입니다. 원격 컴퓨터에 연결하려면 다음 명령을 실행합니다.

Enter-PSSession hq-srv01.woshub.com

Enter-PSSession 완벽 가이드: 대화형 PowerShell 원격 세션으로 원격 명령 실행하기

현재 사용자에게 원격 호스트에 연결할 권한이 있다면, 원격 컴퓨터의 대화형 셸에 바로 연결됩니다.

연결 시 사용자 자격 증명을 직접 지정할 수도 있습니다.

Enter-PsSession –ComputerName hq-srv01.woshub.com –Credentials woshub\maxbak

또는 다음과 같이 자격 증명을 변수에 담아 사용할 수 있습니다.

$creds = Get-Credential
Enter-PSSession -ComputerName hq-srv01 -Credential $creds

원격 세션 확인 방법

연결에 성공하면 PowerShell 프롬프트 시작 부분에 원격 컴퓨터 이름이 대괄호로 표시됩니다([hq-srv01.woshub.com]). 이 표시를 통해 현재 로컬 셸인지 원격 셸인지 쉽게 구분할 수 있습니다.

원격에서 실행된 모든 명령의 출력 결과는 로컬 콘솔에 표시됩니다. hostname 명령을 실행하여 실제로 원격 컴퓨터에서 명령이 실행되고 있는지 확인해 보세요.

이 대화형 명령 프롬프트에서는 사용자 권한 범위 내에서 모든 명령을 실행할 수 있습니다. 예를 들어 PowerShell로 Windows 네트워크 설정을 조회해 보겠습니다.

Get-NetIPConfiguration

원격 컴퓨터의 DNS 설정도 변경할 수 있습니다.

Set-DNSClientServerAddress –InterfaceIndex 6 –ServerAddresses 192.168.13.4, 192.168.100.4

Enter-PSSession 완벽 가이드: 대화형 PowerShell 원격 세션으로 원격 명령 실행하기

대화형 원격 셸 세션을 종료하려면 Exit-PSSession 또는 exit를 실행합니다. 그러면 프롬프트가 원래 상태로 돌아가며 로컬 PowerShell 콘솔로 복귀합니다.

Enter-PSSession 완벽 가이드: 대화형 PowerShell 원격 세션으로 원격 명령 실행하기

PsExec 대체: 더 이상 외부 도구가 필요 없습니다

과거 관리자들은 원격 Windows 컴퓨터에서 대화형 명령 프롬프트를 실행하기 위해 주로 PsExec 도구를 사용했습니다. 하지만 Enter-PSSession이 등장한 이후에는 외부 도구 없이도 PowerShell 기본 기능만으로 원격 관리가 가능해졌습니다.

PowerShell Remoting 활성화 여부 확인

Windows Server 2016/2019/2022에서는 PowerShell Remoting이 기본적으로 활성화되어 있습니다(서버 관리자 → 로컬 서버 → 원격 관리 = 사용됨에서 확인 가능). 반면 데스크톱 Windows(Win10, Win11)에서는 PSRemoting과 WinRM이 기본적으로 비활성화되어 있습니다.

Enter-PSSession 완벽 가이드: 대화형 PowerShell 원격 세션으로 원격 명령 실행하기

다음 명령으로 현재 컴퓨터에서 PSRemoting이 활성화되어 있는지 확인할 수 있습니다.

Get-PSSessionConfiguration

이 명령은 WinRM을 통해 연결이 허용된 사용자 및 그룹 목록을 확인하는 용도로도 사용됩니다. PSRemoting을 사용하려면 사용자 계정이 Administrators 또는 Remote Management Users 그룹의 구성원이어야 합니다. 관리자가 아닌 사용자를 위한 WinRM PowerShell Remoting 활성화 방법은 별도 문서를 참고하세요.

Enter-PSSession 완벽 가이드: 대화형 PowerShell 원격 세션으로 원격 명령 실행하기

다음 명령으로 로컬에서 PowerShell Remoting 연결이 가능한지 테스트할 수 있습니다.

Test-WSMan -ComputerName localhost

명령이 WSMan 스키마 버전을 반환하면 해당 컴퓨터로의 PS Remoting 원격 연결이 허용된 상태입니다.

Enter-PSSession 완벽 가이드: 대화형 PowerShell 원격 세션으로 원격 명령 실행하기

PowerShell Remoting이 비활성화되어 있거나 구성되지 않은 경우에는 다음과 같은 오류가 나타납니다.

Test-WSMan : <f:WSManFaultxmlns:f="https://schemas.microsoft.com/wbem/wsman/1/wsmanfault" Code="2150858770" Machine="srv02"><f:Message>The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig".

Enable-PSRemoting으로 원격 관리 활성화

PowerShell Remoting을 활성화하려면 다음 명령을 실행합니다.

Enable-PSRemoting -Force

이 명령은 다음 작업을 수행합니다.

  • WinRM 서비스를 활성화하고 시작 유형을 자동으로 설정
  • 기본 WinRM 포트(HTTP 트래픽 기준 TCP/5985)에 연결 지점 생성
  • Windows 방화벽에 WS-Management 예외 규칙 추가(PSRemoting을 수동으로 구성하는 경우 PowerShell 또는 GPO를 통해 방화벽 규칙을 추가해야 함)
  • 원격 PowerShell 세션 허용
  • WinRM 서비스 재시작

WinRM 서비스가 실행 중이고 자동 시작으로 설정되어 있는지 확인합니다.

Get-Service WinRM | Select MachineName,Name,Status, StartType

Enter-PSSession 완벽 가이드: 대화형 PowerShell 원격 세션으로 원격 명령 실행하기

Enable-PSRemoting 명령은 도메인 네트워크 및 개인 네트워크 프로필에서만 작동합니다. 공용 네트워크에 있는 컴퓨터에서 PSRemoting을 활성화하려면 네트워크 위치를 공용에서 개인으로 변경하거나, 다음 명령을 사용하세요.

Enable-PSRemoting -SkipNetworkProfileCheck -Force

Active Directory 도메인 환경에서는 그룹 정책(GPO)을 사용하는 것이 서버와 클라이언트에 Windows 원격 관리(PSRemoting)를 중앙에서 일괄 구성하는 가장 효율적인 방법입니다.

SSH를 통한 PowerShell Remoting 연결

최신 PowerShell 버전(v6 또는 v7)은 SSH(Secure Shell) 프로토콜을 지원하여 PowerShell Remoting을 통해 원격 컴퓨터에 연결할 수 있습니다. 단, 원격 컴퓨터에 SSH 연결 지점이 구성되어 있어야 합니다(Windows 10 내장 OpenSSH 서버 활성화 방법 참고). 다음 명령으로 SSH 기반 대화형 PSRemoting 세션을 시작할 수 있습니다.

Enter-PSSession -HostName maxbak@hq-srv01.woshub.com

또는 RSA 키를 사용해 SSH 인증할 수도 있습니다.

Enter-PSSession -HostName maxbak@hq-srv01.woshub.com:22 -KeyFilePath c:\PS\max_rsa_key

New-PSSession과 함께 사용하기

Enter-PSSession은 New-PSSession cmdlet과 함께 사용할 수 있습니다.

$s = New-PSSession -ComputerName hq-srv01.woshub.com
Enter-PSSession -Session $s

인증 방법과 IP 주소 연결 문제 해결

Enter-PSSession은 여러 인증 방법을 지원하며, -Authentication 매개변수로 원하는 방식을 지정할 수 있습니다. Basic, Digest, Kerberos, CredSSP, NegotiateWithImplicitCredential, Negotiate Challenge 인증 방식을 사용할 수 있습니다.

앞선 예제는 동일한 Windows 도메인 내 컴퓨터 간 대화형 연결 방법이었습니다. 이 경우 FQDN 또는 짧은 컴퓨터 이름만 지정하면 되며 Kerberos 인증이 자동으로 사용됩니다. 하지만 IP 주소나 CNAME으로 원격 컴퓨터에 연결을 시도하면 인증에 실패합니다.

Enter-PSSession : Connecting to remote server 192.168.31.12 failed with the following error message: The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated.
Enter-PSSession 완벽 가이드: 대화형 PowerShell 원격 세션으로 원격 명령 실행하기

IP 주소로 원격 컴퓨터에 연결하려면 신뢰할 수 있는 호스트 목록(Trusted Hosts)에 해당 호스트를 추가하거나, 더 안전한 방법인 WinRM SSL을 사용해야 합니다.

TrustedHosts에 호스트 추가하기

신뢰할 수 있는 호스트 목록에 IP 주소를 추가하려면 다음 명령을 실행합니다.

Set-Item WSMan:\localhost\Client\TrustedHosts -Value 192.168.13.5

와일드카드 마스크를 사용해 여러 호스트를 한 번에 추가할 수도 있습니다.

Set-Item WSMan:\localhost\Client\TrustedHosts -Value *.woshub.com

현재 등록된 신뢰 호스트 목록을 조회하려면 다음 명령을 사용합니다.

Get-Item WSMan:\localhost\Client\TrustedHosts

같은 방식으로 원격 컴퓨터의 신뢰 호스트 목록에도 로컬 호스트를 추가할 수 있습니다. 설정 변경 후에는 서비스를 다시 시작합니다.

Restart-Service WinRM

이제 IP 주소로 원격 컴퓨터에 연결할 수 있습니다.

Enter-PSSession -ComputerName 192.168.13.5 -Credential (Get-Credential -UserName woshub\maxbak)

마무리: Enter-PSSession vs Invoke-Command

Enter-PSSessionNew-PSSession cmdlet은 영구적인 일대일 원격 세션을 생성하며, 주로 대화형 작업 시나리오에 적합합니다. 반면 스크립트나 작업을 자동으로 실행하거나 여러 원격 컴퓨터에서 동시에 명령을 수행해야 한다면 Invoke-Command cmdlet을 사용하는 것이 좋습니다.