💙 PowerShell로 자동화된 시스템 모니터링 및 경고 시스템 구축

PowerShell 튜토리얼

고급 난이도
튜토리얼 타입
11/04 등록일

PowerShell로 자동화된 시스템 모니터링 및 경고 시스템 구축

고급
태그
튜토리얼 가이드 학습 시스템_모니터링 자동화 스크립팅 이메일_알림
## 학습 목표
이 튜토리얼을 통해 Windows 서버의 CPU 사용량, 메모리 사용량, 디스크 공간 등을 실시간으로 모니터링하고 임계치를 초과할 경우 이메일 경고를 보내는 자동화 스크립트를 개발할 수 있습니다.

## 준비사항
- Windows 10/Server 운영체제
- PowerShell 5.1 이상 설치 (Windows 10 기준 기본 제공)
- SMTP 서버 설정 완료 (이메일 전송을 위해)
- 기본적인 PowerShell 문법 이해

## 단계별 진행

### 1단계: 기초 설정
PowerShell 환경 및 이메일 전송 기능 테스트

### 2단계: 핵심 구현
시스템 상태 수집 및 임계치 검사 로직 구현

### 3단계: 고급 기능
스케줄링, 로그 저장, 경고 메시지 최적화

### 4단계: 완성 및 테스트
전체 스크립트 통합 및 실무 환경에서의 검증

## 다음 학습 단계
- Azure Automation을 활용한 클라우드 기반 모니터링 시스템 구축
- PowerShell DSC를 이용한 자동화된 시스템 구성 관리
- 시스템 이벤트 로그 분석 및 대응 스크립트 작성
코드 예제
// 1단계: 이메일 전송 테스트
$smtpServer = "smtp.example.com"
$mailMessage = New-Object System.Net.Mail.MailMessage
$mailMessage.From = "admin@example.com"
$mailMessage.To.Add("user@example.com")
$mailMessage.Subject = "시스템 경고"
$mailMessage.Body = "서버가 정상적으로 작동 중입니다."
$mailSmtpClient = New-Object System.Net.Mail.SmtpClient($smtpServer)
$mailSmtpClient.Send($mailMessage)

// 2단계: 시스템 상태 수집 및 경고 로직
$cpuUsage = (Get-WmiObject -Class Win32_Processor | Select-Object -ExpandProperty LoadPercentage)
$memoryUsage = (Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty TotalPhysicalMemory) / 1GB
$diskSpace = Get-WmiObject -Class Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 } | Select-Object -ExpandProperty FreeSpace / 1GB

if ($cpuUsage -gt 80) {
    Send-MailMessage -SmtpServer $smtpServer -From "admin@example.com" -To "user@example.com" -Subject "CPU 사용량 경고" -Body "CPU 사용량이 80%를 넘었습니다."
}

if ($memoryUsage -gt 80) {
    Send-MailMessage -SmtpServer $smtpServer -From "admin@example.com" -To "user@example.com" -Subject "메모리 사용량 경고" -Body "메모리 사용량이 80%를 넘었습니다."
}

if ($diskSpace -lt 10) {
    Send-MailMessage -SmtpServer $smtpServer -From "admin@example.com" -To "user@example.com" -Subject "디스크 공간 경고" -Body "디스크 사용량이 10GB를 넘었습니다."
}

// 3단계: 스케줄링 및 로그 저장
$scriptPath = "$env:SystemRoot	emp\systemMonitor.ps1"
Set-Content -Path $scriptPath -Value (Get-Content -Path $scriptPath)
Register-ScheduledTask -TaskName "System Monitor" -Command "PowerShell.exe" -ArgumentList "$scriptPath" -Trigger (New-ScheduledTaskTrigger -Daily -At 2AM) -Action (New-ScheduledTaskAction -Execute "powershell.exe" -ArgumentList "-File \$scriptPath")

// 4단계: 전체 스크립트 통합 및 테스트
# 위 단계의 코드를 하나로 합쳐서 검증
등록일: 2025년 11월 04일 02:37
언어 정보
언어
PowerShell
카테고리
Microsoft
인기도
#21
학습 팁
코드를 직접 실행해보세요
변수를 바꿔가며 실험해보세요
오류가 나도 포기하지 마세요
다른 예제도 찾아보세요