alphorm.com - formation powershell 2.0
DESCRIPTION
La formation complète est disponible ici: http://www.alphorm.com/tutoriel/formation-en-ligne-powershell-2-0 Cette formation permet aux participants d'acquérir les compétences et connaissances nécessaires pour utiliser Microsoft Windows Powershell afin d'automatiser et d'administrer les serveurs Windows. Stéphane VAN GULICK vous guidera durant cette formation depuis les bases de Powershell, puis les utilisations avancées et finira par vous apprendre comment créer vos propres cmdlets.TRANSCRIPT
![Page 1: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/1.jpg)
Présentation de la formation
Powershell 2.0 alphorm.com™©
Site : http://www.alphorm.comBlog : http://www.ConfigMgrdistrict.comBlog : http://www.Powershelldistrict.comForum : http://www.alphorm.com/forum
formation
Stéphane van GulickConsultant systèmes et automatisationCertifications : MCT, MCITP, MCSAContact : [email protected] : @Stephanevg
![Page 2: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/2.jpg)
Introduction
Powershell 2.0 alphorm.com™©
![Page 3: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/3.jpg)
Plan• Présentation du formateur.
• Prérequis de cette formation.
• Quelques références.
• Présentation de la formation.
Powershell 2.0 alphorm.com™©
![Page 4: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/4.jpg)
Présentation du formateur• Présentation
• Stéphane van Gulick
� Ingénieur Systèmes et automatisation
� MCTS, MCITP EA, MCSA
Blog : http://PowerShellDistrict.com
Powershell 2.0 alphorm.com™©
� Blog : http://PowerShellDistrict.com
� Twitter : @stephanevg
![Page 5: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/5.jpg)
Prérequis de cette formation� Connaissances des systèmes Windows
� Une expérience en Scripting est un plus. (Mais pas obligatoire !)
� Compréhension de principes algorithmiques (tel que les « IF, ELSE, FOREACH) etc..
� Être motivé et enthousiaste !
Powershell 2.0 alphorm.com™©
Être motivé et enthousiaste !
� Lab:
• 1 machine Windows 7
• (une deuxième machine windows 7 pour le chapitre sur le remoting).
![Page 6: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/6.jpg)
Quelques références• Version CHM de l’aide 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 ->
Powershell 2.0 alphorm.com™©
• Livres ->
� Windows PowerShell Guide de référence �
� PowerShell in Action �
![Page 7: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/7.jpg)
Présentation de la formation
3 grandes parties
Chapitres
Powershell 2.0 alphorm.com™©
Chapitres
Modules
![Page 8: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/8.jpg)
Présentation de la formation1. L’initiation au PowerShell 2.0
Chapitre 1 : Introduction à PowerShell
1. Présentation de la formation
2. Introduction générale a Windows PowerShell
Chapitre 2 : L’apprentissage de PowerShell
Powershell 2.0 alphorm.com™©
Chapitre 2 : L’apprentissage de PowerShell
1. Introduction au langage
2. Opérateurs et expressions
3. La gestion du flux (l’utilisation du pipe)
4. Importer des données depuis des supports externes.
![Page 9: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/9.jpg)
Présentation de la formation• Partie 2 : L’utilisation avancé de PowerShell 2.0
� Chapitre 3 L’utilisation avancé de PowerShell : Le scripting
1. Les fonctions et les filtres
2. La gestion d’erreur
3. Les fonctions avancées
Powershell 2.0 alphorm.com™©
3. Les fonctions avancées
� Chapitre 4 : La maitrise de PowerShell en travaillant à grande échelle
1. Les modules
2. L’execution à distance
3. L’execution en tâche de fond : “Les jobs”
![Page 10: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/10.jpg)
Présentation de la formationPartie 3 : Maîtrise de PowerShell 2.0
Chapitre 5: Créer ses propres cmdlets
• Le WMI
• L’automatisation d’applications (Com objects)
• L’exploitation du dotnet
Powershell 2.0 alphorm.com™©
• L’exploitation du dotnet
Conclusion
![Page 11: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/11.jpg)
Are you ready ? ☺
Let’s GO !
Powershell 2.0 alphorm.com™©
Let’s GO !
![Page 12: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/12.jpg)
Introduction générale à
Introduction à Windows PowerShell
Initiation à PowerShell
Powershell 2.0 alphorm.com™©
Introduction générale à Windows PowerShell
Stéphane van GulickConsultant systèmes 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
![Page 13: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/13.jpg)
Plan1. Qu’est-ce PowerShell ?
2. Les différents versions existantes de PowerShell.
3. Prérequis nécessaire pour l’installation / l’utilisation de PowerShell.
4. Présentation du prompt, et de l’environnement de Scripting intégré.
Powershell 2.0 alphorm.com™©
5. Découverte des premières commandes.
![Page 14: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/14.jpg)
Les avantages à utiliser Windows PowerShell ?� Automatisation de tâches récurrentes / répétitives.
� Réduit le temps d’effort nécessaire pour l’accomplissement de cette tâche.
� Permet de paralléliser plusieurs tâches à la fois.
� Réduit la probabilité d’erreur.
� Possibilité de déléguer une tâche plus facilement.
Powershell 2.0 alphorm.com™©
� Possibilité de déléguer une tâche plus facilement.
� Est idéal pour la réutilisabilité.
� Permet d’aller au-delà des limites des interface graphiques.
![Page 15: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/15.jpg)
Présence de Windows PowerShell
Système d’éxploitation 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)
Powershell 2.0 alphorm.com™©
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)
![Page 16: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/16.jpg)
Prérequis à l’installation de Windows PowerShell
Powershell Windows Management framework
Prérequis Lien
2.0 WMF 2.0 Dotnet 2.0 http://support.microsoft.com/kb/968929/fr
Powershell 2.0 alphorm.com™©
b/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
![Page 17: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/17.jpg)
Découverte de la console PowerShell + ISE
Prompt PowerShell
Console ISE
Powershell 2.0 alphorm.com™©
![Page 18: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/18.jpg)
Prompt – RécapitulatifCommande Résultat
F1 Réécris la dernière commande lettre par lettre.
F3 Auto complète une commande.
F4 Supprime jusqu’au curseur.
F5 Remonte dans l’historique des commandes.
F7 Affiche l’historique des commandes. (CTRL+F7 l’efface)
Commande Résultat
Alt+ espace +E Navigue vers menu édition.
CTRL+C Cesse l’exécution de la commande en cours.
CTRL+S Pause l’affichage en cours.
CTRL + end Supprime tout depuis le curseur.
Powershell 2.0 alphorm.com™©
commandes. (CTRL+F7 l’efface)
F8 Auto complète votre ligne avec votre historique.
F9 Spécifie une ligne de l’historique précise (F7)
Tab / Shift + tab Auto complète votre commande.
Flèche haut / bas Navigue dans l’historique des commandes. (F7)
échappe Efface la ligne entière.
CTRL + Fleche gauche / droite
Navigue sur la ligne du prompt mot par mot.
![Page 19: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/19.jpg)
ISE – Récapitulatif
Commande Résultat
CTRL+O Ouvre un script.
CTRL+N Nouveau script.
CTRL+S Sauvegarde le script.
CTRL+T Nouvel onglet.
Powershell 2.0 alphorm.com™©
CTRL+W Nouvel onglet réseau.
F5 Exécution du code en cours.
F8 Exécution d’une sélection du code.
![Page 20: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/20.jpg)
Ce qu’on a couvert
1. Qu’est-ce PowerShell ?
2. Les différents versions existantes de PowerShell.
3. Prérequis pour nécessaire pour l’installation de PowerShell.
4. Présentation du prompt, et de l’environnement de Scripting intégré.
Powershell 2.0 alphorm.com™©
5. Navigation et raccourcis clavier.
6. Découverte des premières commandes de base.
![Page 21: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/21.jpg)
Introduction au langage
Apprentissage de PowerShellInitiation à Windows PowerShell
Powershell 2.0 alphorm.com™©
Introduction au langage
Stéphane van GulickConsultant systèmes 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
![Page 22: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/22.jpg)
Plan• Vous connaissez déjà le PowerShell !
• Présentation de la structure et découverte premières commandes
• Comment trouver de l’aide ?
• Les variables
Powershell 2.0 alphorm.com™©
![Page 23: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/23.jpg)
Le PowerShell vous connaissez déjà !
Les aliases
Dir
del
Pusd
Powershell 2.0 alphorm.com™©
Pusd
Pwd
Cls
![Page 24: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/24.jpg)
Structure des commandes
Verbe-nom
Get-help
Get-Command
Set-wmiObject
Move-
Powershell 2.0 alphorm.com™©
Move-Adcomputer
Read-host
![Page 25: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/25.jpg)
Comment trouver de l’aide ?• Get-help <nom de la commande>
• Get-command
• Get-member (gm)
• Internet : PowerShell + commande (ou action a réaliser)
Powershell 2.0 alphorm.com™©
![Page 26: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/26.jpg)
Les variables : Récapitulatif
Nom Type Exemple
String [string] Texte Bonjour
Array [array]Tableau Bonjour, Bonsoir, salut
Integer [int]Entier 1, 400, 1000, 14
Double [double]Doubl 14.456, 1.5, 78,4 etc…
Cmdlets
Get-variable
assignation
$=<contenue>
$=<commande>
$=<Opération>
Powershell 2.0 alphorm.com™©
Double [double]Double
14.456, 1.5, 78,4 etc…
Date [date]Une date
11/10/2013, etc…
Hashtable @{} Jean;25Michel;23Hamid;30
Get-variable
Set-variable
Clear-variable
Remove-variable
New-variable
![Page 27: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/27.jpg)
Ce qu’on a couvert• Présentation de la structure et découverte des premières commandes
• Comment trouver de l’aide.
• Les variables.
Powershell 2.0 alphorm.com™©
![Page 28: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/28.jpg)
Opérateurs et
Apprentissage de PowerShellInitiation à Windows PowerShell
Powershell 2.0 alphorm.com™©
Opérateurs et expressions
Stéphane van GulickConsultant systèmes 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
![Page 29: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/29.jpg)
Plan• Opérateurs arithmétiques
• Opérateurs d’assignation
• Opérateurs de comparaison
• Opérateurs de traitement de texte
Powershell 2.0 alphorm.com™©
• Opérateurs logiques
![Page 30: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/30.jpg)
Opérateurs Arythmétiques
Operateur Signification
+ Addition
- Soustraction
/ Division
Powershell 2.0 alphorm.com™©
* multiplication
% Modulo
![Page 31: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/31.jpg)
Opérateurs de d’assignationOperateur Signification
= Assigniation.
+= Assignation an gardant la valeur
precedente.
-= Sous trait en
Powershell 2.0 alphorm.com™©
-= Sous trait en reasignant le
resultat.
/= Divise en reasignant le
resultat.
%= Divise et assigne le modulo à la
variable.
![Page 32: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/32.jpg)
Opérateurs de comparaison
Operateur Signification Variante sensible à la casse
Variante insensible à
la casse
-eq Equal -ceq -ieq
-ne Not equal -cne -ine
Powershell 2.0 alphorm.com™©
-gt Greather then -cgt -igt
-ge Greather or equal
-cge -ige
-lt Less then -clt -ilt
-le Less or equal -cle -ile
![Page 33: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/33.jpg)
Les opérateurs 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
Operateur Signification
-replace remplace
-join concatène
-split Sépare
Powershell 2.0 alphorm.com™©
-notmatch Correspond pas
-cnotmatch -inotmatch
-split Sépare
Operateur Signification
-contains Contient
-Notcontains Ne contientpas
![Page 34: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/34.jpg)
Les opérateurs logiques
Operateur Signification Variante Bits
-and et -band
-or ou -bor
Powershell 2.0 alphorm.com™©
-not non -bnot
-xor Ou exclusif -bxor
![Page 35: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/35.jpg)
Ce qu’on a couvert• Opérateurs arithmétiques
• Opérateurs d’assignation
• Opérateurs de comparaison
• Opérateurs de traitement de texte
Powershell 2.0 alphorm.com™©
• Opérateurs logiques
• Lien:http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
![Page 36: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/36.jpg)
Les providers
Apprentissage de PowerShellInitiation à Windows PowerShell
Powershell 2.0 alphorm.com™©
Les providers
Stéphane van GulickConsultant systèmes 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
![Page 37: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/37.jpg)
Plan• Que sont les providers ?
• Pourquoi utiliser les providers ?
• Démonstration
Powershell 2.0 alphorm.com™©
![Page 38: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/38.jpg)
Les providers• Que sont les providers ?
� C’est un adaptateur PowerShell. Cela permet d’utiliser certaines parties du system de la même manière que l’utilisation du FileSystem sous dos par exemple.
• Pourquoi utiliser les providers ?
Powershell 2.0 alphorm.com™©
� Cela permet de naviguer et d’effectuer des opérations de base dans un système (comme le registre Windows) de la même manière que dans un sytème de fichier.
• Les commandes à utiliser dans les providers
• Get-Item, Copy-Item, Remove-Item …
![Page 39: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/39.jpg)
Les providers• Démonstration :
� Les providers disponibles
� Les commandes à utiliser au sein des providers
� Un exemple concret
Powershell 2.0 alphorm.com™©
![Page 40: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/40.jpg)
Ce qu’on a couvert� Pourquoi utiliser les providers
� Les providers disponibles
� Les Commandes
• Get-Item, Clear-Item, Remove-Item
� Le chargement d’un module rajoute également un provider supplémentaire
Powershell 2.0 alphorm.com™©
� Le chargement d’un module rajoute également un provider supplémentaire (Module Active Directory)
![Page 41: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/41.jpg)
La gestion du flux
Apprentissage de PowerShellInitiation à Windows PowerShell
Powershell 2.0 alphorm.com™©
La gestion du flux
Stéphane van GulickConsultant systèmes 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
![Page 42: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/42.jpg)
Plan• Le pipe
� La rédaction de « one liners » à l’aide du « Pipe »
• Les operateur de conditions
• Les boucles
Powershell 2.0 alphorm.com™©
• Les switchs
• Démonstration
![Page 43: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/43.jpg)
Le pipe « | »• A quoi ça sert ?
• Export vers des fichiers externes
• « Piper » des résultats vers d’autres commandes
� (Permet la rédaction de « one liners » )
Powershell 2.0 alphorm.com™©
• Démonstration
![Page 44: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/44.jpg)
Les opérateurs de condition
Les opérateurs de conditions
Signification
If Si
Elseif sinon si
else sinon
Powershell 2.0 alphorm.com™©
• Démonstration
else sinon
![Page 45: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/45.jpg)
Les boucles
Les boucles Signification
While Tant que
Do while Fait tantque
Do until Fait jusqu’à
Powershell 2.0 alphorm.com™©
• Démonstration
Do until Fait jusqu’à
For Pour
Foreach Pour chaque
![Page 46: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/46.jpg)
Les switchs• Dans quel cas utiliser un switch
• La structure du switch
• Démonstration
Powershell 2.0 alphorm.com™©
![Page 47: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/47.jpg)
Ce qu’on a couvert• Le pipe
� La rédaction de « one liners » à l’aide du « Pipe »
• Les operateur de conditions
• Les boucles
Powershell 2.0 alphorm.com™©
• Les switchs
![Page 48: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/48.jpg)
Import de données
Apprentissage de PowerShellInitiation à Windows PowerShell
Powershell 2.0 alphorm.com™©
Import de données externes
Stéphane van GulickConsultant systèmes 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
![Page 49: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/49.jpg)
Plan
•Pourquoi importer des données ?
• Import/export depuis différents supports:
•Fichiers textes
Powershell 2.0 alphorm.com™©
•Fichiers CSV
•Fichiers XML
•Export HTML
![Page 50: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/50.jpg)
Pourquoi importer/exporter des données ?• Import:
� Pour faire un travail de moyen / grande échelle.
� Traiter des données fournies par d’autres services.
� Afin de garantir une base de travail stable.
• Export:
Powershell 2.0 alphorm.com™©
• Export:
� Exporter des rapports.
� Une nouvelle base de travail.
� Etc…
![Page 51: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/51.jpg)
Fichiers textes:• Import
� Get-content
• Export
� Out-file (>, >>)
Possibilité de lire le fichier pendant que Out-file l’utilise)
Powershell 2.0 alphorm.com™©
• Possibilité de lire le fichier pendant que Out-file l’utilise)
� Set-content
• Attendre la fin de set-content pour pouvoir lire le fichier
� Add-content
![Page 52: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/52.jpg)
Fichiers CSV:• Import
� Import-csv
• Export
� Export-csv
Dans le Shell
Powershell 2.0 alphorm.com™©
• Dans le Shell
� ConvertTo-CSV
� ConverTFrom-CSV
![Page 53: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/53.jpg)
Fichiers XML:• Import
� Import-CLIXML (Objets exportés avec Export-CliXml)
� [xml]$ContenueXml = Get-content –path $FichierXml
• Export
Export-CliXML
Powershell 2.0 alphorm.com™©
� Export-CliXML
� $variable.save(C:\alpormation\configuration\Datas.xml)
• Dans le Shell
� ConvertTo-XML
![Page 54: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/54.jpg)
Export-html• Export
� ExportTo-HTML
• Modifier le style (Css)
� Head
Body
Powershell 2.0 alphorm.com™©
� Body
![Page 55: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/55.jpg)
Ce qu’on a couvert• Import/export depuis différents supports:
� Fichiers textes.
� Fichiers CSV.
� Fichiers XML
� Fichiers HTML
Powershell 2.0 alphorm.com™©
� Fichiers HTML
• A retenir :
� Caster : [XML]$MaVariable = Get-Content –path c:\fichier.xml
� « Rapports » ConvertTo-Html
![Page 56: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/56.jpg)
Les fonctions
Le scriptingL’utilisation avancée de Windows PowerShell
Powershell 2.0 alphorm.com™©
Les fonctions et les filtres
Stéphane van GulickConsultant systèmes 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
![Page 57: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/57.jpg)
Plan• Qu’est ce qu’une fonction ?
• Qu’est ce qu’un filter ?
• Les différences entre une fonction et un filter.
• Quand utiliser une fonction, quand utiliser un filter ?
Powershell 2.0 alphorm.com™©
![Page 58: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/58.jpg)
Les fonctions : La théorie• Utilité:
� S’exécute seulement une fois que tous les éléments du pipe sont chargés.
� Permet d’écrire et de nommer un bout de code qui peut être rappeler a tout moment.
� Permet d’effectuer une action qui va ou non, retourner une valeur / object
Powershell 2.0 alphorm.com™©
Permet d’effectuer une action qui va ou non, retourner une valeur / object
� Gain d’espace au sein du code.
� Organisation du code (Clarté du code).
� Gain de temps lors du codage (Réutilisabilité) .
![Page 59: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/59.jpg)
Les Filters : La théorie• Utilité
� Commence à exécuté du code dés qu’elle reçoit du contenue
� S’utilise de la même manière qu’une fonction.
� A exactement les même avantages qu’une fonction.
• Structure du code
Powershell 2.0 alphorm.com™©
• Structure du code
• Gain de place, gain de temps, réutilisation de code etc..
• Démonstration
![Page 60: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/60.jpg)
Ce qu’on a couvert• Qu’est ce qu’une fonction ?
• Qu’est ce qu’un filter ?
• Les différences entre une fonction et un filter
• Quand utiliser une fonction, quand utiliser un filter ?
Powershell 2.0 alphorm.com™©
![Page 61: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/61.jpg)
La gestion d’erreur
Le scriptingL’utilisation avancée de Windows PowerShell
Powershell 2.0 alphorm.com™©
La gestion d’erreur
Stéphane van GulickConsultant systèmes 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
![Page 62: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/62.jpg)
Introduction
Powershell 2.0 alphorm.com™©
![Page 63: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/63.jpg)
Plan• ExecutionPolicy
• Les différents types d’erreurs.
• Le comportement de PowerShell face à une erreur.
• Le détail d’une erreur.
Powershell 2.0 alphorm.com™©
• Prévoir les erreurs, et agir en conséquence.
![Page 64: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/64.jpg)
ExecutionPolicy
Valeur Comportement lors de la rencontre d’une erreur
Resitricted Impossible d’éxécuter des scripts. Possibilité d’utiliser le prompt uniquement (mode interactif).
AllSigned Seulement les scripts qui sont signés peuvent être exécutés.
Remote signed Les scripts téléchargés doivent être signés avant
Powershell 2.0 alphorm.com™©
Remote signed Les scripts téléchargés doivent être signés avant d’être exécutés.
unrestricted Pas de restrictions. (demande de confirmation avant exécution d’un script).
Bypass Aucune restriction, pas de demande de confirmation.
![Page 65: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/65.jpg)
Les différent types d’erreurs• Les erreurs non-critiques (Non-Terminating).
• Les erreurs critiques (Terminating).
Powershell 2.0 alphorm.com™©
![Page 66: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/66.jpg)
Le comportement de PowerShell face à une erreur
• La variable $errorractionpreference
Valeur Comportement lors de la rencontre d’une erreur
SilentlyContinue Le script continue son exécution sans affichage.
Continue Le script continue son exécution, tout en affichant l’erreur.
Stop Le script s’intérompt (Erreur critique).
Powershell 2.0 alphorm.com™©
Stop Le script s’intérompt (Erreur critique).
Inquire Demande comment ce comporter.
![Page 67: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/67.jpg)
Le détail d’une erreur.• Le contenu de la variable $error .
Powershell 2.0 alphorm.com™©
![Page 68: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/68.jpg)
Prévoir les erreurs, et agir en conséquence• $?
• Trap {}
• Try{}, Catch {}, Finally {}
• Throw {}
Powershell 2.0 alphorm.com™©
![Page 69: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/69.jpg)
Ce qu’on a couvert• Les erreurs critiques et non-crtitiques
• Les differentes variables d’erreurs:
� $erroractionpreference
� $error
$?
Powershell 2.0 alphorm.com™©
� $?
• Ce que contient finalement une erreur � c’est un object !
• Comment prévoir une erreur
� try, catch, finally
� Throw
![Page 70: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/70.jpg)
Les fonctions avancées
Le scriptingL’utilisation avancée de Windows PowerShell
Powershell 2.0 alphorm.com™©
Les fonctions avancées
Stéphane van GulickConsultant systèmes 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
![Page 71: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/71.jpg)
Plan• Qu’est ce qu’une fonction avancé?
• Les « CmdletBinding » attributs
• Les « Parameter » attributs
• Documenter notre fonction
Powershell 2.0 alphorm.com™©
![Page 72: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/72.jpg)
Les fonctions avancées: La théorie• Qu’est ce qu’une fonction avancé?
� Identique à une fonction « normal » sauf qu’elle permet plus de contrôle
• Intégration de fonctionalités natives tel que le –Verbose ou encore le -debug
• Control des paramètres
Powershell 2.0 alphorm.com™©
• Control des paramètres
- Permet la validation des paramètres
- Création d’aide
- Intégration d’aliases
- Permettre le paramètre de provenir du « pipe »
![Page 73: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/73.jpg)
Les « CmdletBinding » attributs• Utilité
� Doit ce mettre toujours avant la section Param()
� Permet l’activation des fonctionalités natives tel que le –verbose, -whatif, -debug, -confirm
Powershell 2.0 alphorm.com™©
• Démonstration.
![Page 74: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/74.jpg)
Les « Parameter » attributs• Utilité
� Permet de mettre de conditions de validation, et de control sur les différents paramètres de nos fonctions
• Mandatory
� Force ou non qu’un valeur soit passé pour ce paramètre
Powershell 2.0 alphorm.com™©
� Force ou non qu’un valeur soit passé pour ce paramètre
• Position
� Permet de définir une position fixe pour un certain paramètre
• ValueFromPipeLine
� Permet de d’accepter ou non, qu’un paramètre provienne du pipe
![Page 75: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/75.jpg)
Les « Parameter » attributs (suite)• ValueFromPipeLineByPropertyName
� Permet la de passer des paramètre via le pipe portant le même nom (exemple : « ComputerName »)
• ValueFromRemainingArguments
� Permet d’utiliser ou non les arguments restant contenue dans le pipe
Powershell 2.0 alphorm.com™©
� Permet d’utiliser ou non les arguments restant contenue dans le pipe
• HelpMessage
� Permet de spécifier un message d’aide.
• ValidateSet
• ValidateScript
![Page 76: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/76.jpg)
Documenter notre fonction• Les commentaires
� L’utilisation du signe « # »
� L’utilisation d’un champ de commentaire plus large : « <# « code » #> »
Powershell 2.0 alphorm.com™©
![Page 77: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/77.jpg)
Le « comment-based help »• Comment créer une aide « professionnel »
� Il s’agit de mettre des tags à l’interieur d’un champ de commentaire <# #>
� .SYNOPSIS: Permet de spécifier en une phrase courte l’utilité du script ou de la fonction
� .DESCRIPTION: La description est généralement plus long. Elle permetd’écrire ce que le script fait en détail
Powershell 2.0 alphorm.com™©
.DESCRIPTION: La description est généralement plus long. Elle permetd’écrire ce que le script fait en détail
� .PARAMETER NomDuParametre: Permet d’afficher des information concernant les paramètres de notre fonction (exemple : Computername)
� .EXAMPLE: Permet de spécifier un exemple précis concernant l’utilisation de la fonction / script.
![Page 78: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/78.jpg)
Ce qu’on a couvert• Qu’est ce qu’une fonction avancé?
• Les « CmdletBinding » attributs
• Les « Parameter » attributs
• Documenter notre fonction
Powershell 2.0 alphorm.com™©
� Les commentaires
� Le comment-based help
![Page 79: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/79.jpg)
Les modules
Le travail à grande échelleL’utilisation avancée de Windows PowerShell
Powershell 2.0 alphorm.com™©
Les modules
Stéphane van GulickConsultant systèmes 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
![Page 80: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/80.jpg)
Plan• Qu’est-ce un module PowerShell ?
• Les différents types de modules existants.
• Comment créer son propre module.
• Les Modules manifest.
Powershell 2.0 alphorm.com™©
• Comment créer son propre manifest.
![Page 81: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/81.jpg)
Qu’est ce un module ?• Permet de regrouper un grand nombe de fonctions dans un même
endroit.
• Facilite la distribution.
• Facilite l’accès à des nouvelles fonctionalités qui ne sont pas toujoursnécessaires.
Powershell 2.0 alphorm.com™©
• Facilite le partage.
• Permet la standardisation de fonctions de base.
![Page 82: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/82.jpg)
Les différents types de modules existants• Binary modules
� Code écrit en C#
� Extension .DLL
• Script modules
Basés sur des fichiers textes contenants des fonctions
Powershell 2.0 alphorm.com™©
� Basés sur des fichiers textes contenants des fonctions
� Extension .PSM1
![Page 83: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/83.jpg)
Les commandes de base• Get-module
• Import-module
• Remove-module
Powershell 2.0 alphorm.com™©
• Démonstration
![Page 84: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/84.jpg)
Comment créer un script module• Fichier text
• Fonctions
• Extension .psm1
Powershell 2.0 alphorm.com™©
• Démonstration
![Page 85: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/85.jpg)
Les modules manifest• Qu’est un module manifest ?
� A été créer pour pouvoir créer de modules de manière pouvoir les distribuer a grande échelle
� Contiendras les informations tel que : L’auteur, le copyright, Nom de l’entreprise, version de PowerShell a utiliser etc…
C’est un fichier avec l’extension .psd1
Powershell 2.0 alphorm.com™©
� C’est un fichier avec l’extension .psd1
• Démonstration
![Page 86: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/86.jpg)
Ce qu’on a couvert• Qu’est-ce un module PowerShell ?
• Les différents types de modules existants
• Les Modules manifest
Powershell 2.0 alphorm.com™©
![Page 87: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/87.jpg)
L’exécution à distance
Le travail à grande échelleL’utilisation avancée de Windows PowerShell
Powershell 2.0 alphorm.com™©
L’exécution à distance
Stéphane van GulickConsultant systèmes 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
![Page 88: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/88.jpg)
Plan: L’exécution à distance• Les prérequis
• Les commandes offrant nativement des fonctionnalités d’exécution a distance.
• Les connexions Non-Persistantes
• Les connexions Persistantes Interactives
Powershell 2.0 alphorm.com™©
• Les connexions Persistantes Interactives
• Les connexions Persistantes Non-interactives
![Page 89: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/89.jpg)
Les prérequis� Windos remote management (WINRM) � Désactivée par défaut.
• Web Services for management (WS-MAN)
• HTTP, HTTPS (WINRM 2.0 �5985)
� Par défaut, il est possible pour toute machine eéquipée de PowerShell 2.0 d’initier des connections distances.
Powershell 2.0 alphorm.com™©
� Certaines conditions doivent être respectées pour pouvoir accepter ses connections.
![Page 90: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/90.jpg)
Les prérequis – Mise en place• Une machine en workgroup
� Enable-PsRemoting
• Active le service WinRM, Intègre Set-WSMANQuickConfig
• Déploiement en grande échelle(GPO)
Computer Configuration->Policies->Administrative templates->Windows Components
Powershell 2.0 alphorm.com™©
� Computer Configuration->Policies->Administrative templates->Windows Components
� Configuration � Policies � WindowsSettings � SecuritySettings � SystemServicesWindows � Windows remote management = Automatic
• TOUJOURS ouvrir en tant que “Administrator”
![Page 91: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/91.jpg)
Les commandes et le remoting• Beaucoup de commandes permettent déjà de faire de l’execution a
distance via “-computername”
• Démonstration
Powershell 2.0 alphorm.com™©
![Page 92: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/92.jpg)
Cmdlet avec -computerName• Get-WinEvent
• Get-Counter
• Get-EventLog
• Clear-EventLog
• Remove-EventLog
• Get-WmiObject
• Get-Process
• Get-Service
• Add-Computer
• Remove-Computer
• Rename-Computer
• Reset-ComputerMachineP
Powershell 2.0 alphorm.com™©
• Write-EventLog
• Limit-EventLog
• Show-EventLog
• New-EventLog
• Remove-EventLog
• Get-WmiObject
• Get-Service
• Set-Service
• Get-HotFix
• Restart-Computer
• Stop-Computer
• Add-Computer
ComputerMachinePassword
![Page 93: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/93.jpg)
Les connections Non-Persistantes Non-interactives
� Invoke-command
� L’exécution d’une seule commande voir d’un script à distance.
� Fonctionne en 3 étapes:
• Établissement de le connexion
• Envois de la commande / script
Powershell 2.0 alphorm.com™©
• Envois de la commande / script
• Fermeture de la connexion
� Démonstration
![Page 94: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/94.jpg)
Les connections Non-Persistantes Non-interactives
• Les inconvénients de invoke-command
� Est peut être simple d’utilisation mais la création de connexion, vérification de version etc est lourd pour l’os.
� Ne permet pas de travailler de manière interactive
� Impossibilité de garder des variables / fonctions chargées en mémoire
Powershell 2.0 alphorm.com™©
Impossibilité de garder des variables / fonctions chargées en mémoire
• Avantages
� Permet de deployer un script /commandes sur plusieurs machines distantes
![Page 95: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/95.jpg)
Connection Persistante interactive
• Possibilité de créer une session, executer des commandes, scripts, de quitter la session, puis de reprendre la session la ou elle s’était arrêter.
• Enter-PsSession
• Démonstration
Powershell 2.0 alphorm.com™©
• Démonstration
![Page 96: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/96.jpg)
Connexion Persistante Non-interactive
• Ouvrir et fermer la connexion après chaque exécution de commande / script est lourd.
• Permet de créer une connexion persistante
• Elle est non-interactive (ne permet pas de travailler sur le retour des commandes)
Powershell 2.0 alphorm.com™©
• New-PsSession
• Démonstration
![Page 97: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/97.jpg)
TroubleShooting• Service WINRM toujours sur automatic
� Par défaut pour OS serveurs.
� Pas pour OS clients.
Powershell 2.0 alphorm.com™©
![Page 98: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/98.jpg)
Ce qu’on a couvert• Les prérequis
• Les commandes offrant nativement des fonctionnalités d’exécution a distance.
• Les connexions Non-Persistantes
• Les connexions Persistantes Non-interactives
Powershell 2.0 alphorm.com™©
• Les connexions Persistantes Non-interactives
• Les connexions Persistantes Interactives
![Page 99: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/99.jpg)
L’exécution en tâche de
Le travail à grande échelleL’utilisation avancée de Windows PowerShell
Powershell 2.0 alphorm.com™©
L’exécution en tâche de fond : Les jobs
Stéphane van GulickConsultant systèmes 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
![Page 100: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/100.jpg)
Plan
• Introduction
•Un « job », c’est quoi ?
•Les commandes
Powershell 2.0 alphorm.com™©
�Commandes natives
�Cmdlets PowerShell
![Page 101: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/101.jpg)
Introduction : Dir C:\ -recurse
Powershell 2.0 alphorm.com™©
![Page 102: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/102.jpg)
Un job, c’est quoi ?• Cela va nous permettre de faire d’éxecuter des commandes en arrière
plan, et nous libérer le prompt immédiatement.
• Très utile lorsqu’on utilise des commandes / scripts qui ont besoin de beaucoup de temps pour finir
.
Powershell 2.0 alphorm.com™©
![Page 103: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/103.jpg)
Les commandes
• 3 façons de créer un ‘job’
� Avec –AsJob
• Get-WmiObject –Asjob (Execute toujours la commande l’un apresl’autre)
• Invoke-command –AsJob
Powershell 2.0 alphorm.com™©
• Invoke-command –AsJob
• Cmdlet job
� Start-job –ScriptBlock{…}
![Page 104: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/104.jpg)
Utilisation:
• Récupération des résultats:
• Get-job
� Retourne le statut actual des jobs.
• Receive-job
Powershell 2.0 alphorm.com™©
• Receive-job
� Récupère le résultat du job.
� L’option –keep Permet de garder le retour de la console (Par défaut le retour est supprimé).
![Page 105: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/105.jpg)
Utilisation:• Actions sur un job :
� Get-job
� Remove-job
• Supprime un job.
� Wait-job
Powershell 2.0 alphorm.com™©
� Wait-job
• Attends que le job ce termine.
� Receive-job
• Récupère le résultat.
� Démonstration
![Page 106: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/106.jpg)
Les jobs enfants
• Get-job –id <JobId> ¦ select-object –expand childjobs
• Démonstration
Powershell 2.0 alphorm.com™©
![Page 107: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/107.jpg)
Gérer 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
Powershell 2.0 alphorm.com™©
� Piper directement le job vers une des commande de gestion
• Commandes disponibles
� Remove-job
� Stop-Job
� Wait-job
![Page 108: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/108.jpg)
Ce qu’on a couvert• Synchrone / Asynchrone
� Différences
• Les jobs fonctionnent en 3 étapes
� Lancement du job
Start-job
Powershell 2.0 alphorm.com™©
• Start-job
� Récupérer les résultats / les états des jobs
• Receive-job
� Gérer les jobs
• Get-job, wait-job, remove-job, Stop-Job
![Page 109: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/109.jpg)
Le WMI
Créer ses propres cmdletsLa maîtrise de Windows PowerShell
Powershell 2.0 alphorm.com™©
Le WMI
Stéphane van GulickConsultant systèmes 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
![Page 110: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/110.jpg)
Plan• Qu’est ce que le WMI ?
• A quoi ça vous servir ?
• Quels sont les prérequis ?
• Quelques outils indispensables pour gérer le WMI
Powershell 2.0 alphorm.com™©
• L’architecture WMI en quelques mots
• PowerShell et le WMI
![Page 111: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/111.jpg)
Qu’est ce que le WMI ?• Windows Management Interface
� Est l’implémentation Microsoft du modèle de données CIM (Common information Model)
� C’est une modèle de données qui a été créer dans le but de pouvoir manager de manière universelle des systèmes divers (PC, Servers, switch, applications system d’exploitations etc..)
Powershell 2.0 alphorm.com™©
system d’exploitations etc..)
• WMI (CIM) permet d’interagir avec n’importe quel système de manière standardisé.
� Existe depuis l’implémentation depuis Windows 2000 sp2
� Est devenue très facilement accessible avec l’arrivé de PowerShell
![Page 112: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/112.jpg)
A quoi ça vous servir ?• Avoir un control sur les éléments du systèmes offert par des ‘providers’
tel qu’une application, la base de registre, ou encore le système d’exploitation.
• Avoir accès a des informations / méthodes supplémentaires de l’OS.
• Exemple :
Powershell 2.0 alphorm.com™©
� Access informations du BIOS
� Possibilité créer de partages
![Page 113: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/113.jpg)
Quels sont Les prérequis ?• Aucun !
� Si ce n’est qu’il faut avoir les bons outils pour pouvoir explorer WMI
Powershell 2.0 alphorm.com™©
![Page 114: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/114.jpg)
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
Powershell 2.0 alphorm.com™©
• 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/
![Page 115: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/115.jpg)
WMIExplorer2014 � www.sapien.com
Powershell 2.0 alphorm.com™©
![Page 116: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/116.jpg)
L’architecture WMI en quelques mots• Les ‘providers’ offrent des ‘classes’ dans les quels ce trouvent des
‘instances’.
� Chaque instance a des propriétés (Nom, Description)
• Et peut avoir des ‘méthodes’ tel que Stop, start delete etc..
Powershell 2.0 alphorm.com™©
• Démonstration de WMI explorer et de l’architecture WMI
![Page 117: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/117.jpg)
PowerShell et le WMI• Les commandes PowerShell disponibles permettant de gérer le WMI
� Get-WMIObject
• Retourne des objects de WMI
� Set-WmiInstance
• Permet de mettre des valeurs a des propriétées WMI
Powershell 2.0 alphorm.com™©
• Permet de mettre des valeurs a des propriétées WMI
� Invoke-WMIMethod
• Permet d’appeler une méthode (WMI)
� Remove-WmiObject
• Permet de supprimer une instance créer de la base WMI
� Démonstration
![Page 118: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/118.jpg)
Ce qu’on a couvert• Qu’est ce que le WMI ?
• A quoi ça vous servir ?
• Quels sont Les prérequis ?
• Quelques outils indispensables pour gérer le WMI
Powershell 2.0 alphorm.com™©
• L’architecture WMI en quelques mots
• PowerShell et le WMI
� Les commandes WMI
• .put()
![Page 119: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/119.jpg)
L’automatisation d’applications :
Créer ses propres cmdletsLa maîtrise de Windows PowerShell
Powershell 2.0 alphorm.com™©
L’automatisation d’applications : Les objets COM
Stéphane van GulickConsultant systèmes 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
![Page 120: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/120.jpg)
Plan
• Qu’est ce qu’un objet COM ?
• A quoi ça vous servir ?
• PowerShell et les objets COM
Powershell 2.0 alphorm.com™©
![Page 121: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/121.jpg)
Qu’est ce qu’un objet COM ?
• Un objet COM est une interface offerte par les fabriquant de logiciels afin d’offrir une possibilité d’automatisation sur leurs produits
Powershell 2.0 alphorm.com™©
![Page 122: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/122.jpg)
A quoi ça vous servir ?
• Les objets COM va permettre d’automatiser des actions au sein de programmes connues tel que Word, Internet explorer ou bien encore de l’explorateur de fichier.
Powershell 2.0 alphorm.com™©
![Page 123: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/123.jpg)
PowerShell et les objets COM
• Comment identifier les objets COM existants ?
� Registre
� WMI
• Comment utiliser un objet COM ?
Powershell 2.0 alphorm.com™©
• Comment utiliser un objet COM ?
� $Variable = New-Object –ComObject <NomComObjet>
� $Variable | Get-Member
• Démonstration
![Page 124: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/124.jpg)
Ce qu’on a couvert• Qu’est ce qu’un objet COM ?
• A quoi ça vous servir ?
• PowerShell et les objets COM
Powershell 2.0 alphorm.com™©
![Page 125: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/125.jpg)
L’exploitation du DotNet
Créer ses propres cmdletsLa maîtrise de Windows PowerShell
Powershell 2.0 alphorm.com™©
L’exploitation du DotNet
Stéphane van GulickConsultant systèmes 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
![Page 126: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/126.jpg)
Plan
• Introduction
• Pourquoi utiliser le .NET
• Le vocabulaire
• Comment instancier des objets
Powershell 2.0 alphorm.com™©
• Comment instancier des objets
• Notre premier fonction basée sur DotNet
![Page 127: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/127.jpg)
Introduction
• Module difficile
• Il y a beaucoup de vocabulaire liée au développement. Ceci dépasse un peu les compétences de base d’un simple « scripteur »
Powershell 2.0 alphorm.com™©
![Page 128: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/128.jpg)
Pourquoi utiliser le .NET ?
• PowerShell offre beaucoup de cmdlets (236 en PowerShell 2.0)
• On peut augmenter ce nombre grâce au différents modules disponibles (Active Directory, Module Exchange etc..)
• Des fois, ce n’est pas suffisant �
Powershell 2.0 alphorm.com™©
• Des fois, ce n’est pas suffisant
• PowerShell est basé sur du .NET, donc tout ce qui est faisable en .NET, on peut le faire en PowerShell.
![Page 129: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/129.jpg)
Le vocabulaire
Classe
L’espace de noms
NameSpace
Struct Interfaces
Powershell 2.0 alphorm.com™©
ClasseClass
Methode
Method
Propiétes
Properties
StructClass
Methode
Method
Propriétes
Properties
InterfacesClass
Methode
Method
Propiétes
Properties
![Page 130: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/130.jpg)
Le vocabulaire
L’espace de nomsNameSpace
Classe
Contient des classes
L’équivalent a une sorte de modèle qui contiens ‘des membres’(Méthodes & propriétés)
Powershell 2.0 alphorm.com™©
Class
Méthode
Method
Propriétés
Properties
& propriétés)
Une méthode retourne une valeur, ou le résultat d’une opération
C’est comme une variable : cela contient une valeur
![Page 131: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/131.jpg)
Les classes statiques
• Les classes statiques n’ont pas besoin d’être ‘instanciées’. (Et ne peuvent pas l’être).
• Méthodes retournant simplement un résultat.
• C’est la façon ‘facile’ d’utilser le .NET au sein de PowerShell
Powershell 2.0 alphorm.com™©
• C’est la façon ‘facile’ d’utilser le .NET au sein de PowerShell
• Démonstration
![Page 132: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/132.jpg)
Comment s’en servir ?
Classe
L’espace de nomsNameSpace
Methode
Instanciation
Powershell 2.0 alphorm.com™©
ClasseClass
Methode
Method
Propiétes
Properties
Method
Propiétes
Properties
![Page 133: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/133.jpg)
La jungle des classes
• Comment trouve-t-on les classes / Méthodes / propriétés.NET
� Notre moteur de recherché favoris + msdn (ou .NET)
Demonstration
Powershell 2.0 alphorm.com™©
� Demonstration
![Page 134: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/134.jpg)
Les assemblies• Une assembly c’est un fichier.DLL qui va contenir des classes, qui vont
pouvoir êre chargés dans .NET. Ceci dans un but de gain d’éspacedisque, et de réutilisabilité du code.
• PowerShell charge beaucoup de composants du framework .NET par défaut, mais certains éléments le sont pas. Il est cépendant possible de
Powershell 2.0 alphorm.com™©
défaut, mais certains éléments le sont pas. Il est cépendant possible de les chargés afin d’avoir accès à leur Classes, méthodes et propriétés via powershell.
![Page 135: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/135.jpg)
Ce qu’on a couvert• Introduction
• Pourquoi utiliser le .NET
• Le vocabulaire
� Le framework
Powershell 2.0 alphorm.com™©
� Les espaces de nom
� Les classes (static et non static)
• Méthodes & propriétés
• Comment instancier des objets
• Notre premier fonction basée sur DotNet
![Page 136: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/136.jpg)
Conclusion
Powershell 2.0 alphorm.com™©
Stéphane van GulickConsultant systèmes 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
![Page 137: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/137.jpg)
Conclusion• Regard en arrière
� Initiation, approfondissement, specialisation
• Prochaine étapes
� PowerShell 3.0
PowerShell lié aux composants MS (Active directory, Exchange, System
Powershell 2.0 alphorm.com™©
� PowerShell lié aux composants MS (Active directory, Exchange, System Center)
� Scripter le plus possible
![Page 138: alphorm.com - Formation Powershell 2.0](https://reader034.vdocuments.mx/reader034/viewer/2022050620/556c0171d8b42a6d768b5107/html5/thumbnails/138.jpg)
Merci !
Powershell 2.0 alphorm.com™©