Windows Powershell tutvustus

From ICO wiki
Revision as of 00:17, 15 March 2010 by Mjogi (talk | contribs) (→‎Moodulid)
Jump to navigationJump to search

Sissejuhatus

Referaadi eesmärk on tutvustada ja teha lühiülevaade Windows Powershellist - selle võimalustest ja funktsioonidest.

Autor

  • Martin Jõgi
  • Grupp A21

Üldine

Mis on Windows Powershell?

Windows powershell (2.0) on revolutsiooniline skriptimiskeel ja käsurea keskkond, mis elab Elab .NET Framework’i seljas. See on lisatud Windows 7 ja Windows Server 2008 R2 sisse. Powershell 2.0 võimaldab kaughaldust kõigile serveritele ja rakendustele, haldust on võimalik teha läbi Powershelli. Näiteks Exchange 2010, Windows Server 2008 R2(ning uuemad Microsofti server-tooted). Powershell 2.0-s ei pea oma masinasse enam rakenduste haldusliidest installima. Halduseks vajalikud käsud käitatakse serveril, käsurida jooksutatakse aga masinas. Powershellis pole vaja leida, paigaldada ega tundma õppida uusi tööriistu või käske - kõik on lihtsalt avastatav. Windows powershellil on paindlik väljund ehk ühe käsu väljund võib olla teise käsu sisendiks (nagu bash shellis). Windows Powershell vähendab tulemuse saavutamise aega säilitades ühtlase keele.


Tugevused

Windows Powershell tugevused on:

  • Järjekindel süntaks: Add, New, Get, Set, Remove, Clear, Connection, EventLog, Member, Printer, Service, Transaction ja WmiObject
  • Komponeeritav: Interaktiivsed keskkonnad võimaldavad tükk-haaval lahenduste loomist ja lihtsalt kokkuseotav (“toru”) keeruliste ülesannete lahendamiseks.
  • Tulemuste saavutamise aeg on palju lühem
  • Laiendatavus Moodulid viivad asja järgmisele tasemele.
  • Skriptimine Skriptimine ei ole enam ajutiste lahenduste leiutamiseks. Tööriistakomplekt on täienenud.
  • Integreeritud läbi serveri rollide Active Directory®, Exchange, Group Policy, jne.
  • Paindlik väljund: PowerShelli väljundiks on objektid, mida on lihtne teisendada ja tarbida järgmise käsu poolt (ei mingit tekstitöötlust!).

(Eneta, 2010)

Käsud (cmdlets)

Käsud on tegu- ja nimisõna paarid PS C:\> Get-ADUser TAB-klahv lisab puuduva osa süntaksist PS C:\> New-AD<vajuta TAB> Käsurea argumendid on positsioonilised PS C:\> Get-ADUser –Identity James PS C:\> Get-ADUser James Spikker on sisse ehitatud … ja samuti järjekindel Tuntud käsud toetatud läbi aliaste süsteemi ning programme saab käivitada dir, cls, cd, md , ping.exe, fsutil.exe


Windows PowerShell (Cmdlet) Windows PowerShell (Alias) Selgitus
row 1, cell 1 row 1, cell 2 row 1, cell 3
row 2, cell 1 row 2, cell 2 row 2, cell 3


Aliaste tegemistest siia veel jne...

Moodulid

Moodulid on Windows PowerShelli laiendamise vahendid. Scriptimise mugavuse muutmiseks on olemas Windows Powershell Pack, mis sisaldab 10 moodulit. See pakk aitab kirjutada kasutajaliideseid powershell scriptis, hallata RSS feede, toimetada Task Scheduler-s otse käsurealt jne. Tõmba siit: PowerShellPack Näiteks:

  • Taskide haldamine:
 PS> New-task | 
Add-TaskTrigger -DayOfWeek Monday, Wednesday, Friday -WeeksInterval 2 -At "3:00 PM" |
 Add-TaskAction -Script {
   Get-Process | Out-GridView
   Start-Sleep -Seconds 100
 } |
 Register-ScheduledTask TestTask
  • RSS feedide töötlemine:
 PS> Get-Feed |
Get-Article |
 Sort-Object PubDate -Descending |
 Select-Object Title, Description -First 10 |
 Out-GridView

Scriptimine ja ISE

Ajalugu

Every released version of Microsoft DOS and Microsoft Windows for personal computers has featured a command-line interface tool. These are COMMAND.COM (in installations relying on MS-DOS, including Windows 9x) and cmd.exe (in Windows NT-family operating systems). These are regular command line interpreters that include only a handful of basic commands. For other purposes, a separate console application needs to be provided, to be invoked from these shells. They also include a scripting language (batch files), which can be used to automate various tasks. However, they cannot be used to automate all facets of GUI functionality, in part because command-line equivalents of operations exposed via the graphical interface are limited, and the scripting language is elementary, preventing the creation of complex scripts by composing available functionality. In Windows Server 2003, the situation was improved,[8] but scripting support was still considered unsatisfactory.

Microsoft attempted to address some of these shortcomings by introducing the Windows Script Host in 1998 with Windows 98, and its command-line based host: cscript.exe . It integrates with the Active Script engine and allows scripts to be written in compatible languages, such as JScript and VBScript, leveraging the APIs exposed by applications via COM. However, it has its own deficiencies, as well. It is not integrated with the shell, its documentation is not very accessible, and it quickly gained a reputation as a system vulnerability vector after several high-profile computer viruses exploited weaknesses in its security provisions. Different versions of Windows provided various special-purpose command line interpreters (such as netsh and WMIC) with their own command sets. None of them were integrated with the command shell; nor were they interoperable.

