« PowerShell » : différence entre les versions

Aller à la navigation Aller à la recherche
 
(13 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
{{Sommaire}}
'''PowerShell''' est un langage de programmation pour les systèmes Microsoft Windows.
'''PowerShell''' est un langage de programmation pour les systèmes Microsoft Windows.


== Référence ==
== Référence ==
=== IT-Connect ===
* http://www.it-connect.fr/powershell-pour-les-debutants-1ere-partie/
* http://www.it-connect.fr/powershell-pour-les-debutants-1ere-partie/
* http://www.it-connect.fr/powershell-et-active-directory-modifier-le-domaine-des-adresses-mails/
* http://www.it-connect.fr/powershell-et-active-directory-modifier-le-domaine-des-adresses-mails/
Ligne 7 : Ligne 11 :
* http://www.it-connect.fr/creer-des-comptes-utilisateurs-par-lot-avec-powershell-v2-et-le-module-active-directory-server-2008-r2/
* http://www.it-connect.fr/creer-des-comptes-utilisateurs-par-lot-avec-powershell-v2-et-le-module-active-directory-server-2008-r2/
* http://www.it-connect.fr/chapitres/modifier-la-resolution-de-lecran/
* http://www.it-connect.fr/chapitres/modifier-la-resolution-de-lecran/
----
 
Convention de nommage (charte de nommage) pour ses serveurs :
* https://www.it-connect.fr/quelle-convention-de-nommage-utiliser-pour-ses-serveurs/
 
=== Marie Pascal Delamare ===
Site de Marie Pascal Delamare, BTS SIO / STS SIO
Site de Marie Pascal Delamare, BTS SIO / STS SIO
* http://mariepascal.delamare.free.fr/
* http://mariepascal.delamare.free.fr/
=== Youtube ===
John Savill's Technical Training, chaîne Youtube
* https://www.youtube.com/@NTFAQGuy
* série de vidéos sur PowerShell :
** https://www.youtube.com/watch?v=sQm4zRvvX58&list=PLlVtbbG169nFq_hR7FcMYg32xsSAObuq8
TechSnips by ATA Learning, chaine Youtube, sélection "Doing anything with PowerShell", 334 vidéos au 29/05/2023 :
* https://www.youtube.com/playlist?list=PLviQuRV5ySnzt8YpB14SfHeJm8Rh0oO1A
TechThoughts, chaine Youtube, 41 vidéos au 11/06/2023 :
* https://www.youtube.com/@Techthoughts2/videos
* série de vidéos sur PowerShell :
** Learn PowerShell, https://www.youtube.com/watch?v=csuz7qSJv_Q&list=PL2j0_s2VJe2hzQuQyn6yfMS2olhhs4UnQ
CyberAutomate :
* https://www.youtube.com/@CyberAutomate/videos


== Document ==
== Document ==
Ligne 44 : Ligne 69 :
* <source inline>Set-Service -Name wuauserv -StartupType Disabled</source>
* <source inline>Set-Service -Name wuauserv -StartupType Disabled</source>


== Script de mise à jour d'Active Directory ==
== Impression ==  
 
Supprimer toutes les fichiers des files d'attente d'impression :
=== Lire les lignes d'un fichier ===
Avec le fichier suivant <code>C:\tmp\source.csv</code> :
<pre>
compte;civilité;nom;prénom;société;service
dupondp;M.;Dupond;Pierre;MaSociété;SI
martinj;M.;Martin;Jean;MaSociété;Commercial
defrattec;Mme;De Fratte;Catherine;MaSociété;Export
</pre>
 
Premier script :
<source>
<source>
$fichier = Import-Csv "C:\tmp\source.csv" -Delimiter ";"
Stop-Service Spooler
Foreach ($ligne in $fichier) {
Remove-Item "C:\Windows\System32\spool\PRINTERS\*.*" -Force
    Write-Host $ligne
Start-Service Spooler
    }
</source>
Le résultat du premier script est :
<pre>
@{compte=dupondp; nom=Dupond; prénom=Pierre; société=MaSociété; service=SI}
@{compte=martinj; nom=Martin; prénom=Jean; société=MaSociété; service=Commercial}
@{compte=defrattec; nom=De Fratte; prénom=Catherine; société=MaSociété; service=Export}
</pre>
 
Deuxième script :
<source>
Import-Csv "C:\tmp\source.csv" -Delimiter ";" | Foreach { Write-Host ("{0} {1} appartient au service {2}" -f $_.civilité, $_.nom, $_.service) }
</source>
</source>
Le résultat du deuxième script est :
<pre>
M. Dupond appartient au service SI
M. Martin appartient au service Commercial
Mme De Fratte appartient au service Export
</pre>
== Liste des droits sur le contenu d'une arborescence ==
<source>
# Liste des droits de sécurité sur les dossiers et fichiers d'une arborescence
# Création d'un fichier


$FichierDestination = "C:\Support\PermissionListeTest.csv"
== Autre ==
$CheminRacine = "C:\Support\"


# Entête de document
=== Logiciel menu ===
$FichierEntete = "Chemin;IdentityReference;AccessControlType;IsInherited;InheritanceFlags;PropagationFlags;FilesystemRights"  
<source>#################### - VARIABLES - ####################
$logiciel = @(
  [pscustomobject]@{nom="7Zip";           path="C:\Support\7zip\7z2201-x64.exe"}
  [pscustomobject]@{nom="VLC";           path="C:\Support\VLC\vlc-3.0.17.4-win64.exe"}
  [pscustomobject]@{nom="Adobe";         path="C:\Support\AdobeXI\AcroRdrDC2101120039_fr_FR.exe"}
  [pscustomobject]@{nom="BgInfo";         path="C:\Support\BgInfo\setup-BGInfo4.27.exe"}
  [pscustomobject]@{nom="Bureau distant"; path="C:\Support\Bureau distant\setup-bureau distant-v202211.exe"}
  )


$FichierExiste = Test-Path $FichierDestination 
Clear-Host
If ($FichierExiste -eq $True) {Remove-Item $FichierDestination} 


Add-Content -Encoding UTF8 -Value $FichierEntete -Path $FichierDestination
#################### - SCRIPT - #################### 
$continue = $true
While ($continue) {
  Write-Host "-------------------- Logiciel --------------------" -ForegroundColor Cyan
  Write-Host "> 1 --->" $logiciel[0].nom -ForegroundColor Cyan
  Write-Host "> 2 --->" $logiciel[1].nom -ForegroundColor Cyan
  Write-Host "> 3 --->" $logiciel[2].nom -ForegroundColor Cyan
  Write-Host "> 4 --->" $logiciel[3].nom -ForegroundColor Cyan
  Write-Host "> 5 --->" $logiciel[4].nom -ForegroundColor Cyan
  Write-Host "> Tout ---> Installation de tous les logiciels de la liste" -ForegroundColor Cyan
  Write-Host "--------------------------------------------------" -ForegroundColor Cyan
  Write-Host "Sélectionner une option ou un type" -NoNewline
  Write-Host " [fin] " -ForegroundColor Yellow -NoNewline
  Write-Host "pour sortir : " -NoNewline
  $choix = Read-Host
    Switch ($choix) {
        "1" {
            try {
                Write-Host "Installation de"$logiciel[0].nom"sur le poste"
                Start-Process -FilePath $logiciel[0].path -ArgumentList "/S" -NoNewWindow -Wait
                }


$DossierListe = dir $CheminRacine -recurse | Where {$_.psiscontainer -eq $True}
            catch {
                Clear-Host
                Write-Warning "L'installation n'a pas pu se faire !"
                }
            }


Foreach ($Dossier in $DossierListe) {  
      "2" {
  $DroitListe = Get-ACL $Dossier.fullname | ForEach-Object { $_.Access }
          Write-Host "Installation de"$logiciel[1].nom"sur le poste"
  Foreach ($Droit in $DroitListe) {
          Start-Process -FilePath $logiciel[1].path -ArgumentList "/S" -NoNewWindow -Wait
    $FichierLigne = ($Dossier.Fullname,
          }
                    $Droit.IdentityReference,
                    $Droit.AccessControlType,
                    $Droit.IsInherited,
                    $Droit.InheritanceFlags,
                    $Droit.PropagationFlags,
                    ($Droit.FileSystemRights -replace ',', '/' )
                    ) -join ";"
    Add-Content -Encoding UTF8 -Value $FichierLigne -Path $FichierDestination
    }
}
</source>


=== Autre ===
      "3" {
Pour la mise à jour de compte utilisateur, attribut titre et numéro de mobile, d'après le nom du compte et une fichier :
          Write-Host "Installation de"$logiciel[2].nom"sur le poste"
* <source inline>Import-Csv "D:\script\util-maj-tel.csv" | Foreach { Set-ADUser -Identity $_.SamAccountName –Title $_.Title -MobilePhone $_.MobilePhone }</source>
          Start-Process -FilePath $logiciel[2].path -ArgumentList "/S" -NoNewWindow -Wait
          }


Copier les groupes de sécurité d'un compte vers un autre !
      "4" {
* <source inline>Get-ADUser -Identity "comptesource" -Properties memberof | Select-Object -ExpandProperty memberof | Add-ADGroupMember -Members "comptecible"</source>
          Write-Host "Installation de"$logiciel[3].nom"sur le poste"
          Start-Process -FilePath $logiciel[3].path -ArgumentList "/S" -NoNewWindow -Wait
          }


=== Ressource ===
      "5" {
* https://www.it-connect.fr/
          Write-Host "Installation de"$logiciel[4].nom"sur le poste"
* https://www.tutos.eu/
          Start-Process -FilePath $logiciel[4].path -ArgumentList "/S" -NoNewWindow -Wait
          }


Installer le module Active Directory sur Windows 10 et Windows Server
      "all" {
* https://www.it-connect.fr/chapitres/installer-le-module-active-directory-sur-windows-10-et-windows-server/
          Write-Host "Installation de"$logiciel[0,1,2,3,4].nom"sur le poste"
          Start-Process -FilePath $logiciel[0,1,2,3,4].path -ArgumentList "/S" -NoNewWindow -Wait
          }


Gérer les utilisateurs Active Directory avec PowerShell
      "fin" {
* https://www.it-connect.fr/modules/gerer-les-utilisateurs-active-directory-avec-powershell/
          $continue = $false
          }


Créer un utilisateur dans l’Active Directory avec PowerShell
    default {
* https://www.it-connect.fr/chapitres/creer-un-utilisateur-dans-lactive-directory-avec-powershell/
        Clear-Host
 
        Write-Warning "Choix invalide"
Gérer les utilisateurs et ordinateurs inactifs dans l’Active Directory
        }
* https://www.it-connect.fr/gerer-les-utilisateurs-et-ordinateurs-inactifs-dans-lactive-directory/
  }
}
</source>


== Impression ==  
== Voir aussi ==
Supprimer toutes les fichiers des files d'attente d'impression :
* &nbsp;
<source>
Stop-Service Spooler
Remove-Item "C:\Windows\System32\spool\PRINTERS\*.*" -Force
Start-Service Spooler
</source>

Dernière version du 16 novembre 2023 à 00:09

PowerShell est un langage de programmation pour les systèmes Microsoft Windows.

Référence

IT-Connect

Convention de nommage (charte de nommage) pour ses serveurs :

Marie Pascal Delamare

Site de Marie Pascal Delamare, BTS SIO / STS SIO

Youtube

John Savill's Technical Training, chaîne Youtube

TechSnips by ATA Learning, chaine Youtube, sélection "Doing anything with PowerShell", 334 vidéos au 29/05/2023 :

TechThoughts, chaine Youtube, 41 vidéos au 11/06/2023 :

CyberAutomate :

Document

Gestion des path sous PowerShell, PDF, 31 pages :

Source de script

Gérer les autorisations NTFS avec PowerShell :

Copie de fichiers avec BITS (Background Intelligent Transfer Service) :

PowerShell : convertir PS1 en EXE ; création d'un exécutable :

Interface graphique

Windows10Debloater

Script si problème mise à jour Windows

Le service "Windows Update" s'appelle wuauserv :

  • Stop-Service -Name wuauserv
  • Start-Service -Name wuauserv
  • Set-Service -Name wuauserv -StartupType Automatic
  • Set-Service -Name wuauserv -StartupType Manual
  • Set-Service -Name wuauserv -StartupType Disabled

Impression

Supprimer toutes les fichiers des files d'attente d'impression :

Stop-Service Spooler 
Remove-Item "C:\Windows\System32\spool\PRINTERS\*.*" -Force 
Start-Service Spooler

Autre

Logiciel menu

#################### - VARIABLES - ####################
$logiciel = @(
   [pscustomobject]@{nom="7Zip";           path="C:\Support\7zip\7z2201-x64.exe"}
   [pscustomobject]@{nom="VLC";            path="C:\Support\VLC\vlc-3.0.17.4-win64.exe"}
   [pscustomobject]@{nom="Adobe";          path="C:\Support\AdobeXI\AcroRdrDC2101120039_fr_FR.exe"}
   [pscustomobject]@{nom="BgInfo";         path="C:\Support\BgInfo\setup-BGInfo4.27.exe"}
   [pscustomobject]@{nom="Bureau distant"; path="C:\Support\Bureau distant\setup-bureau distant-v202211.exe"}
   )

Clear-Host

#################### - SCRIPT - ####################  
$continue = $true
While ($continue) {
   Write-Host "-------------------- Logiciel --------------------" -ForegroundColor Cyan
   Write-Host "> 1 --->" $logiciel[0].nom -ForegroundColor Cyan
   Write-Host "> 2 --->" $logiciel[1].nom -ForegroundColor Cyan
   Write-Host "> 3 --->" $logiciel[2].nom -ForegroundColor Cyan
   Write-Host "> 4 --->" $logiciel[3].nom -ForegroundColor Cyan
   Write-Host "> 5 --->" $logiciel[4].nom -ForegroundColor Cyan
   Write-Host "> Tout ---> Installation de tous les logiciels de la liste" -ForegroundColor Cyan
   Write-Host "--------------------------------------------------" -ForegroundColor Cyan
   Write-Host "Sélectionner une option ou un type" -NoNewline
   Write-Host " [fin] " -ForegroundColor Yellow -NoNewline
   Write-Host "pour sortir : " -NoNewline
   $choix = Read-Host
    Switch ($choix) {
        "1" {
            try {
                Write-Host "Installation de"$logiciel[0].nom"sur le poste"
                Start-Process -FilePath $logiciel[0].path -ArgumentList "/S" -NoNewWindow -Wait
                }

            catch {
                Clear-Host
                Write-Warning "L'installation n'a pas pu se faire !"
                }
            }

       "2" {
          Write-Host "Installation de"$logiciel[1].nom"sur le poste"
          Start-Process -FilePath $logiciel[1].path -ArgumentList "/S" -NoNewWindow -Wait
          }

       "3" {
          Write-Host "Installation de"$logiciel[2].nom"sur le poste"
          Start-Process -FilePath $logiciel[2].path -ArgumentList "/S" -NoNewWindow -Wait
          }

       "4" {
          Write-Host "Installation de"$logiciel[3].nom"sur le poste"
          Start-Process -FilePath $logiciel[3].path -ArgumentList "/S" -NoNewWindow -Wait
          }

       "5" {
          Write-Host "Installation de"$logiciel[4].nom"sur le poste"
          Start-Process -FilePath $logiciel[4].path -ArgumentList "/S" -NoNewWindow -Wait
          }

       "all" {
          Write-Host "Installation de"$logiciel[0,1,2,3,4].nom"sur le poste"
          Start-Process -FilePath $logiciel[0,1,2,3,4].path -ArgumentList "/S" -NoNewWindow -Wait
          }

       "fin" {
          $continue = $false
          }

    default { 
        Clear-Host 
        Write-Warning "Choix invalide"
        }
  }
}

Voir aussi

  •