PowerShell

Envoyer un email

$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
mail.ps1 -From "sender@domain.tld" -To "receiver@domain.tld" -Subject "test" -Body "test"

Active Directory

Supprimer un object protégé contre la suppression accidentelle

Get-ADObject -Server localhost:389 -Identity 'OU=xxx,DC=yyy,DC=zzz' |
  Set-ADObject -ProtectedFromAccidentalDeletion:$false -PassThru |
  Remove-ADOrganizationalUnit -Confirm:$false