support de la formation powershell 2.0

Upload: livadari-ion

Post on 15-Oct-2015

55 views

Category:

Documents


1 download

DESCRIPTION

Formation Powershell

TRANSCRIPT

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 1

    Powershell 2.0 alphorm.com

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

    Prsentation de la formation

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Powershell 2.0 alphorm.com

    Introduction

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 2

    Powershell 2.0 alphorm.com

    Plan Prsentation du formateur.

    Prrequis de cette formation.

    Quelques rfrences.

    Prsentation de la formation.

    Powershell 2.0 alphorm.com

    Prsentation du formateur PrsentationPrsentationPrsentationPrsentation

    Stphane van GulickStphane van GulickStphane van GulickStphane van Gulick

    Ingnieur Systmes et automatisation

    MCTS, MCITP EA, MCSA

    Blog : http://PowerShellDistrict.com

    Twitter : @stephanevg

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 3

    Powershell 2.0 alphorm.com

    Prrequis de cette formation Connaissances des systmes Windows

    Une exprience en Scripting est un plus. (Mais pas obligatoire !)

    Comprhension de principes algorithmiques (tel que les IF, ELSE, FOREACH) etc..

    tre motiv et enthousiaste !

    Lab:

    1 machine Windows 7

    (une deuxime machine windows 7 pour le chapitre sur le remoting).

    Powershell 2.0 alphorm.com

    Quelques rfrences Version CHM de laide PowerShell http://www.microsoft.com/en-

    us/download/details.aspx?id=10552

    Script center http://technet.microsoft.com/en-us/scriptcenter/

    Blog Scripting Guy http://blogs.technet.com/b/heyscriptingguy/

    Livres ->

    Windows PowerShell Guide de rfrence

    PowerShell in Action

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 4

    Powershell 2.0 alphorm.com

    Prsentation de la formation

    3 grandes parties3 grandes parties3 grandes parties3 grandes parties

    Chapitres

    Modules

    Powershell 2.0 alphorm.com

    Prsentation de la formation1.1.1.1. LinitiationLinitiationLinitiationLinitiation au PowerShell au PowerShell au PowerShell au PowerShell 2.02.02.02.0

    Chapitre 1 : Introduction PowerShell

    1. Prsentation de la formation

    2. Introduction gnrale a Windows PowerShell

    Chapitre 2 : Lapprentissage de PowerShell

    1. Introduction au langage

    2. Oprateurs et expressions

    3. La gestion du flux (lutilisation du pipe)

    4. Importer des donnes depuis des supports externes.

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 5

    Powershell 2.0 alphorm.com

    Prsentation de la formation Partie 2 : Lutilisation avanc de Partie 2 : Lutilisation avanc de Partie 2 : Lutilisation avanc de Partie 2 : Lutilisation avanc de PowerShellPowerShellPowerShellPowerShell 2.02.02.02.0

    Chapitre 3 Lutilisation avanc de PowerShell : Le scripting

    1. Les fonctions et les filtres

    2. La gestion derreur

    3. Les fonctions avances

    Chapitre 4 : La maitrise de PowerShell en travaillant grande chelle

    1. Les modules

    2. Lexecution distance

    3. Lexecution en tche de fond : Les jobs

    Powershell 2.0 alphorm.com

    Prsentation de la formationPartiePartiePartiePartie 3 : 3 : 3 : 3 : MatriseMatriseMatriseMatrise de PowerShell de PowerShell de PowerShell de PowerShell 2.02.02.02.0

    Chapitre 5: Crer ses propres cmdlets

    Le WMI

    Lautomatisation dapplications (Com objects)

    Lexploitation du dotnet

    Conclusion

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 6

    Powershell 2.0 alphorm.com

    Are you ready ?

    Lets GO !

    Powershell 2.0 alphorm.com

    Introduction gnrale Windows PowerShell

    Introduction Windows PowerShell

    Initiation PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 7

    Powershell 2.0 alphorm.com

    Plan1. Quest-ce PowerShell ?

    2. Les diffrents versions existantes de PowerShell.

    3. Prrequis ncessaire pour linstallation / lutilisation de PowerShell.

    4. Prsentation du prompt, et de lenvironnement de Scripting intgr.

    5. Dcouverte des premires commandes.

    Powershell 2.0 alphorm.com

    Les avantages utiliser Windows PowerShell ? Automatisation de tches rcurrentes / rptitives.

    Rduit le temps deffort ncessaire pour laccomplissement de cette tche.

    Permet de parallliser plusieurs tches la fois.

    Rduit la probabilit derreur.

    Possibilit de dlguer une tche plus facilement.

    Est idal pour la rutilisabilit.

    Permet daller au-del des limites des interface graphiques.

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 8

    Powershell 2.0 alphorm.com

    Prsence de Windows PowerShell

    Systme dxploitation PowerShell 2.0

    Windows XP PowerShell 2.0 (Facultatif)

    Windows Server 2003 PowerShell 2.0 (Facultatif)

    Windows Vista PowerShell 2.0 (Facultatif)

    Windows 7 PowerShell 2.0 (Natif)

    Windows Server 2008 PowerShell 2.0 (Natif)

    Windows 8 PowerShell 3.0 (Natif)

    Windows Server 2012 PowerShell 3.0 (Natif)

    Windows 8.1 PowerShell 4.0 (Natif)

    Windows Server 2012 R2 PowerShell 4.0 (Natif)

    Powershell 2.0 alphorm.com

    Prrequis linstallation de Windows PowerShell

    Powershell Windows Management framework

    Prrequis Lien

    2.0 WMF 2.0 Dotnet 2.0 http://support.microsoft.com/kb/968929/fr

    3.0 WMF 3.0 Dotnet 3.5 http://support.microsoft.com/kb/2506143/fr

    4.0 WMF 4.0 Dotnet 4.5 http://www.microsoft.com/fr-fr/download/details.aspx?id=40855

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 9

    Powershell 2.0 alphorm.com

    Dcouverte de la console PowerShell + ISE

    Prompt PowerShell

    Console ISE

    Powershell 2.0 alphorm.com

    Prompt RcapitulatifCommande Rsultat

    F1 Rcris la dernire commande lettre par lettre.

    F3 Auto complte une commande.

    F4 Supprime jusquau curseur.

    F5 Remonte dans lhistorique des commandes.

    F7 Affiche lhistorique des commandes. (CTRL+F7 lefface)

    F8 Auto complte votre ligne avec votre historique.

    F9 Spcifie une ligne de lhistorique prcise (F7)

    Tab / Shift + tab Auto complte votre commande.

    Flche haut / bas Navigue dans lhistorique des commandes. (F7)

    chappe Efface la ligne entire.

    CTRL + Fleche gauche / droite

    Navigue sur la ligne du prompt mot par mot.

    Commande Rsultat

    Alt+ espace +E Navigue vers menu dition.

    CTRL+C Cesse lexcution de la commande en cours.

    CTRL+S Pause laffichage en cours.

    CTRL + end Supprime tout depuis le curseur.

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 10

    Powershell 2.0 alphorm.com

    ISE Rcapitulatif

    Commande Rsultat

    CTRL+O Ouvre un script.

    CTRL+N Nouveau script.

    CTRL+S Sauvegarde le script.

    CTRL+T Nouvel onglet.

    CTRL+W Nouvel onglet rseau.

    F5 Excution du code en cours.

    F8 Excution dune slection du code.

    Powershell 2.0 alphorm.com

    Ce quon a couvert

    1. Quest-ce PowerShell ?

    2. Les diffrents versions existantes de PowerShell.

    3. Prrequis pour ncessaire pour linstallation de PowerShell.

    4. Prsentation du prompt, et de lenvironnement de Scripting intgr.

    5. Navigation et raccourcis clavier.

    6. Dcouverte des premires commandes de base.

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 11

    Powershell 2.0 alphorm.com

    Introduction au langage

    Apprentissage de PowerShellInitiation Windows PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

    Powershell 2.0 alphorm.com

    Plan Vous connaissez dj le PowerShell !

    Prsentation de la structure et dcouverte premires commandes

    Comment trouver de laide ?

    Les variables

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 12

    Powershell 2.0 alphorm.com

    Le PowerShell vous connaissez dj !

    Les aliases

    Dir

    del

    Pusd

    Pwd

    Cls

    Powershell 2.0 alphorm.com

    Structure des commandes

    Verbe-nom

    Get-help

    Get-Command

    Set-wmiObject

    Move-Adcomputer

    Read-host

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 13

    Powershell 2.0 alphorm.com

    Comment trouver de laide ? Get-help

    Get-command

    GetGetGetGet----membermembermembermember (gm)

    Internet : PowerShell + commande (ou action a raliser)

    Powershell 2.0 alphorm.com

    Les variables : Rcapitulatif

    Nom Type Exemple

    String [string] Texte Bonjour

    Array [array]Tableau Bonjour, Bonsoir, salut

    Integer [int]Entier 1, 400, 1000, 14

    Double [double]Double

    14.456, 1.5, 78,4 etc

    Date [date]Une date

    11/10/2013, etc

    Hashtable @{} Jean;25Michel;23Hamid;30

    Cmdlets

    Get-variable

    Set-variable

    Clear-variable

    Remove-variable

    New-variable

    assignation

    $=

    $=

    $=

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 14

    Powershell 2.0 alphorm.com

    Ce quon a couvert Prsentation de la structure et dcouverte des premires commandes

    Comment trouver de laide.

    Les variables.

    Powershell 2.0 alphorm.com

    Oprateurs et expressions

    Apprentissage de PowerShellInitiation Windows PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 15

    Powershell 2.0 alphorm.com

    Plan Oprateurs arithmtiques

    Oprateurs dassignation

    Oprateurs de comparaison

    Oprateurs de traitement de texte

    Oprateurs logiques

    Powershell 2.0 alphorm.com

    Oprateurs Arythmtiques

    Operateur Signification

    + Addition

    - Soustraction

    / Division

    * multiplication

    % Modulo

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 16

    Powershell 2.0 alphorm.com

    Oprateurs de dassignationOperateur Signification

    = Assigniation.

    += Assignation an gardant la valeur

    precedente.

    -= Sous trait en reasignant le

    resultat.

    /= Divise en reasignant le

    resultat.

    %= Divise et assigne le modulo la

    variable.

    Powershell 2.0 alphorm.com

    Oprateurs de comparaison

    Operateur Signification Variante sensible la casse

    Variante insensible

    la casse

    -eq Equal -ceq -ieq

    -ne Not equal -cne -ine

    -gt Greather then -cgt -igt

    -ge Greather or equal

    -cge -ige

    -lt Less then -clt -ilt

    -le Less or equal -cle -ile

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 17

    Powershell 2.0 alphorm.com

    Les oprateurs de traitement de textOperateur Significati

    onVariante

    sensible la casse

    Variante insensible la casse

    -like comme -clike -ilike

    -notlike Pas comme -cnotlike -inotlike

    -match Correspond -cmatch -imatch

    -notmatch Correspond pas

    -cnotmatch -inotmatch

    Operateur Signification

    -replace remplace

    -join concatne

    -split Spare

    Operateur Signification

    -contains Contient

    -Notcontains Ne contientpas

    Powershell 2.0 alphorm.com

    Les oprateurs logiques

    Operateur Signification Variante Bits

    -and et -band

    -or ou -bor

    -not non -bnot

    -xor Ou exclusif -bxor

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 18

    Powershell 2.0 alphorm.com

    Ce quon a couvert Oprateurs arithmtiques

    Oprateurs dassignation

    Oprateurs de comparaison

    Oprateurs de traitement de texte

    Oprateurs logiques

    Lien:http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

    Powershell 2.0 alphorm.com

    Les providers

    Apprentissage de PowerShellInitiation Windows PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 19

    Powershell 2.0 alphorm.com

    Plan Que sont les providers ?

    Pourquoi utiliser les providers ?

    Dmonstration

    Powershell 2.0 alphorm.com

    Les providers Que sont les providers ?

    Cest un adaptateur PowerShell. Cela permet dutiliser certaines parties du system de la mme manire que lutilisation du FileSystem sous dos par exemple.

    Pourquoi utiliser les providers ?

    Cela permet de naviguer et deffectuer des oprations de base dans un systme (comme le registre Windows) de la mme manire que dans un sytme de fichier.

    Les commandes utiliser dans les providers

    Get-Item, Copy-Item, Remove-Item

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 20

    Powershell 2.0 alphorm.com

    Les providers Dmonstration :Dmonstration :Dmonstration :Dmonstration :

    Les providers disponibles

    Les commandes utiliser au sein des providers

    Un exemple concret

    Powershell 2.0 alphorm.com

    Ce quon a couvert Pourquoi utiliser les providers

    Les providers disponibles

    Les Commandes

    Get-Item, Clear-Item, Remove-Item

    Le chargement dun module rajoute galement un provider supplmentaire (Module Active Directory)

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 21

    Powershell 2.0 alphorm.com

    La gestion du flux

    Apprentissage de PowerShellInitiation Windows PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

    Powershell 2.0 alphorm.com

    Plan Le pipe

    La rdaction de one liners laide du Pipe

    Les operateur de conditions

    Les boucles

    Les switchs

    Dmonstration

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 22

    Powershell 2.0 alphorm.com

    Le pipe | A quoi a sert ?

    Export vers des fichiers externes

    Piper des rsultats vers dautres commandes

    (Permet la rdaction de one liners )

    DmonstrationDmonstrationDmonstrationDmonstration

    Powershell 2.0 alphorm.com

    Les oprateurs de condition

    DmonstrationDmonstrationDmonstrationDmonstration

    Les oprateurs de conditions

    Signification

    If Si

    Elseif sinon si

    else sinon

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 23

    Powershell 2.0 alphorm.com

    Les boucles

    DmonstrationDmonstrationDmonstrationDmonstration

    Les boucles Signification

    While Tant que

    Do while Fait tantque

    Do until Fait jusqu

    For Pour

    Foreach Pour chaque

    Powershell 2.0 alphorm.com

    Les switchs Dans quel cas utiliser un switch

    La structure du switch

    DmonstrationDmonstrationDmonstrationDmonstration

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 24

    Powershell 2.0 alphorm.com

    Ce quon a couvert Le pipe

    La rdaction de one liners laide du Pipe

    Les operateur de conditions

    Les boucles

    Les switchs

    Powershell 2.0 alphorm.com

    Import de donnes externes

    Apprentissage de PowerShellInitiation Windows PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 25

    Powershell 2.0 alphorm.com

    Plan

    Pourquoi importer des donnes ?

    Import/export depuis diffrents supports:

    Fichiers textes

    Fichiers CSV

    Fichiers XML

    Export HTML

    Powershell 2.0 alphorm.com

    Pourquoi importer/exporter des donnes ? Import:

    Pour faire un travail de moyen / grande chelle.

    Traiter des donnes fournies par dautres services.

    Afin de garantir une base de travail stable.

    Export:

    Exporter des rapports.

    Une nouvelle base de travail.

    Etc

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 26

    Powershell 2.0 alphorm.com

    Fichiers textes: Import

    Get-content

    Export

    Out-file (>, >>)

    Possibilit de lire le fichier pendant que Out-file lutilise)

    Set-content

    Attendre la fin de set-content pour pouvoir lire le fichier

    Add-content

    Powershell 2.0 alphorm.com

    Fichiers CSV: Import

    Import-csv

    Export

    Export-csv

    Dans le Shell

    ConvertTo-CSV

    ConverTFrom-CSV

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 27

    Powershell 2.0 alphorm.com

    Fichiers XML: Import

    Import-CLIXML (Objets exports avec Export-CliXml)

    [xml]$ContenueXml = Get-content path $FichierXml

    Export

    Export-CliXML

    $variable.save(C:\alpormation\configuration\Datas.xml)

    Dans le Shell

    ConvertTo-XML

    Powershell 2.0 alphorm.com

    Export-html Export

    ExportTo-HTML

    Modifier le style (Css)

    Head

    Body

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 28

    Powershell 2.0 alphorm.com

    Ce quon a couvert Import/export depuis diffrents supports:

    Fichiers textes.

    Fichiers CSV.

    Fichiers XML

    Fichiers HTML

    A retenir :

    Caster : [XML]$MaVariable = Get-Content path c:\fichier.xml

    Rapports ConvertTo-Html

    Powershell 2.0 alphorm.com

    Les fonctions et les filtres

    Le scriptingLutilisation avance de Windows PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 29

    Powershell 2.0 alphorm.com

    Plan Quest ce quune fonction ?

    Quest ce quun filter ?

    Les diffrences entre une fonction et un filter.

    Quand utiliser une fonction, quand utiliser un filter ?

    Powershell 2.0 alphorm.com

    Les fonctions : La thorie Utilit:

    Sexcute seulement une fois que tous les lments du pipe sont chargs.

    Permet dcrire et de nommer un bout de code qui peut tre rappeler a tout moment.

    Permet deffectuer une action qui va ou non, retourner une valeur / object

    Gain despace au sein du code.

    Organisation du code (Clart du code).

    Gain de temps lors du codage (Rutilisabilit) .

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 30

    Powershell 2.0 alphorm.com

    Les Filters : La thorie Utilit

    Commence excut du code ds quelle reoit du contenue

    Sutilise de la mme manire quune fonction.

    A exactement les mme avantages quune fonction.

    Structure du code

    Gain de place, gain de temps, rutilisation de code etc..

    DmonstrationDmonstrationDmonstrationDmonstration

    Powershell 2.0 alphorm.com

    Ce quon a couvert Quest ce quune fonction ?

    Quest ce quun filter ?

    Les diffrences entre une fonction et un filter

    Quand utiliser une fonction, quand utiliser un filter ?

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 31

    Powershell 2.0 alphorm.com

    La gestion derreur

    Le scriptingLutilisation avance de Windows PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

    Powershell 2.0 alphorm.com

    Introduction

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 32

    Powershell 2.0 alphorm.com

    Plan ExecutionPolicy

    Les diffrents types derreurs.

    Le comportement de PowerShell face une erreur.

    Le dtail dune erreur.

    Prvoir les erreurs, et agir en consquence.

    Powershell 2.0 alphorm.com

    ExecutionPolicy

    Valeur Comportement lors de la rencontre dune erreur

    Resitricted Impossible dxcuter des scripts. Possibilit dutiliser le prompt uniquement (mode interactif).

    AllSigned Seulement les scripts qui sont signs peuvent tre excuts.

    Remote signed Les scripts tlchargs doivent tre signs avant dtre excuts.

    unrestricted Pas de restrictions. (demande de confirmation avant excution dun script).

    Bypass Aucune restriction, pas de demande de confirmation.

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 33

    Powershell 2.0 alphorm.com

    Les diffrent types derreurs Les erreurs non-critiques (Non-Terminating).

    Les erreurs critiques (Terminating).

    Powershell 2.0 alphorm.com

    Le comportement de PowerShell face une erreur

    La variable $errorractionpreferenceerrorractionpreferenceerrorractionpreferenceerrorractionpreference

    Valeur Comportement lors de la rencontre dune erreur

    SilentlyContinue Le script continue son excution sans affichage.

    Continue Le script continue son excution, tout en affichant lerreur.

    Stop Le script sintrompt (Erreur critique).

    Inquire Demande comment ce comporter.

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 34

    Powershell 2.0 alphorm.com

    Le dtail dune erreur. Le contenu de la variable $errorerrorerrorerror .

    Powershell 2.0 alphorm.com

    Prvoir les erreurs, et agir en consquence $?

    Trap {}

    Try{}, Catch {}, Finally {}

    Throw {}

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 35

    Powershell 2.0 alphorm.com

    Ce quon a couvert Les erreurs critiques et non-crtitiques

    Les differentes variables derreurs:

    $erroractionpreference

    $error

    $?

    Ce que contient finalement une erreur cest un object !

    Comment prvoir une erreur

    try, catch, finally

    Throw

    Powershell 2.0 alphorm.com

    Les fonctions avances

    Le scriptingLutilisation avance de Windows PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 36

    Powershell 2.0 alphorm.com

    Plan Quest ce quune fonction avanc?

    Les CmdletBinding attributs

    Les Parameter attributs

    Documenter notre fonction

    Powershell 2.0 alphorm.com

    Les fonctions avances: La thorie Quest ce quune fonction avanc?

    Identique une fonction normal sauf quelle permet plus de contrle

    Intgration de fonctionalits natives tel que le Verbose ou encore le -debug

    Control des paramtres

    - Permet la validation des paramtres

    - Cration daide

    - Intgration daliases

    - Permettre le paramtre de provenir du pipe

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 37

    Powershell 2.0 alphorm.com

    Les CmdletBinding attributs Utilit

    Doit ce mettre toujours avant la section Param()

    Permet lactivation des fonctionalits natives tel que le verbose, -whatif, -debug, -confirm

    DmonstrationDmonstrationDmonstrationDmonstration....

    Powershell 2.0 alphorm.com

    Les Parameter attributs Utilit

    Permet de mettre de conditions de validation, et de control sur les diffrents paramtres de nos fonctions

    Mandatory

    Force ou non quun valeur soit pass pour ce paramtre

    Position

    Permet de dfinir une position fixe pour un certain paramtre

    ValueFromPipeLine

    Permet de daccepter ou non, quun paramtre provienne du pipe

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 38

    Powershell 2.0 alphorm.com

    Les Parameter attributs (suite) ValueFromPipeLineByPropertyName

    Permet la de passer des paramtre via le pipe portant le mme nom (exemple : ComputerName )

    ValueFromRemainingArguments

    Permet dutiliser ou non les arguments restant contenue dans le pipe

    HelpMessage

    Permet de spcifier un message daide.

    ValidateSet

    ValidateScript

    Powershell 2.0 alphorm.com

    Documenter notre fonction Les commentaires

    Lutilisation du signe #

    Lutilisation dun champ de commentaire plus large :

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 39

    Powershell 2.0 alphorm.com

    Le comment-based help Comment crer une aide professionnel

    Il sagit de mettre des tags linterieur dun champ de commentaire

    .SYNOPSIS: Permet de spcifier en une phrase courte lutilit du script ou de la fonction

    .DESCRIPTION: La description est gnralement plus long. Elle permetdcrire ce que le script fait en dtail

    .PARAMETER NomDuParametre: Permet dafficher des information concernant les paramtres de notre fonction (exemple : Computername)

    .EXAMPLE: Permet de spcifier un exemple prcis concernant lutilisation de la fonction / script.

    Powershell 2.0 alphorm.com

    Ce quon a couvert Quest ce quune fonction avanc?

    Les CmdletBinding attributs

    Les Parameter attributs

    Documenter notre fonction

    Les commentaires

    Le comment-based help

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 40

    Powershell 2.0 alphorm.com

    Les modules

    Le travail grande chelleLutilisation avance de Windows PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

    Powershell 2.0 alphorm.com

    Plan Quest-ce un module PowerShell ?

    Les diffrents types de modules existants.

    Comment crer son propre module.

    Les Modules manifest.

    Comment crer son propre manifest.

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 41

    Powershell 2.0 alphorm.com

    Quest ce un module ? Permet de regrouper un grand nombe de fonctions dans un mme

    endroit.

    Facilite la distribution.

    Facilite laccs des nouvelles fonctionalits qui ne sont pas toujoursncessaires.

    Facilite le partage.

    Permet la standardisation de fonctions de base.

    Powershell 2.0 alphorm.com

    Les diffrents types de modules existants Binary modules

    Code crit en C#

    Extension .DLL

    Script modules

    Bass sur des fichiers textes contenants des fonctions

    Extension .PSM1

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 42

    Powershell 2.0 alphorm.com

    Les commandes de base Get-module

    Import-module

    Remove-module

    DmonstrationDmonstrationDmonstrationDmonstration

    Powershell 2.0 alphorm.com

    Comment crer un script module Fichier text

    Fonctions

    Extension .psm1

    DmonstrationDmonstrationDmonstrationDmonstration

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 43

    Powershell 2.0 alphorm.com

    Les modules manifest Quest un module manifest ?

    A t crer pour pouvoir crer de modules de manire pouvoir les distribuer a grande chelle

    Contiendras les informations tel que : Lauteur, le copyright, Nom de lentreprise, version de PowerShell a utiliser etc

    Cest un fichier avec lextension .psd1

    DmonstrationDmonstrationDmonstrationDmonstration

    Powershell 2.0 alphorm.com

    Ce quon a couvert Quest-ce un module PowerShell ?

    Les diffrents types de modules existants

    Les Modules manifest

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 44

    Powershell 2.0 alphorm.com

    Lexcution distance

    Le travail grande chelleLutilisation avance de Windows PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

    Powershell 2.0 alphorm.com

    Plan: Lexcution distance Les prrequis

    Les commandes offrant nativement des fonctionnalits dexcution a distance.

    Les connexions Non-Persistantes

    Les connexions Persistantes Interactives

    Les connexions Persistantes Non-interactives

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 45

    Powershell 2.0 alphorm.com

    Les prrequis Windos remote management (WINRM) Dsactive par dfaut.

    Web Services for management (WS-MAN)

    HTTP, HTTPS (WINRM 2.0 5985)

    Par dfaut, il est possible pour toute machine equipe de PowerShell 2.0 dinitier des connections distances.

    Certaines conditions doivent tre respectes pour pouvoir accepter ses connections.

    Powershell 2.0 alphorm.com

    Les prrequis Mise en place Une machine en workgroup

    Enable-PsRemoting

    Active le service WinRM, Intgre Set-WSMANQuickConfig

    Dploiement en grande chelle(GPO)

    Computer Configuration->Policies->Administrative templates->Windows Components

    Configuration Policies WindowsSettings SecuritySettings SystemServicesWindows Windows remote management = Automatic

    TOUJOURSTOUJOURSTOUJOURSTOUJOURS ouvrir en tant que Administrator

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 46

    Powershell 2.0 alphorm.com

    Les commandes et le remoting Beaucoup de commandes permettent dj de faire de lexecution a

    distance via -computername

    DmonstrationDmonstrationDmonstrationDmonstration

    Powershell 2.0 alphorm.com

    Cmdlet avec -computerName Get-WinEvent

    Get-Counter

    Get-EventLog

    Clear-EventLog

    Write-EventLog

    Limit-EventLog

    Show-EventLog

    New-EventLog

    Remove-EventLog

    Get-WmiObject

    Remove-EventLog

    Get-WmiObject

    Get-Process

    Get-Service

    Set-Service

    Get-HotFix

    Restart-Computer

    Stop-Computer

    Add-Computer

    Add-Computer

    Remove-Computer

    Rename-Computer

    Reset-ComputerMachinePassword

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 47

    Powershell 2.0 alphorm.com

    Les connections Non-Persistantes Non-interactives

    Invoke-command

    Lexcution dune seule commande voir dun script distance.

    Fonctionne en 3 tapes:

    tablissement de le connexion

    Envois de la commande / script

    Fermeture de la connexion

    DmonstrationDmonstrationDmonstrationDmonstration

    Powershell 2.0 alphorm.com

    Les connections Non-Persistantes Non-interactives

    Les inconvnients de invokeinvokeinvokeinvoke----commandcommandcommandcommand

    Est peut tre simple dutilisation mais la cration de connexion, vrification de version etc est lourd pour los.

    Ne permet pas de travailler de manire interactive

    Impossibilit de garder des variables / fonctions charges en mmoire

    Avantages

    Permet de deployer un script /commandes sur plusieurs machines distantes

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 48

    Powershell 2.0 alphorm.com

    Connection Persistante interactive

    Possibilit de crer une session, executer des commandes, scripts, de quitter la session, puis de reprendre la session la ou elle stait arrter.

    EnterEnterEnterEnter----PsSessionPsSessionPsSessionPsSession

    DmonstrationDmonstrationDmonstrationDmonstration

    Powershell 2.0 alphorm.com

    Connexion Persistante Non-interactive

    Ouvrir et fermer la connexion aprs chaque excution de commande / script est lourd.

    Permet de crer une connexion persistante

    Elle est non-interactive (ne permet pas de travailler sur le retour des commandes)

    NewNewNewNew----PsSessionPsSessionPsSessionPsSession

    DmonstrationDmonstrationDmonstrationDmonstration

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 49

    Powershell 2.0 alphorm.com

    TroubleShooting Service WINRM toujours sur automatic

    Par dfaut pour OS serveurs.

    Pas pour OS clients.

    Powershell 2.0 alphorm.com

    Ce quon a couvert Les prrequis

    Les commandes offrant nativement des fonctionnalits dexcution a distance.

    Les connexions Non-Persistantes

    Les connexions Persistantes Non-interactives

    Les connexions Persistantes Interactives

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 50

    Powershell 2.0 alphorm.com

    Lexcution en tche de fond : Les jobs

    Le travail grande chelleLutilisation avance de Windows PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

    Powershell 2.0 alphorm.com

    Plan

    Introduction

    Un job , cest quoi ?

    Les commandes

    Commandes natives

    Cmdlets PowerShell

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 51

    Powershell 2.0 alphorm.com

    Introduction : Dir C:\ -recurse

    Powershell 2.0 alphorm.com

    Un job, cest quoi ? Cela va nous permettre de faire dxecuter des commandes en arrire

    plan, et nous librer le prompt immdiatement.

    Trs utile lorsquon utilise des commandes / scripts qui ont besoin de beaucoup de temps pour finir

    .

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 52

    Powershell 2.0 alphorm.com

    Les commandes

    3 faons de crer un job

    Avec AsJob

    Get-WmiObject Asjob (Execute toujours la commande lun apreslautre)

    Invoke-command AsJob

    Cmdlet job

    Start-job ScriptBlock{}

    Powershell 2.0 alphorm.com

    Utilisation:

    Rcupration des rsultats:

    Get-job

    Retourne le statut actual des jobs.

    Receive-job

    Rcupre le rsultat du job.

    Loption keep Permet de garder le retour de la console (Par dfaut le retour est supprim).

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 53

    Powershell 2.0 alphorm.com

    Utilisation: Actions sur un job :

    Get-job

    Remove-job

    Supprime un job.

    Wait-job

    Attends que le job ce termine.

    Receive-job

    Rcupre le rsultat.

    DmonstrationDmonstrationDmonstrationDmonstration

    Powershell 2.0 alphorm.com

    Les jobs enfants

    Get-job id select-object expand childjobs

    DmonstrationDmonstrationDmonstrationDmonstration

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 54

    Powershell 2.0 alphorm.com

    Grer les jobs Il est possible de gerer les jobs en identifiant les jobs parmi une de

    identification suivantes :

    N ID

    Nom du job

    Piper directement le job vers une des commande de gestion

    Commandes disponibles

    Remove-job

    Stop-Job

    Wait-job

    Powershell 2.0 alphorm.com

    Ce quon a couvert Synchrone / Asynchrone

    Diffrences

    Les jobs fonctionnent en 3 tapes

    Lancement du job

    Start-job

    Rcuprer les rsultats / les tats des jobs

    Receive-job

    Grer les jobs

    Get-job, wait-job, remove-job, Stop-Job

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 55

    Powershell 2.0 alphorm.com

    Le WMI

    Crer ses propres cmdletsLa matrise de Windows PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

    Powershell 2.0 alphorm.com

    Plan Quest ce que le WMI ?

    A quoi a vous servir ?

    Quels sont les prrequis ?

    Quelques outils indispensables pour grer le WMI

    Larchitecture WMI en quelques mots

    PowerShell et le WMI

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 56

    Powershell 2.0 alphorm.com

    Quest ce que le WMI ? Windows Management Interface

    Est limplmentation Microsoft du modle de donnes CIM (Common information Model)

    Cest une modle de donnes qui a t crer dans le but de pouvoir manager de manire universelle des systmes divers (PC, Servers, switch, applications system dexploitations etc..)

    WMI (CIM) permet dinteragir avec nimporte quel systme de manire standardis.

    Existe depuis limplmentation depuis Windows 2000 sp2

    Est devenue trs facilement accessible avec larriv de PowerShell

    Powershell 2.0 alphorm.com

    A quoi a vous servir ? Avoir un control sur les lments du systmes offert par des providers

    tel quune application, la base de registre, ou encore le systme dexploitation.

    Avoir accs a des informations / mthodes supplmentaires de lOS.

    Exemple :

    Access informations du BIOS

    Possibilit crer de partages

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 57

    Powershell 2.0 alphorm.com

    Quels sont Les prrequis ? Aucun !

    Si ce nest quil faut avoir les bons outils pour pouvoir explorer WMI

    Powershell 2.0 alphorm.com

    Les outils pour explorer le WMI Ils ont tous des noms surprenant !

    WMI explorer

    http://www.ks-soft.net/hostmon.eng/downpage.htm

    WMI explorer

    Sapien

    WMI explorer ( fait par The powerShell Guy (script powerShell))

    http://jdhitsolutions.com/blog/2013/03/wmi-explorer-from-the-powershell-guy/

    Ou encore WMI explorer

    SolarWinds http://www.solarwinds.com/products/freetools/wmi_monitor/

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 58

    Powershell 2.0 alphorm.com

    WMIExplorer2014 www.sapien.com

    Powershell 2.0 alphorm.com

    Larchitecture WMI en quelques mots Les providers offrent des classes dans les quels ce trouvent des

    instances.

    Chaque instance a des proprits (Nom, Description)

    Et peut avoir des mthodes tel que Stop, start delete etc..

    Dmonstration de WMI explorer et de larchitecture WMIDmonstration de WMI explorer et de larchitecture WMIDmonstration de WMI explorer et de larchitecture WMIDmonstration de WMI explorer et de larchitecture WMI

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 59

    Powershell 2.0 alphorm.com

    PowerShell et le WMI Les commandes PowerShell disponibles permettant de grer le WMI

    Get-WMIObject

    Retourne des objects de WMI

    Set-WmiInstance

    Permet de mettre des valeurs a des proprites WMI

    Invoke-WMIMethod

    Permet dappeler une mthode (WMI)

    Remove-WmiObject

    Permet de supprimer une instance crer de la base WMI

    DmonstrationDmonstrationDmonstrationDmonstration

    Powershell 2.0 alphorm.com

    Ce quon a couvert Quest ce que le WMI ?

    A quoi a vous servir ?

    Quels sont Les prrequis ?

    Quelques outils indispensables pour grer le WMI

    Larchitecture WMI en quelques mots

    PowerShell et le WMI

    Les commandes WMI

    .put()

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 60

    Powershell 2.0 alphorm.com

    Lautomatisation dapplications : Les objets COM

    Crer ses propres cmdletsLa matrise de Windows PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

    Powershell 2.0 alphorm.com

    Plan

    Quest ce quun objet COM ?

    A quoi a vous servir ?

    PowerShell et les objets COM

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 61

    Powershell 2.0 alphorm.com

    Quest ce quun objet COM ?

    Un objet COM est une interface offerte par les fabriquant de logiciels afin doffrir une possibilit dautomatisation sur leurs produits

    Powershell 2.0 alphorm.com

    A quoi a vous servir ?

    Les objets COM va permettre dautomatiser des actions au sein de programmes connues tel que Word, Internet explorer ou bien encore de lexplorateur de fichier.

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 62

    Powershell 2.0 alphorm.com

    PowerShell et les objets COM

    Comment identifier les objets COM existants ?

    Registre

    WMI

    Comment utiliser un objet COM ?

    $Variable = New-Object ComObject

    $Variable | Get-Member

    DmonstrationDmonstrationDmonstrationDmonstration

    Powershell 2.0 alphorm.com

    Ce quon a couvert Quest ce quun objet COM ?

    A quoi a vous servir ?

    PowerShell et les objets COM

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 63

    Powershell 2.0 alphorm.com

    Lexploitation du DotNet

    Crer ses propres cmdletsLa matrise de Windows PowerShell

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

    Powershell 2.0 alphorm.com

    Plan

    Introduction

    Pourquoi utiliser le .NET

    Le vocabulaire

    Comment instancier des objets

    Notre premier fonction base sur DotNet

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 64

    Powershell 2.0 alphorm.com

    Introduction

    Module difficile

    Il y a beaucoup de vocabulaire lie au dveloppement. Ceci dpasse un peu les comptences de base dun simple scripteur

    Powershell 2.0 alphorm.com

    Pourquoi utiliser le .NET ?

    PowerShell offre beaucoup de cmdlets (236 en PowerShell 2.0)

    On peut augmenter ce nombre grce au diffrents modules disponibles (Active Directory, Module Exchange etc..)

    Des fois, ce nest pas suffisant

    PowerShell est bas sur du .NET, donc tout ce qui est faisable en .NET, on peut le faire en PowerShell.

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 65

    Powershell 2.0 alphorm.com

    Le vocabulaire

    ClasseClass

    Methode

    Method

    Propites

    Properties

    Lespace de noms

    NameSpace

    StructClass

    Methode

    Method

    Proprites

    Properties

    InterfacesClass

    Methode

    Method

    Propites

    Properties

    Powershell 2.0 alphorm.com

    Le vocabulaire

    Lespace de nomsNameSpace

    ClasseClass

    Mthode

    Method

    Proprits

    Properties

    Contient des classes

    Lquivalent a une sorte de modle qui contiens des membres(Mthodes & proprits)

    Une mthode retourne une valeur, ou le rsultat dune opration

    Cest comme une variable : cela contient une valeur

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 66

    Powershell 2.0 alphorm.com

    Les classes statiques

    Les classes statiques nont pas besoin dtre instancies. (Et ne peuvent pas ltre).

    Mthodes retournant simplement un rsultat.

    Cest la faon facile dutilser le .NET au sein de PowerShell

    DmonstrationDmonstrationDmonstrationDmonstration

    Powershell 2.0 alphorm.com

    Comment sen servir ?

    ClasseClass

    Methode

    Method

    Propites

    Properties

    Lespace de nomsNameSpace

    Methode

    Method

    Propites

    Properties

    Instanciation

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 67

    Powershell 2.0 alphorm.com

    La jungle des classes

    Comment trouve-t-on les classes / Mthodes / proprits.NET

    Notre moteur de recherch favoris + msdn (ou .NET)

    DemonstrationDemonstrationDemonstrationDemonstration

    Powershell 2.0 alphorm.com

    Les assemblies Une assembly cest un fichier.DLL qui va contenir des classes, qui vont

    pouvoir re chargs dans .NET. Ceci dans un but de gain dspacedisque, et de rutilisabilit du code.

    PowerShell charge beaucoup de composants du framework .NET par dfaut, mais certains lments le sont pas. Il est cpendant possible de les chargs afin davoir accs leur Classes, mthodes et proprits via powershell.

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 68

    Powershell 2.0 alphorm.com

    Ce quon a couvert Introduction

    Pourquoi utiliser le .NET

    Le vocabulaire

    Le framework

    Les espaces de nom

    Les classes (static et non static)

    Mthodes & proprits

    Comment instancier des objets

    Notre premier fonction base sur DotNet

    Powershell 2.0 alphorm.com

    Conclusion

    Stphane van GulickConsultant systmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg

    Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum

  • alphorm.com - support de la Formation Powershell 2.0

    29/12/2013

    INTERDECTION DE TOUTE UTILISATION EN DEHORS D'ALPHORM.COM 69

    Powershell 2.0 alphorm.com

    Conclusion Regard en arrire

    Initiation, approfondissement, specialisation

    Prochaine tapes

    PowerShell 3.0

    PowerShell li aux composants MS (Active directory, Exchange, System Center)

    Scripter le plus possible

    Powershell 2.0 alphorm.com

    Merci !