Richard Bruno
navigation
Home
admin









PowerShell
October 18th, 2016

Sommaire


Manip sur les fichiers
Gestion d'un AD
Commandes diverses


Manip sur les fichiers



Supprimer les CRLF d'un fichier Windows pour qu'il soit lu correctement sous linux (i.e. pas de ^M à la fin de chaque ligne)


$text = [IO.File]::ReadAllText($fout) -replace "`r`n", "`n"
[IO.File]::WriteAllText($fout, $text)



Source : http://stackoverflow.com/questions/19127741/replace-crlf-using-powershell

Gestion d'un AD



Chargement du module

ipmo activedirectory



ipmo : import module

Exemple de recherche d'utilisateurs

Get-ADUser -Filter { samaccountname -eq "u0001"} -Properties HomeDirectory



Get-ADUser -Filter * -Properties HomeDirectory -SearchBase "OU=[...]"



Get-ADUser -Identity "saraDavis"



Get-ADUser -Identity "CN=SaraDavis,CN=Europe,CN=Users,DC=corp,DC=contoso,DC=com"





Source : http://technet.microsoft.com/en-us/library/ee617241.aspx

Modification d'un compte

Method 1: Modify the Manager property for the "saraDavis" user by using the Identity and Manager parameters.

Set-ADUser -Identity "saraDavis" -Manager "JimCorbin"



Method 2: Modify the Manager property for the "saraDavis" user by passing the "saraDavis" user through the pipeline and specifying the Manager parameter.

Get-ADUser -Identity "saraDavis" | Set-ADUser -Manager "JimCorbin"



Method 3: Modify the Manager property for the "saraDavis" user by using the Windows PowerShell command line to modify a local instance of the "saraDavis" user. Then set the Instance parameter to the local instance.

$user = Get-ADUser -Identity "saraDavis"
$user.Manager = "JimCorbin"
Set-ADUser -Instance $user.



Source : http://technet.microsoft.com/en-us/library/ee617215.aspx

Exemple de script

# permet l'utilisation des commandes du type GET-ADUser
ipmo activedirectory

# Recherche dans l'OU souhaité tous les comptes utilisateurs
#$ldapquery = [ADSI] 'LDAP://OU=[...],DC=ecole'

$objrechercher = New-object system.directoryservices.directorysearcher($ldapQuery)
$objrechercher.filter ='(&(objectClass=person)(objectCategory=person">'
$boucle = $objrechercher.findall()

# Pour chaque utilisateur
foreach ($user in $boucle)
{
write $user.properties.name

$filter = [System.String] $user.properties.samaccountname
$usermod = Get-ADUser -Identity $filter -Properties *

# Les modications se font ici !
$usermod.HomeDirectory="\\staff\"+$filter

# instanciation
Set-ADUser -Instance $usermod

}


Commandes diverses



Lister les fichiers/répertoires cachés

Get-childItem -force



Tail

Get-Content : Gets the content of the item at the specified location.

Get-Content -Path ... -Wait



Source : http://stackoverflow.com/questions/4426442/unix-tail-equivalent-command-in-windows-powershell

Lister les commandes disponibles

Get-Command



Lister les commandes disponibles et faire une sélection parmi ces commandes

Get-Command | where-Object {$_.Name -like 'Add*'}



Note 1 : The $_ symbol represents the current object in the pipeline.
Note 2 : Instead of spelling out Where-Object, you can simply use thewhere alias

Autre syntaxe :

Get-Command | where Name -like 'Add*'




Pagination

| more



Les opérateurs de comparaison

PowerShell supports comparison operators such as:

-ne (not equal to)
-lt (less than)
-le (less than or equal to)
-gt (greater than)
-ge (greater than or equal to)
-like (like—a wildcard comparison)
-notlike (not like—a wildcard comparison)
-contains (contains the specified value)
-notcontains (doesn't contain the specified value)

Source : http://windowsitpro.com/powershell/powershell-basics-filtering-objects

Les opérateurs de logiques

PowerShell supports several logical operators:

-and (The script block evaluates to True if the expressions on both sides of the logical operator evaluate to True.)
-or (The script block evaluates to True when one of the expressions on either side of the logical operator evaluates to True.)
-xor (The script block evaluates to True when one of the expressions on either side of the logical operator evaluates to True and the other expression evaluates to False.)
-not or ! (Negates, or reverses, the script element that follows it.)

Exemple :

Get-Command | Where-Object {($_.Name -like '*clear*') `
-and ($_.CommandType -eq 'cmdlet')}



Récupérer uniquement les fichiers

| Where-Object {-not $_.psiscontainer}



je filtre les dossiers pour n'avoir que les objet de type System.IO.FileInfo

Source : http://powershell-scripting.com/index.php?option=com_joomlaboard&Itemid=76&func=view&id=16772&catid=5&limit=25&limitstart=0




Dernières modifs
Divers (October 18th, 2016)
Exchange 2013 (October 18th, 2016)
Outlook 2010 (October 18th, 2016)
PowerShell (October 18th, 2016)
Windows 2003 Serveur (October 18th, 2016)
Windows 2008 R2 (October 18th, 2016)
Windows 2012 serveur (October 18th, 2016)

Contact
Pour m'envoyer un mail,
Pour me laisser un commentaire :
richard.brunooo
chez
gmail.com


powered by kure, modified by Bruno