PowerShell

Un article de WindowsLinux.net - Astuces pour Windows et Linux !.

Sommaire

Définition

Windows PowerShell, anciennement Microsoft Command Shell -MSH-, (nom de code Monad) est une interface en ligne de commande et un langage de script développé par Microsoft. Il est basé sur la programmation orientée objet et le framework Microsoft .NET.

Source : Wikipedia

PowerShell et Windows 7

Windows 7 est déjà installé avec PowerShell version 2.

Il ne faut pas croire que la version de PowerShell installée est la 1.0, malgré le dossier de travail qui est nommé "C:\windows\System32\WindowsPowerShell\v1.0", et les fichiers qui sont enregistrés en ".PS1"


Pour être certain, taper :

get-host

La version affichée, est "Version : 2.0"

WinRM

Le package Windows Management Framework Core comprend Windows PowerShell 2.0 et Windows Remote Management (WinRM) 2.0.

A installer absolument : http://support.microsoft.com/kb/968929


Des informations sur le PowerShell SDK sont disponibles ici : http://msdn.microsoft.com/en-us/library/ms714469(VS.85).aspx

Exécution des scripts

Pour exécuter des scripts (extension .ps1 par exemple), s'il ne sont pas signés, il faut taper :

Set-ExecutionPolicy remotesigned

Ainsi votre PowerShell va exécuter tous les scripts locaux que vous écrivez, et demander des scripts signés pour tout les scripts provenant d'internet.

Exemples de scripts

Avoir une liste des ordinateurs de l'Active Directory faisant tourner un certain service :

$strCategory = "computer"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("(objectCategory=$strCategory)")
$colProplist = "name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{
$temp=[System.ServiceProcess.ServiceController]::GetServices($objComputer.name)
where{$_.name -eq 'Dhcp'}
If($temp.status -eq "Running")
{
Write-host $objComputer.name
}
}


Se connecter automatiquement, via un formulaire, à une page web :

#Lancer IE
Write-Host -ForegroundColor Green "Tweeted; $tweet.";
 
$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
$ie.navigate('https://Mon/url/authentification')
 
#Authentication sur la page web
while($ie.busy) {start-sleep 1}
if ($ie.document.getElementByID("logonForm") -ne $null)
    {
    $ie.document.getElementByID("username").value = "MonLogin"
    $ie.document.getElementByID("password").value = "MotDePasse"
 
    #Click on the button for logon
	$ie.Document.getElementByID("SubmitCreds").Click();
    while($ie.busy) {start-sleep 1}
    }
 
$ie.navigate('https://mon/url/authentifiee.html')
while($ie.busy) {start-sleep 1}

Liens

Externes

Site de Microsoft : http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx

PowerShell Scripts : http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Autres références : http://www.powershell-scripting.com/

PowerShell V2 pour Windows XP (KB968929) : http://support.microsoft.com/kb/968929

Windows PowerShell Scriptomatic : Disponible ici

Utilitaires

PrimalForms Community Edition : http://www.primaltools.com/downloads/communitytools/signup.asp?tool=pforms

PowerGUI : http://www.powergui.org/downloads.jspa