Windows 7 Powershell: Sprievodca
Ako riešiť zložité úlohy s novým PowerShell vo Windows 7. Náš tutoriál ukazuje príklady a poskytuje podrobné pokyny.
Pochopiť nový koncept powerhell
Na rozdiel od starého príkazového riadka pracuje Powershell dôsledne objektovo; je založený na objektoch rámca Microsoft .NET framework. Ďalšou výhodou okrem rozšíreného rozsahu funkcií: Prístup k objektom je jednotný. Namiesto predchádzajúcich príkazov DOS, takzvaných cmdletov (prečítajte si „príkazové riadky“), ktoré môžete použiť ako znak „|“ môže reťaziť v rade. To znamená, že jeden cmdlet spracúva výstup z predchádzajúceho a umožňuje tak kompaktnú formuláciu zložitých úloh. Ako začať Powershell:
- Ak chcete vyvolať Powershell, prejdite v ponuke Štart do položky „Všetky programy“> „Príslušenstvo“> „Windows PowerShell“ alebo do vyhľadávacieho riadku napíšte „powerhell“.
- Výsledné okno zobrazuje tri objekty: „PowerShell“ spúšťa prostredie priameho príkazu, „Windows PowerShell ISE“ je jednoduché grafické užívateľské rozhranie, ktoré pomáha predovšetkým pri vývoji skriptov. Ako tretí prvok sú stále k dispozícii moduly. Na začiatok použite jasnú grafickú variantu.
- Pre opakované použitie je vhodné nastaviť rýchly prístup k PowerShell. Ak to chcete urobiť, vytvorte odkaz alebo kliknite pravým tlačidlom myši na program a potom vyberte kontextový príkaz „Pripnúť tento program na panel úloh“.
Nová syntax príkazu
- Okno Powershell ISE je rozdelené na oblasť skriptov, výstupov a príkazov. Oblasti aktivujete kliknutím myši alebo pomocou ponuky „Zobraziť“. V dialógovom okne „Súbor“ môžete skripty postupne ukladať, načítavať a spúšťať v ponuke „Ladiť“.
- Začnite na príkazovom riadku v oblasti príkazov. Výzva Powershell ukazuje aktuálnu cestu ako obvykle. Názvy príkazov, ktoré sú preddefinované ako cmdlets, nasledujú štruktúru podstatného mena slovesa. Napríklad cmdlet "Set-Location" s cestou slúži ako argument pre zmenu do iného adresára. „Get-Childitem“ zobrazuje obsah priečinka. Prehľad existujúcich príkazov získate pomocou príkazu „Získať príkaz“.
- Niektoré známe príkazy DOS je stále možné použiť - „cd“ (zmena adresára) funguje aj pri zmene adresára. Aspoň to môžete stále vyvolať, pretože spoločnosť Microsoft predefinovala všetky príkazy pod kapotou. Toto je zabezpečené priradením aliasov, ktoré pri zadávaní skratky volajú zodpovedajúcu rutinu cmdlet. Zoznam definovaných pojmov aliasu je uvedený zadaním „Získať alias“. Pri používaní starých príkazov si však musíte uvedomiť, že známe parametre zvyčajne už nefungujú, pretože pre rutiny cmdlets bola definovaná zmenená syntax. Správny pravopis a všetky možné parametre nájdete v pomocníkovi pre príslušnú rutinu, napríklad v časti „Get-Help Get-Childitem“. Ako argument použite príkaz, pre ktorý potrebujete pomoc. Uvedený príklad poskytuje pomoc pre „GetChilditem“, rutinu cmdlet pre starý dobrý príkaz adresára „dir“.
- Poznámka: K pomoci môžete kedykoľvek vstúpiť v grafickom používateľskom rozhraní pomocou klávesu [F1], všeobecne alebo kontextovo citlivým na aktuálny vstup.
Prepojiť príkazy Powershell
Jednoduchý príklad výpisu obsahu priečinkov ukazuje, ako koncept cmdlet rozširuje funkčnosť známu zo starého príkazu DOS „dir“ v niekoľkých krokoch. Typickou úlohou je nájsť objekty medzi súbormi v priečinku, ktoré spĺňajú určité vlastnosti. Napríklad nájdite päť najväčších súborov MP3 v jednom adresári. V Powershell to môžete urobiť rýchlo pomocou niekoľkých pripútaných príkazov. Ak to chcete urobiť, zadajte nasledujúci text:
- Get-Childitem -recurse -include *. Sams
- Dĺžka zoradeného objektu - vzostupne
- Vyberte objekt - prvé 5
Tento príkaz najskôr rekurzívne určí súbory MP3 obsiahnuté vo všetkých podpriečinkoch a odovzdá ich ako objekt. Druhý krok triedi zoznam v zostupnom poradí podľa veľkosti, zatiaľ čo tretí krok z neho vyberie prvých päť prvkov