Powershell: Cacher l’exécution d’un script powershell

Ludovic QUENEC'HDU
Ludovic QUENEC'HDU Ajouter un commentaire 4e lecture en min
Si vous voulez cacher l’exécution d’un script powershell ou même d’un programme, voici une méthode simple. Pour cela on va passer par un script vbs. En effet l’interpréteur wscript, n’affiche pas de fenêtre d’exécution par défaut, contrairement à l’interpréteur cscript ou powershell. L’intérêt c’est que l’utilisateur ne voit pas l’exécution d’un script ou processus quel qu’il soit. Cela peut être utile pour cache un script de démarrage, une tache planifiée, une publication CITRIX ou RDS. Pour nos tests on va utiliser un script en powershell pour afficher une fenêtre toute simple :
add-type -assemblyName "Microsoft.VisualBasic"
[Microsoft.VisualBasic.Interaction]::MsgBox("info")
Voici ce qu’on veut cacher l’interpréteur powershell avec son fond bleu disgracieux : hide-powershell1-4 Ensuite nous allons utiliser la méthode Run :
object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
C’est le paramètre intWindowStyle qui va nous intéresser ici, voici les différents paramètres possibles : C’est le paramètre intWindowStyle qui va nous intéresser ici, voici les différents paramètres possibles :
intWindowStyle Description
0 Hides the window and activates another window.
1 Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
2 Activates the window and displays it as a minimized window.
3 Activates the window and displays it as a maximized window.
4 Displays a window in its most recent size and position. The active window remains active.
5 Activates the window and displays it in its current size and position.
6 Minimizes the specified window and activates the next top-level window in the Z order.
7 Displays the window as a minimized window. The active window remains active.
8 Displays the window in its current state. The active window remains active.
9 Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
10 Sets the show-state based on the state of the program that started the application.
Extrait de la documentation msdn https://msdn.microsoft.com/en-us/library/d5fk67ky(v=vs.84).aspx Le paramètre 0 c’est celui qu’on utilisera pour cacher notre fenêtre d’exécution. Voici le script en Vbs:
Dim objShell
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.run "[process]" , 0
Set objShell = Nothing
à la ligne :
objShell.run "[process]" , 0
il suffit juste de remplacer [ process] par le processus à cacher:
Dim objShell
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.run "powershell.exe e:\script\info.ps1" , 0
Set objShell = Nothing
  Donc si on utilise l’interpréteur cscript voici le résultat : hide-powershell2-4 Bon j’ai un peu triché car normalement le cscript s’affiche rapidement et on n’a pas forcément le temps de le voir. Maintenant nous allons passer le wscript est là c’est magique :
wscript E:\script\hide.vbs
hide-powershell3-4 Et cela fonctionne tout type de processus exemple ici avec wordpad : hide-powershell4-4    
Partager cet article
Par Ludovic QUENEC'HDU Expert en Virtualisation et Sécurité Informatique au Service de Grands Comptes et du Logiciel Libre
Ludovic QUENECHDU est un véritable virtuose de la virtualisation et de la sécurité informatique. Depuis son indépendance professionnelle en 2000, il a apporté son expertise à de prestigieux clients tels que Cameroun Telecom, Merryl Lynch, Ernst & Young, AXA, et bien d'autres. En parallèle, Ludovic s'engage régulièrement dans le démarrage de petites entreprises, notamment dans le domaine du logiciel libre, une passion qu'il chérit pour ses valeurs de partage et d'échange. Son parcours professionnel et sa passion pour la technologie en font un acteur incontournable de l'industrie informatique.
Laisser un commentaire