개요
New-PSSession은 원격 컴퓨터와의 연결을 지속 세션(PSSession) 객체로 만들어, 여러 명령에 걸쳐 그 연결과 상태를 재사용할 수 있게 해 준다. 85장에서 짚었듯 Invoke-Command -ComputerName은 매번 연결을 새로 만들고 끊었는데, 이 장은 그 연결 자체를 변수에 담아 계속 살려 두는 법을 다룬다.
정신 모델은 “지속 세션은 원격 컴퓨터에 열어 둔 채로 유지되는 하나의 작업 공간"이라는 것이다. 이 작업 공간 안에서 만든 변수·임포트한 모듈은 그 세션이 살아 있는 동안 계속 남아 있어, 마치 원격 컴퓨터에 별도의 PowerShell 창을 하나 열어 둔 것과 같다.
사용법
| |
종류
| cmdlet | 역할 |
|---|---|
New-PSSession | 지속 세션 생성, PSSession 객체(들) 반환 |
Get-PSSession | 현재 열려 있는 세션 목록·상태 확인 |
Remove-PSSession | 세션을 완전히 닫고 리소스 반환 |
Disconnect-PSSession / Connect-PSSession | 세션 연결을 끊었다가(연결 종료 후에도 세션은 원격에 살아있음) 나중에 다시 연결 |
Receive-PSSession | 연결이 끊긴 세션에서 그동안 쌓인 출력을 가져옴 |
예시
| |
주의사항·함정
세션을 다 쓰고 Remove-PSSession으로 정리하지 않으면 리소스가 계속 소비된다: 지속 세션은 원격 컴퓨터에서 실제로 리소스(메모리, 세션 슬롯)를 점유하므로, 스크립트가 끝났는데도 세션을 정리하지 않으면 원격 컴퓨터에 열린 세션이 누적된다. 특히 세션 수 제한(기본 동시 세션 수 제한이 있음)에 걸리면 이후 연결 시도가 실패할 수 있다.
Disconnect-PSSession은 84장의 Enter-PSSession이 만든 임시 대화형 세션에는 쓸 수 없다: 84장에서 짚었듯 연결 끊기·재연결 기능은 오직 New-PSSession으로 만든 지속 세션에서만 지원된다. “대화형으로 접속했다가 나중에 다시 이어서 하고 싶다"는 요구가 있다면 처음부터 New-PSSession으로 세션을 만들고, 그 세션에 Enter-PSSession -Session으로 진입하는 방식을 써야 한다.
세션이 여러 개면 Invoke-Command -Session은 전부에 명령을 보낸다: New-PSSession -ComputerName Server01, Server02로 배열 형태의 세션을 만들면, 이후 -Session $s로 넘기는 모든 Invoke-Command는 두 컴퓨터 모두에서 실행된다. 한 컴퓨터에만 명령을 보내고 싶다면 $s[0]처럼 배열 인덱스로 특정 세션만 골라 넘겨야 한다.
네트워크가 끊기면 세션 상태를 확실히 알 수 없다: Get-PSSession의 State 속성이 Broken으로 나타날 수 있지만, 일시적인 네트워크 문제인지 원격 컴퓨터 자체의 문제인지는 이 정보만으로 구분되지 않는다. 장시간 배치 작업에는 65장의 try/catch로 세션 오류를 감지하고 재연결을 시도하는 로직을 함께 고려해야 한다.
이식성: Bash의 tmux/screen이 “연결을 끊어도 세션이 살아있고 나중에 다시 붙을 수 있다"는 점에서 Disconnect-PSSession/Connect-PSSession과 개념적으로 유사하다. 다만 PowerShell 지속 세션은 원격 컴퓨터의 PowerShell 프로세스를 대상으로 하고, tmux는 로컬 터미널 멀티플렉서라는 점에서 적용 범위가 다르다.
![Featured image of post [PowerShell] 86. New-PSSession — 지속 세션 관리](/post/powershell/new-pssession-persistent-session-powershell/wordcloud_hu_34d8e032ac9d3b20.webp)
![[PowerShell] 84. Enter-PSSession — 대화형 원격 세션](/post/powershell/enter-pssession-interactive-remote-powershell/wordcloud_hu_24c82b8d46d3843e.webp)
![[PowerShell] 85. Invoke-Command — 원격 명령 실행](/post/powershell/invoke-command-remote-execution-powershell/wordcloud_hu_b2fb59e6cb4d3966.webp)
![[PowerShell] 86. New-PSSession — 지속 세션 관리](/post/powershell/new-pssession-persistent-session-powershell/wordcloud_hu_51a31538b35462d9.webp)
![[PowerShell] 87. Copy-Item -ToSession/-FromSession — 원격 파일 전송](/post/powershell/copy-item-tosession-fromsession-powershell/wordcloud_hu_f0a496ccacaca52b.webp)
![[PowerShell] 88. SSH 기반 PowerShell Remoting(크로스플랫폼)](/post/powershell/ssh-remoting-cross-platform-powershell/wordcloud_hu_47753be953b04c31.webp)
![[PowerShell] 00. 과정 개요와 커리큘럼](/post/powershell/getting-started-powershell/wordcloud_hu_15979a96cd2a594f.webp)
![[PowerShell] 34. Copy-Item — 복사](/post/powershell/copy-item-command-copy-file-powershell/wordcloud_hu_ebeda4bcc9a1c9ff.webp)
![[PowerShell] 83. PowerShell Remoting 개념과 WinRM 활성화](/post/powershell/powershell-remoting-winrm-enable-powershell/wordcloud_hu_4444c309846040dd.webp)