2 분 소요

다수의 Windows Server를 관리할 때 각 서버에 개별 로그인하는 것은 비효율적입니다. WinRM과 PowerShell Remoting을 활용하면 원격에서 명령을 실행하고 서버를 일괄 관리할 수 있습니다.

서버 랙 이미지

WinRM으로 원격 관리되는 Windows Server 환경

WinRM 기본 설정

서버(대상)에서 설정

# WinRM 빠른 설정 (방화벽 + 서비스 동시 설정)
winrm quickconfig

# 또는 PowerShell Remoting 활성화
Enable-PSRemoting -Force

# WinRM 서비스 상태 확인
Get-Service WinRM

# WinRM 수신기(Listener) 확인
winrm enumerate winrm/config/listener

도메인 환경에서 설정

# 그룹 정책으로 일괄 활성화 시
# Computer Configuration > Policies > Windows Settings >
# Security Settings > Windows Firewall with Advanced Security
# 인바운드: WinRM HTTP (5985), HTTPS (5986) 허용

# 수동 방화벽 설정
New-NetFirewallRule `
    -Name "WinRM-HTTP" `
    -DisplayName "WinRM HTTP" `
    -Direction Inbound `
    -Protocol TCP `
    -LocalPort 5985 `
    -Action Allow

HTTPS로 보안 통신 설정

# 자체 서명 인증서 생성
$cert = New-SelfSignedCertificate `
    -DnsName "server.domain.com" `
    -CertStoreLocation "Cert:\LocalMachine\My"

# HTTPS 리스너 생성
$thumbprint = $cert.Thumbprint
winrm create winrm/config/Listener?Address=*+Transport=HTTPS `
    @{Hostname="server.domain.com"; CertificateThumbprint=$thumbprint}

# HTTPS 방화벽 허용
New-NetFirewallRule `
    -Name "WinRM-HTTPS" `
    -DisplayName "WinRM HTTPS" `
    -Direction Inbound `
    -Protocol TCP `
    -LocalPort 5986 `
    -Action Allow

워크그룹 환경 (비도메인) 설정

# 클라이언트에서: 신뢰할 호스트 목록에 추가
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "192.168.1.10" -Force

# 여러 호스트 추가
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "192.168.1.*" -Force

# 현재 신뢰 호스트 확인
Get-Item WSMan:\localhost\Client\TrustedHosts

PowerShell Remoting 사용

원격 세션 연결

# 단일 서버 접속
Enter-PSSession -ComputerName server01 -Credential (Get-Credential)

# HTTPS로 접속
Enter-PSSession `
    -ComputerName server01 `
    -UseSSL `
    -Credential (Get-Credential) `
    -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck)

# 세션 종료
Exit-PSSession

여러 서버에 명령 일괄 실행

# 여러 서버에 동시 실행
$servers = @("WEB01", "WEB02", "DB01")
$cred = Get-Credential

Invoke-Command -ComputerName $servers -Credential $cred -ScriptBlock {
    # 디스크 사용량 확인
    Get-PSDrive -PSProvider FileSystem |
        Select-Object Name, Used, Free,
        @{N='UsedGB'; E={[math]::Round($_.Used/1GB,1)}},
        @{N='FreeGB'; E={[math]::Round($_.Free/1GB,1)}}
}

# 결과를 PSComputerName으로 구분
Invoke-Command -ComputerName $servers -ScriptBlock {
    [PSCustomObject]@{
        Server = $env:COMPUTERNAME
        CPU    = (Get-WmiObject Win32_Processor).LoadPercentage
        Memory = [math]::Round(
            (Get-WmiObject Win32_OperatingSystem).FreePhysicalMemory / 1MB, 1
        )
    }
} | Format-Table PSComputerName, Server, CPU, Memory -AutoSize

서버 코드 이미지

PowerShell Remoting으로 원격 서버를 관리하는 화면

재사용 가능한 세션 생성

# 영구 세션 생성 (빠른 재연결)
$sessions = New-PSSession -ComputerName $servers -Credential $cred

# 세션 목록 확인
Get-PSSession

# 세션에 명령 실행
Invoke-Command -Session $sessions -ScriptBlock {
    Restart-Service -Name W3SVC
}

# 세션 정리
Remove-PSSession $sessions

WinRM 설정 강화

# 최대 동시 셸 수 제한
Set-Item WSMan:\localhost\Shell\MaxShellsPerUser -Value 10

# 최대 메모리 제한 (MB)
Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB -Value 512

# 유휴 타임아웃 설정 (ms)
Set-Item WSMan:\localhost\Shell\IdleTimeOut -Value 900000  # 15분

# 특정 IP에서만 연결 허용
$sddl = "O:NSG:BAD:P(A;;GA;;;RM)S:P(AU;FA;GA;;;WD)(AU;SA;GWGX;;;WD)"
Set-PSSessionConfiguration -Name Microsoft.PowerShell `
    -SecurityDescriptorSddl $sddl

# WinRM 인증 방식 확인
winrm get winrm/config/client/auth

문제 해결

# 연결 테스트
Test-WSMan -ComputerName server01

# 연결 오류 진단
Test-NetConnection -ComputerName server01 -Port 5985

# WinRM 설정 전체 확인
winrm get winrm/config

# 방화벽 규칙 확인
Get-NetFirewallRule | Where-Object { $_.DisplayName -like "*WinRM*" }

WinRM은 도메인 환경에서 그룹 정책으로 일괄 배포하면 관리가 훨씬 편리합니다. 보안을 위해 필요한 관리자 IP만 WinRM에 접근할 수 있도록 방화벽을 구성하고, 가능하면 HTTPS 리스너를 사용하는 것을 권장합니다.

댓글남기기