By 2003, Microsoft had started to develop a new shell called Monad (aka Microsoft Shell or MSH). Monad was to be a new extensible command shell with a fresh design that would be capable of automating a full range of core administrative tasks. Microsoft published the first Monad public beta release on June 17, 2005, Beta 2 on September 11, 2005, and Beta 3 on January 10, 2006. They announced on April 25, 2006 that Monad was renamed to Windows PowerShell, positioning it as a significant part of their management technology offerings.[9] Release Candidate 1 of PowerShell was released at the same time. Release Candidate 2 of PowerShell was released September 26, 2006 and released to web (RTW) on November 14, 2006. PowerShell for Vista was released on January 30, 2007.[10] The last CTP release of Windows PowerShell v2.0 was made available in December 2008.

PowerShell v2.0 was completed and released to manufacturing in August 2009, as an integral part to Windows 7 and Windows Server 2008 R2. Versions of PowerShell for downlevel OSs (i.e. Windows XP, Windows Server 2003, Windows Vista and Windows Server 2008) were released in October 2009 and are available for download for both 32-bit and 64-bit platforms.[11]

Näited

Mõned vajalikud käsud administraatoritele:

  • Registry navigatsioon:
 PS> cd hkcu: 
  • Stringide otsimine failidest (rekursiivne)
 PS> dir –r | select string "searchforthis"
  • Otsi 5 protsessi, mis võtab kõige rohkem mälu:
 PS> ps | sort –p ws | select –last 5
  • Service restartimine (nt. DHCP):
 PS> Restart-Service DHCP
  • Kuva kõik failid antud kaustas:
 PS> Get-ChildItem
  • Arvuti restartimine:
 PS> (Get-WmiObject -Class Win32_OperatingSystem -ComputerName .).Win32Shutdown(2)
  • Kõikide failide kustutamine antud kaustas:
 PS> Remove-Item C:\tobedeleted –Recurse
  • Informatsioon arvuti margi ja mudeli kohta:
 PS> Get-WmiObject-klassi Win32_ComputerSystem
  • Informatsioon BIOS-i kohta antud arvutil:
 PS> Get-WmiObject -Class Win32_BIOS -ComputerName .
  • Kuva hiljuti installitud uuendused:
 PS> Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName .
  • Kuva kasutajanimi, kes praegu antud arvutit kasutab:
 PS>  Get-WmiObject -Class Win32_ComputerSystem -Property UserName -ComputerName
  • Kuva antud arvuti IP aadress:
 PS> Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Format-Table -Property IPAddress
  • Detailsem IP info antud arvuti kohta:
 PS> Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Select-Object -Property [a-z]* -ExcludeProperty IPX*,WINS*
  • Et teada saada, millisel võrgukaardil töötab DHCP:
 PS> Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "DHCPEnabled=true" -ComputerName .
  • Käivita DHCP kõigil võrgu adapteritel antud arvutis:
 PS> Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=true -ComputerName . | ForEach-Object -Process {$_.EnableDHCP()}
  • Installi .msi pakett kaugarvutil:
 PS> (Get-WMIObject -ComputerName TARGETMACHINE -List | Where-Object -FilterScript {$_.Name -eq "Win32_Product"}).Install(\\MACHINEWHEREMSIRESIDES\path\package.msi)
  • Upgrade tegemine:
 PS> (Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Name='name_of_app_to_be_upgraded'").Upgrade(\\MACHINEWHEREMSIRESIDES\path\upgrade_package.msi)
  • .MSI paketti uninstallimine:
 PS> (Get-WmiObject -Class Win32_Product -Filter "Name='product_to_remove'" -ComputerName . ).Uninstall()
  • Sulge antud arvuti minuti pärast:
 PS> Start-Sleep 60; Restart-Computer –Force –ComputerName TARGETMACHINE
  • Lisa printer:
 PS> (New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\printerserver\hplaser3")
  • Kustuta printer:
 PS> (New-Object -ComObject WScript.Network).RemovePrinterConnection("\\printerserver\hplaser3 ")
  • Kaug shelli sisenemine (remote management peab olema enabled):
 PS> enter-pssession TARGETMACHINE
  • Scripti käivitamine kaugarvutil:
 PS> invoke-command -computername machine1, machine2 -filepath c:\Script\script.ps1
  • Leia protsessid, mis kasutavad nt rohkem mälu kui 100mb:
 PS> get-process | where-object { $_.WS -gt 1000MB }
  • Leia protsessid, mis kasutavad nt rohkem mälu kui 100mb ja sulge need:
 PS> get-process | where-object { $_.WS -gt 1000MB } | stop-process
  • Sulge protsessid, mis algavad f tähega:
 PS> get-process f* | stop-process


Kokkuvõte

Windows PowerShell on käsurea keskkond ja skriptimiskeel on eelkõige just süsteemiülematele. Powershell sisaldab järgmisi funktsioone: Käske(cmdlets), et kiirendada administreerimist, skriptimise võimalus, objektidega manipuleerimine (objekte on võimalik saata mujale tööriistadesse või andmebaasi) ning palju muud. Powershell on loodud just mõeldes IT spetsialisti peale, et võimaldada Windows süsteemide ja rakenduste haldamist ning automatiseerimist. Minu arust on Powershell vägagi asjalik ning vajalik tööriist.



Kasutatud kirjandus

  1. Eneta foorum
  2. PowerShellPack
  3. Windows Powershell - Wikipedia
  4. Microsoft TechNet - Powershell
  5. google.com