Opublikowany w: Poradniki
9 wyświetlenia 0 Lubię
Read more
PowerShell to zaawansowana powloka wiersza polecen i jezyk skryptowy od Microsoftu, ktory zrewolucjonizowal sposob zarzadzania infrastruktura IT. Dla administratorow Windows Server to absolutnie niezbedne narzedzie — pozwala automatyzowac powtarzalne zadania, zarzadzac setkami serwerow jednoczesnie i wykonywac w minuty operacje, ktore recznie zajmowalyby godziny.
PowerShell to znacznie wiecej niz zwykly wiersz polecen. To obiektowy jezyk skryptowy oparty na platformie .NET, ktory pozwala na interakcje z praktycznie kazdym elementem ekosystemu Windows — od systemu plikow, przez rejestr, po Active Directory, Hyper-V i uslugi chmurowe Azure.
Get-Process, Set-Service).| Cecha | CMD | Bash | PowerShell 7 |
|---|---|---|---|
| Typ danych | Tekst | Tekst | Obiekty .NET |
| Wieloplatformowosc | Tylko Windows | Linux/macOS | Windows, Linux, macOS |
| Zarzadzanie AD | Ograniczone | Brak | Pelne moduly |
| Zdalne zarzadzanie | PsExec | SSH | WinRM + SSH + PSRemoting |
| Obsluga bledow | Kody wyjscia | trap | Try/Catch/Finally |
Dla administratora Windows Server 2022 czy Windows Server 2025 PowerShell to optymalne narzedzie.
winget install --id Microsoft.PowerShell --source wingetiex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI -Quiet"Po instalacji: pwsh --version. PS 7 wspolistnieje z PS 5.1.
Get-Process | Where-Object { $_.WorkingSet64 -gt 100MB } | Sort-Object WorkingSet64 -Descending
Restart-Service -Name "Spooler" -Force
Get-Service -Name "DNS","NTDS","W3SVC" | Format-Table Name, Status, StartTypeGet-ChildItem -Path "C:\Logs" -Recurse -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } | Remove-Item -ForceGet-CimInstance Win32_OperatingSystem | Select-Object Caption, Version, FreePhysicalMemory
Get-CimInstance Win32_LogicalDisk | Select-Object DeviceID, @{N='FreeGB';E={[math]::Round($_.FreeSpace/1GB,2)}}
Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object -First 10$servers = @("DC01", "SQL01", "WEB01", "FILE01")
$freeSpace = (Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'").FreeSpace / 1GB
if ($freeSpace -lt 10) {
Write-Warning "KRYTYCZNIE: Mniej niz 10 GB!"
} elseif ($freeSpace -lt 50) {
Write-Warning "Mniej niz 50 GB wolnego."
} else {
Write-Host "OK: $([math]::Round($freeSpace,2)) GB wolnego." -ForegroundColor Green
}
foreach ($server in $servers) {
$status = Invoke-Command -ComputerName $server -ScriptBlock { Get-Service "W32Time" | Select-Object Status }
Write-Host "$server - W32Time: $($status.Status)"
}function Test-ServerHealth {
[CmdletBinding()]
param([Parameter(Mandatory,ValueFromPipeline)][string[]]$ComputerName, [int]$CpuThreshold=80)
process {
foreach ($c in $ComputerName) {
$cpu = (Get-CimInstance -ComputerName $c Win32_Processor | Measure-Object LoadPercentage -Average).Average
[PSCustomObject]@{ Server=$c; CpuPct=$cpu; Alert=$cpu -gt $CpuThreshold }
}
}
}
"DC01","SQL01" | Test-ServerHealth | Format-TableActive Directory to serce kazdej infrastruktury Windows:
Install-WindowsFeature -Name RSAT-AD-PowerShell
Get-ADUser -Filter {LastLogonDate -lt $((Get-Date).AddDays(-90)) -and Enabled -eq $true} -Properties LastLogonDate | Export-Csv "InactiveUsers.csv"
Import-Csv "NewUsers.csv" | ForEach-Object { New-ADUser -Name "$($_.FirstName) $($_.LastName)" -SamAccountName $_.Username -Enabled $true -ChangePasswordAtLogon $true }
Search-ADAccount -LockedOut | ForEach-Object { Unlock-ADAccount -Identity $_.SamAccountName }Modul Hyper-V pozwala zarzadzac VM z konsoli:
Get-VM | Format-Table Name, State, CPUUsage, MemoryAssigned, Uptime
New-VM -Name "WebServer01" -MemoryStartupBytes 4GB -NewVHDPath "D:\VMs\WebServer01.vhdx" -NewVHDSizeBytes 60GB -Generation 2
Set-VM -Name "WebServer01" -ProcessorCount 4 -DynamicMemory -MemoryMinimumBytes 2GB -MemoryMaximumBytes 8GB
Checkpoint-VM -Name "WebServer01" -SnapshotName "Pre-Update"
Get-VM | Where-Object { $_.State -eq 'Off' } | Start-VMEnable-PSRemoting -Force
Enter-PSSession -ComputerName "DC01" -Credential (Get-Credential)
$results = Invoke-Command -ComputerName @("DC01","SQL01","WEB01") -ScriptBlock {
[PSCustomObject]@{ Server=$env:COMPUTERNAME; Uptime=((Get-Date)-(gcim Win32_OperatingSystem).LastBootUpTime).Days; FreeGB=[math]::Round((gcim Win32_LogicalDisk -Filter "DeviceID='C:'").FreeSpace/1GB,2) }
}
$results | Format-Table$action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument "-File C:\Scripts\DailyReport.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At "06:00AM"
Register-ScheduledTask -TaskName "Daily Report" -Action $action -Trigger $trigger -Principal (New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest)Get-ExecutionPolicy -List
Set-ExecutionPolicy RemoteSigned -Scope LocalMachine
# JEA
New-PSRoleCapabilityFile -Path "HelpDesk.psrc" -VisibleCmdlets @('Restart-Service','Get-Service','Unlock-ADAccount')
Start-Transcript -Path "C:\PSTranscripts\session.txt"@("DC01","SQL01","WEB01") | ForEach-Object {
[PSCustomObject]@{ Server=$_; Status=if(Test-Connection $_ -Count 2 -Quiet){"ONLINE"}else{"OFFLINE"} }
} | Format-Table@("C:\Windows\Temp","C:\inetpub\logs") | ForEach-Object {
if (Test-Path $_) { Get-ChildItem $_ -Recurse -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } | Remove-Item -Force -EA SilentlyContinue }
}Zaawansowana powloka i jezyk skryptowy Microsoftu do automatyzacji administracji Windows, Active Directory, Hyper-V, Exchange i Azure.
Win+X → Terminal (Admin) w Windows 11, lub PPM na ikone → Uruchom jako administrator.
PS 5.1 = .NET Framework, tylko Windows. PS 7 = .NET 8, wieloplatformowy. Uruchamiaj pwsh (PS7) vs powershell (PS 5.1).
Tak — Execution Policy, JEA, Constrained Language Mode, Script Block Logging.
Get-Help, Get-Command, Get-Member. Cwicz na VM Hyper-V. Kurs "PowerShell 101" na learn.microsoft.com.
Oryginalne klucze z natychmiastowa dostawa.
Artykul zaktualizowany: marzec 2026 | Autor: Zespol KluczeSoft
Dodaj komentarz