Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Windows Server 2008 管理利器:PowerShell 批量自動化實戰

編輯:Windows Server 教學

前言:為什麼需要 PowerShell?

IT 運維常面臨「跨平台、跨廠商、跨層級」技能要求,但預算有限無法開發專屬管理軟體。PowerShell 並非取代 CMD,而是提供 **物件導向管線、豐富 .NET 整合、遠端執行、腳本模組化** 的管理自動化引擎,專為 **系統管理員 (非開發人員)** 設計,以極低門檻解決批量、遠端、重複性任務。

典型場景:自動化部署 Office 2007 至符合條件的用戶端

  1. 需求:收集所有用戶端剩餘磁碟空間 ≥ 4 GB 且記憶體 ≥ 1 GB 的機器,自動推送安裝 Office 2007;不符合者記錄略過。
  2. 傳統痛點:需撰寫 C#/VB.NET 完整應用程式,開發週期長、維護難、錯誤率高。
  3. PowerShell 方案:單一腳本搞定查詢、判斷、推送、日誌。

核心優勢

  • 物件管線:`Get-WmiObject Win32_LogicalDisk | Where-Object {$_.FreeSpace -ge 4GB}` 直接輸出物件,無需字串解析。
  • 遠端原生支援:`Invoke-Command -ComputerName (Get-Content servers.txt) -ScriptBlock { ... }` 一次對百台機器執行。
  • .NET 全權存取:`[System.IO.Directory]::GetFiles()`、`[Microsoft.Win32.Registry]::LocalMachine` 等隨時可用。
  • 模組化與社群:`Import-Module ActiveDirectory`、`Install-Module -Name PSWindowsUpdate` 即插即用;TechNet Gallery、PowerShell Gallery 提供海量現成腳本。

常用管理腳本範例

1. 批量查詢磁碟空間與記憶體


$servers = Get-Content C:\List\servers.txt
$results = foreach ($s in $servers) {
    $disk = Get-WmiObject Win32_LogicalDisk -ComputerName $s -Filter "DeviceID='C:'" -ErrorAction SilentlyContinue
    $mem  = Get-WmiObject Win32_ComputerSystem -ComputerName $s -ErrorAction SilentlyContinue
    if ($disk -and $mem) {
        [pscustomobject]@{
            Server = $s
            FreeGB = [math]::Round($disk.FreeSpace/1GB,2)
            TotalRAM_GB = [math]::Round($mem.TotalPhysicalMemory/1GB,2)
            Eligible = ($disk.FreeSpace -ge 4GB -and $mem.TotalPhysicalMemory -ge 1GB)
        }
    }
}
$results | Export-Csv C:\Reports\Eligibility.csv -NoTypeInformation -Encoding UTF8

2. 遠端推送安裝 (假設共享點 `\\Deploy\Office2007\setup.exe /adminfile config.msp`)


$eligible = Import-Csv C:\Reports\Eligibility.csv | Where-Object {$_.Eligible -eq 'True'}
foreach ($row in $eligible) {
    Invoke-Command -ComputerName $row.Server -ScriptBlock {
        & "\\Deploy\Office2007\setup.exe" /adminfile "\\Deploy\Office2007\config.msp" /quiet /norestart
    } -Credential (Get-Credential) -ErrorAction Continue
}

3. 服務狀態巡檢與自動修復


$critical = @('wuauserv','bits','w3svc','MSSQLSERVER')
$servers = Get-Content C:\List\servers.txt
foreach ($s in $servers) {
    foreach ($svc in $critical) {
        $status = Get-Service -Name $svc -ComputerName $s -ErrorAction SilentlyContinue
        if ($status -and $status.Status -ne 'Running') {
            Write-Warning "[$s] $svc 狀態: $($status.Status) → 嘗試啟動"
            Start-Service -Name $svc -ComputerName $s -ErrorAction SilentlyContinue
        }
    }
}

學習資源與遷移建議

  • 內建說明:`Get-Help about_*`、`Update-Help` 下載最新文件。
  • 官方腳本中心:TechNet Script Center (現遷移至 GitHub/PowerShell Gallery)。
  • 書籍推薦:《Windows PowerShell in Action》、《PowerShell 實戰指南》。
  • 遷移策略:現有 `.bat`/`.cmd`/`vbs` 腳本可漸進式重寫為 `.ps1`,利用 `Start-Process cmd /c` 相容舊指令。

結語

PowerShell 將「管理員會寫腳本」從願景變為現實:無需編譯、無需部署代理、物件管線直觀、遠端原生、社群資源豐富。Server 2008 內建 PowerShell 1.0 (R2 內建 2.0),配合 WinRM 2.0 即可開啟自動化運維新紀元。建議每位網管投入 20 小時系統學習,終身受益。

Copyright © Windows教程網 All Rights Reserved