Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
kb_it:powershell_dev [2025/02/03 11:10] befekb_it:powershell_dev [2025/02/20 09:23] (Version actuelle) befe
Ligne 1: Ligne 1:
 +====== PowerShell ======
 +
 +===== Envoyer un email =====
 +
 +<code pwsh>
 +$SMTP_HOST = "mail.domain.ltd"
 +$SMTP_PORT = 25
 +$SMTP_SSL = $False
 +$SMTP_USER = "username"
 +$SMTP_PWD = "*****"
 +$MAIL_ATTACHMENT = ""
 +
 +# Paramètres système
 +$ErrorActionPreference = "Stop"
 +[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
 +
 +Function Main {
 +  param(  
 +    [string] $From = "from@example.com",
 +    [string] $To = "to@example.com",
 +    [string] $Subject = "SMTP Client Submission - $(Get-Date -Format g)",
 +    [string] $Body = "This is a test email using SMTP Client Submission"
 +  )
 +  Send-Mail -From $From -To $To -Subject $Subject -Body $Body
 +}
 +
 +Function Send-Mail {
 +  param(  
 +    [string] $From = "from@example.com",
 +    [string] $To = "to@example.com",
 +    [string] $Subject = "SMTP Client Submission - $(Get-Date -Format g)",
 +    [string] $Body = "This is a test email using SMTP Client Submission"
 +  )
 +
 +  $mailParams = @{
 +    SmtpServer                 = $SMTP_HOST
 +    Port                       = $SMTP_PORT
 +    From                       = $From
 +    To                         = $To
 +    Subject                    = $Subject
 +    Body                       = $Body
 +    UseSSL                     = $SMTP_SSL
 +    DeliveryNotificationOption = "OnFailure", "OnSuccess"
 +  }
 +  
 +  if ((Get-Variable SMTP_USER -Scope Script -ErrorAction "Ignore") -and (Get-Variable SMTP_PWD -Scope Script -ErrorAction "Ignore")) {
 +    $mailParams.Credential = New-Object System.Management.Automation.PSCredential($SMTP_USER, $(ConvertTo-SecureString $SMTP_PWD -AsPlainText -Force))
 +  }
 +  if ($MAIL_ATTACHMENT -ne "") {
 + $mailParams.Attachment = $MAIL_ATTACHMENT
 +  }
 +
 +  Send-MailMessage @mailParams
 +}
 +
 +# Traitement
 +Main @args
 +</code>
 +
 +<code pwsh>
 +mail.ps1 -From "sender@domain.tld" -To "receiver@domain.tld" -Subject "test" -Body "test"
 +</code>
 +
 +===== Active Directory =====
 +
 +==== Supprimer un object protégé contre la suppression accidentelle ====
 +
 +<code pwsh>
 +Get-ADObject -Server localhost:389 -Identity 'OU=xxx,DC=yyy,DC=zzz' |
 +  Set-ADObject -ProtectedFromAccidentalDeletion:$false -PassThru |
 +  Remove-ADOrganizationalUnit -Confirm:$false
 +</code>