Powershell, Windows
Powershell encryption decryption
Deux méthodes pour encrypter/decrypter une variable: $MyPswd = « password1234 » ConvertTo-SecureString $MyPswd -AsPlainText -Force | ConvertFrom-SecureString | Out-File -FilePath « $chemin\pass.txt » $password = Get-Content « $chemin\pass.txt » | ConvertTo-SecureString $bPswd = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password) $pswd = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bPswd) Write-Host $pswd rm « $chemin\pass.txt » Add-Type -assembly System.Security $passwordBytes = [System.Text.Encoding]::Unicode.GetBytes(« Open Sesame ») $entropy = [byte[]](1,2,3,4,5) $encrytpedData = [System.Security.Cryptography.ProtectedData]::Protect($passwordBytes, $entropy, ‘CurrentUser’) $encrytpedData | Set-Content -enc byte .\password.bin $encrytpedData = Get-Content -enc byte .\password.bin $unencrytpedData = [System.Security.Cryptography.ProtectedData]::Unprotect( $encrytpedData, $entropy, ‘CurrentUser’) $password = [System.Text.Encoding]::Unicode.GetString($unencrytpedData) $password function Set-Key { param([string]$string) $length = $string.length $pad = 32-$length if (($length -lt 16) -or ($length -gt 32)) {Throw « String must be between 16 and 32 characters »} $encoding = New-Object System.Text.ASCIIEncoding $bytes = $encoding.GetBytes($string + « 0 » * $pad) return $bytes } function Set-EncryptedData { param($key,[string]$plainText) $securestring = new-object System.Security.SecureString $chars = $plainText.toCharArray() foreach ($char in $chars) {$secureString.AppendChar($char)} $encryptedData = ConvertFrom-SecureString -SecureString $secureString -Key $key return $encryptedData } function Get-EncryptedData { param($key,$data) $data | ConvertTo-SecureString -key $key | ForEach-Object {[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($_))} } #Encrypt $plainText = « Some Super Secret Password » $key = Set-Key « AGoodKeyThatNoOneElseWillKnow » $encryptedTextThatIcouldSaveToFile = Set-EncryptedData -key $key -plainText $plaintext $encryptedTextThatIcouldSaveToFile #Decrypt $DecryptedText = Get-EncryptedData -data $encryptedTextThatIcouldSaveToFile -key $key $DecryptedText Voilà vous pouvez encrypter vos données sensibles…
Read MorePowershell, Windows
Powershell envoyer un email
Envoyer un email avec Powershell: $attachfile = New-Item -type file -force « $chemin\attachment.txt » Write-Output « attachment file example » | Out-File -Append $attachfile $to = « name@domain.com » $from = « name@domain.com » $smtp = « 192.168.1.1 » $subject = « Subject » $body = « Body » Send-MailMessage -to $to -From $from -Attachments « $chemin\attachment.txt » -SmtpServer $smtp -Subject $subject -Body $body rm « $chemin\attachment.txt » Assez simple finalement !
Read MorePowershell, Windows
Powershell comparaison de fichier
Une petie comparaison de fichier pour faire apparaître les différences et similitudes: $file1 = « $chemin\left.txt » $file2 = « $chemin\right.txt » Write-Output « equalite » | Out-File -Append $file1 Write-Output « equal » | Out-File -Append $file1 Write-Output « diff1 » | Out-File -Append $file1 Write-Output « equalite » | Out-File -Append $file2 Write-Output « equal » | Out-File -Append $file2 Write-Output « diff2 » | Out-File -Append $file2 Compare-Object $(Get-Content « left.txt ») $(Get-Content « right.txt ») -IncludeEqual | Where {$_.SideIndicator -eq ‘==’} | ForEach-Object {$_.InputObject} | Out-File « result_equal.txt » Compare-Object $(Get-Content « left.txt ») $(Get-Content « right.txt ») -IncludeEqual | Where {$_.SideIndicator -eq ‘=>’} | ForEach-Object {$_.InputObject} | Out-File « result_right.txt » Compare-Object $(Get-Content « left.txt ») $(Get-Content « right.txt ») -IncludeEqual | Where {$_.SideIndicator -eq ‘<=’} | ForEach-Object {$_.InputObject} | Out-File « result_left.txt » $compequal = gc « result_equal.txt » $compleft = gc « result_left.txt » $compright = gc « result_right.txt » $context = Get-Content $file1 | Select-String equalite -Context 2 Write-host $context Write-Host « Comparaison ==: » $compequal Write-Host « Comparaison:=> » $compleft Write-Host « Comparaison:<= » $compright rm « left.txt » rm « right.txt » rm « result_equal.txt » rm « result_right.txt » rm « result_left.txt » Très pratique !
Read MorePowershell, Windows
Powershell sorting de fichier
Petit aperçu du sorting de fichier: $createsortfile = New-Item -type file -force « $chemin\sort.txt » Write-Output « chiffre;lettre;date » | Out-File -Append $createsortfile Write-Output « 10;a;2012-04-05 » | Out-File -Append $createsortfile Write-Output « 11;z;2012-04-01 » | Out-File -Append $createsortfile Write-Output « 13;b;2012-04-02 » | Out-File -Append $createsortfile Import-csv « $chemin\sort.txt » -delimiter « ; » | sort-object date,lettre | export-csv « $chemin\sort.csv » $resultsort = gc « $chemin\sort.csv » Write-Host $resultsort rm « $chemin\sort.* » ça reste simple comme exemple, on est d’accord…
Read MoreAdministration système, Powershell, Windows
Encryption des mots de passe
Première méthode : $MyPswd = « password1234 » $chemin = « C:\ » ConvertTo-SecureString $MyPswd -AsPlainText -Force | ConvertFrom-SecureString | Out-File -FilePath « $chemin\pass.txt » $password = Get-Content « $chemin\pass.txt » | ConvertTo-SecureString $bPswd = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password) $pswd = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bPswd) Write-Host $pswd rm « $chemin\pass.txt » Deuxième méthode: # Stick password into DPAPI storage once – accessible only by current user Add-Type -assembly System.Security $passwordBytes = [System.Text.Encoding]::Unicode.GetBytes(« Open Sesame ») $entropy = [byte[]](1,2,3,4,5) $encrytpedData = [System.Security.Cryptography.ProtectedData]::Protect($passwordBytes, $entropy, ‘CurrentUser’) $encrytpedData | Set-Content -enc byte .\password.bin # Retrieve and decrypted password $encrytpedData = Get-Content -enc byte .\password.bin $unencrytpedData = [System.Security.Cryptography.ProtectedData]::Unprotect( $encrytpedData, $entropy, ‘CurrentUser’) $password = [System.Text.Encoding]::Unicode.GetString($unencrytpedData) $password Troisième méthode: function Set-Key { param([string]$string) $length = $string.length $pad = 32-$length if (($length -lt 16) -or ($length -gt 32)) {Throw « String must be between 16 and 32 characters »} $encoding = New-Object System.Text.ASCIIEncoding $bytes = $encoding.GetBytes($string + « 0 » * $pad) return $bytes } function Set-EncryptedData { param($key,[string]$plainText) $securestring = new-object System.Security.SecureString $chars = $plainText.toCharArray() foreach ($char in $chars) {$secureString.AppendChar($char)} $encryptedData = ConvertFrom-SecureString -SecureString $secureString -Key $key return $encryptedData } function Get-EncryptedData { param($key,$data) $data | ConvertTo-SecureString -key $key | ForEach-Object {[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($_))} } #Encrypt $plainText = « Some Super Secret Password » $key = Set-Key « AGoodKeyThatNoOneElseWillKnow » $encryptedTextThatIcouldSaveToFile = Set-EncryptedData -key $key -plainText $plaintext $encryptedTextThatIcouldSaveToFile #Decrypt $DecryptedText = Get-EncryptedData -data $encryptedTextThatIcouldSaveToFile -key…
Read MoreAdministration système, Powershell, Windows
Traiter des dates
Formatage de date: $datestring = Get-Date -uformat « Date: %d-%m-%Y Heure: %H:%M:%S Millis: %s » Write-Host $datestring Fonction pour faire des timestamp pendant le traitement (avec exemple d’utilisation) ######fonction temps function GetElapsedTime() { $runtime = $(get-date) – $script:StartTime $retStr = [string]::format(« {0} days, {1} hours, {2} minutes, {3}.{4} seconds », ` $runtime.Days, ` $runtime.Hours, ` $runtime.Minutes, ` $runtime.Seconds, ` $runtime.Milliseconds) $retStr } #on récupère le temps courant $script:startTime = get-date #on affiche le temps écoulé $(GetElapsedTime) Ajouter jours/mois à une date $currentdate = [datetime]::now $currentdate.AddDays(10) $currentdate.AddMonths(2)
Read More