Windows PowerShell
une nouvelle approche de
l’administration des environnements
Windows
Objectif
Pour les néophiques : découvrir Windows PowerShell
Pour les experts : comment étendre Windows Powershell
Pas trop de blabla, des démonstrations !!
Windows PowerShell
Windows PowerShell est une invite de commande (shell)
interactive et un environnement de scripts offrant une
manière de traiter des tâches orientée commandes.
Les concepteurs ont suivi différentes approches afin que
PowerShell soit :
aussi interactif et composable que KSH ou BASH,
aussi programmable que PERL ou RUBY,
aussi orienté-production que VMS DCL ou AS400 CL.
permettre l’accès aux sources de données aussi facilement que
l’accès au système de fichiers
Agenda
Découverte de Windows PowerShell
Windows PowerShell et l’administration
Etendre Windows PowerShell
Découverte de
PowerShell
Mieux comprendre la structure et les bénéfices de PowerShell !
« J’ai refait tous les calculs, ils confirment l’opinion
des spécialistes : mon idée est irréalisable. Il ne me
reste plus qu’une seule chose à faire : la réaliser »
Pierre-Georges Latécoère.
Découverte de PowerShell
1. Les bases
Le modèle des commandes
Que manipule-t-on ? Expressions, Variables, Types
Le pipeline & les filtres
2. Constructions du langage
Opérateurs
Tests & boucles
3. Intégration avec le système
Lecteurs étendus
WMI
COM
Configuration de PowerShell
Version 1.0
Pré-requis : Framework .Net 2.0
Disponible pour:
Windows Server • Une « feature » à installer •x86
Code Name • Pas installée par défaut •x64
Longhorn • Pas disponible avec « Server Core » •IA64
Windows Server 2003 • Une mise à jour système entre le SP1 et le SP2 •x86
SP1 (KB926139, 140, 141) •x64
• A télécharger •IA64
Windows Vista • Une mise à jour (KB928439) •x86
• A télécharger •x64
Windows XP SP2 • Une mise à jour système (KB926139, 140, 141) •x86
• A télécharger •x64
http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx
Multi-langues
Version anglaise (KB926139)
Ou version localisée en 10 autres langues (KB926140) :
ou MUI (KB926141)
Homogénéité & Cohérence
Get-Command sur le modèle : <verb>-<nom>
avec un certain nombre de paramètres omniprésents :
Plus de détails : help about-parameter
Les 4 commandes de découverte
1. Get-command
Get-Command informations de base sur les commandes
2. Get-help
Get-help aide de base (utiliser –full)
3. Get-member
Get-member informations sur les objets
4. Get-PSDrive
Get-psdrive informations sur les lecteurs étendus
parmi 129 commandes intégrées ou “cmdlets” commandettes ?
5 categories de commandes
Plus de détails : get-command * | group commandtype
Tout est objet !
(date).adddays(100)
L’accès aux objets est uniformisé !
.Net Object Adapter
.Net Object
WMI Object Adapter
Script PowerShell accédant à
WMI Object un objet
COM Object Adapter
COM Object
Other Adapters :
ADSI , ADO…
Custom Object Adapter
Les Types
PowerShell et les nombres
Plus de détails : [math] | get-member -static
Les opérateurs PowerShell
Plus de détails : help about*operator*
Les instructions de contrôle de flux
Fournisseurs et magasins de données
PowerShell et les fichiers
PowerShell et le texte
Les ordres de formatage
PowerShell et XML
PowerShell et l’accès au système
Stratégie d’éxécution
Contrôle ce qui peut être exécuté
Get-ExecutionPolicy
Set-ExecutionPolicy UnRestricted
Set-ExecutionPolicy
Plus de détails : help about_signing
Accès universel !
COM
TXT webservice
ADSI
&
WMI ADO .Net
Services &
Processus
Que faut-il retenir ?
Une syntaxe homogène : verbe-nom
Les 4 commandes de base :
Get-command, get-help, get-member, get-psdrive
Tout est objet !
Le pipeline aussi !
Interactions avec le système via :
Text & XML
COM & .Net
WMI et EventLog
Sécurisé
Windows PowerShell et
l’administration
L’administration
Outils d’administration « graphique »
Outils en ligne de commande natifs (cmd, exe, etc…)
Scripting Windows Automatiser les tâches d'administration
VBScript, WSH, WMI, ADSI, CDO et les objets COM
Windows PowerShell et l’administration
L'objectif : Améliorer et faciliter le contrôle administratif
sur les systèmes, que ce soit de manière interactive ou par
le biais de scripts.
Gestion des processus locaux
Gestion de services locaux
Collecte d'informations sur des ordinateurs
Utilisation d'installations logicielles
Changement d'état de l'ordinateur : verrouillage, fermeture de
session, arrêt en cours et redémarrage
Utilisation d'imprimantes
Tâches de gestion de réseau
Utilisation des fichiers et dossiers
Utilisation du registre
Au cœur des solutions d’entreprise
Etendre
Windows PowerShell
Etendre Windows PowerShell
Comment fonctionne Windows PowerShell
Les différentes manières d’étendre Windows
PowerShell
Functions/Filters/Scripts
Cmdlets
Providers
Hosts
Etendre PowerShell
Communauté Française : http://www.powershell-scripting.com/
Newsgroup: Microsoft.Public.Windows.PowerShell
Team blog: http://blogs.msdn.com/PowerShell/
Channel 9 tag: http://channel9.msdn.com/tags/PowerShell
OMark van Orsouw’s blog: http://ThePowerShellGuy.Com
Wiki: http://channel9.msdn.com/wiki/default.aspx/Channel9.WindowsPowerShellWiki
Script Center:
http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
Manning Press book by Bruce Payette: PowerShell in Action
http://manning.com/powershell/
O’Reilly book – Monad: Introducing the MSH Command Shell and Language
http://www.oreilly.com/catalog/msh/
Sapien Press book by Don Jones: PowerShell
http://www.sapienpress.com/
MshAnalyzer tool
http://www.karlprosser.com/coder/?cat=8
Questions?
Ressources
Technical Chats and Webcasts
http://www.microsoft.com/communities/chats/default.mspx
http://www.microsoft.com/usa/webcasts/default.asp
Microsoft Learning and Certification
http://www.microsoft.com/learning/default.mspx
MSDN & TechNet
http://microsoft.com/msdn
http://microsoft.com/technet
Virtual Labs
http://www.microsoft.com/technet/traincert/virtuallab/rms.mspx
Newsgroups
http://communities2.microsoft.com/
communities/newsgroups/en-us/default.aspx
Technical Community Sites
http://www.microsoft.com/communities/default.mspx
User Groups
http://www.microsoft.com/communities/usergroups/default.mspx
www.PowerShell-Scripting.com
Entrez dans la communauté
francophone
Profitez d’un partage de
connaissances autour de PowerShell
Ressources
Votre potentiel, notre passion TM
© 2007 Microsoft France
Reference Slides
Access existing instrumentation
Bind to COM objects
$fso = New-Object -ComObject Scripting.FileSystemObject
$m = [System.Runtime.InteropServices.Marshal]
$word = $m::GetActiveObject("Word.Application")
Invoke methods/access properties
$fso.GetDrive(“C:”)
$fso.VolumeName = “System Drive”
Understand/extend instrumentation
Extend and discover properties/methods
Update-TypeData Office.Word.Types.ps1xml
$fso | Get-Member
Manipulate and format results
Define and import custom formating
Update-FormatData Office.Word.Format.ps1xml
$word.RecentFiles | Sort name | Format-Table
Allows more simpler/more powerful COM scripts because of utilities
and formatting
PowerShell provides native WMI support
Get-WmiObject
Allows for inspection of WMI namespace
Get-WmiObject –list [-Namespace xx]
Get-WmiObject –Class xx –Namespace xx –Property xxx – Filter
xxx –ComputerName xxx –Credential xxx
Native language support
[WMI] “\\JPSDESK10\root\cimv2:Win32_Process.Handle="0“
[WMICLASS] "ROOT\cimv2:WIN32_PROCESS"
[WMISEARCHER]"select * from Win32_process WHERE Name = 'calc.exe'"
PowerShell provides native access to any .NET class
Create any object
[reflection.assembly]::LoadWithPartialName("System.Windo
ws.Forms")
$d = New-Object System.DateTime 2006,12,25
Access Properties/Invoke Methods
$d.DayOfWeek
$d.AddDays(-30)
Access Statics Allows admins to easily access and
[DateTime]::Now leverage a huge API set because of
scriptability, utilities and formatting
[DateTime]::IsLeapYear(2006)
PowerShell provides native XML support
Native datatype
$x=[xml]"<a><b><c>TEST</c></b></a>“
$b =[xml](type c:\i386\mssecure.xml)
Native syntax to access “data” view of properties
$b.BulletinDataStore.Bulletins.Bulletin[0]
Access to XML methods
$b.BulletinDataStore.SelectNodes(“//Patch”)
XML properties available through PSBase property
$b.BulletinDataStore.PSBase.innerXml
Invoke existing tools
Existing command run directly after variables are expanded
Harvest data from existing tools
Parse output into variables using text utilities.
Pipe data to SELECT and use –FIRST and –LAST
Select-String <REGEX> <Path>
Dir | Select-String <REGEX>
[DateTime]”12/25/2006 7:00”
([DateTime]”12/25/2006 7:00”).AddDays(-30)
Use functions/scripts to wrap the commands and convert output to objects or provide standard
syntax
Safely process text
Use CLR types via Windows PowerShell to safely parse text
[URI]” http://blogs.msdn.com/powershell/archive/2006/04/25/583234.aspx”
Allows admins to get 2-10x more power out of existing commands because
of scriptability