Routines.psm1
function Read-Choice { <# .SYNOPSIS Display a prompt like PowerShell does. .DESCRIPTION Make a Powershell style prompt. You must provide the same number of options and help messages. The function returns an integer, the selected options. .EXAMPLE PS C:\> Read-Choice -Title 'Do you want to continue ?' -Prompt 'Answer yes or no' Do you want to continue ? Answer yes or no [Y] Yes [N] No [?] Help (default is "Y"): .EXAMPLE PS C:\> Read-Choice -Title 'Do you want to continue ?' -Prompt 'Answer yes or no' -Options '&Yes','&No','Yes for &all','N&o for all' -HelpMessage 'Say yes','Say No','Say allways yes','Say allways no' Do you want to continue ? Answer yes or no [Y] Yes [N] No [?] Help (default is "Y"): .LINK https://dawan.fr #> [CmdletBinding()]param( [parameter(Mandatory)] [string]$Title, [parameter(Mandatory)] [string]$Prompt, [string[]]$Options=@('&Yes','&No'), [string[]]$HelpMessages=@('','') ) if ($Options.Count -ne $HelpMessages.Count) { Throw 'Please provide as much Options as HelpMessages' } $tabOptions = @() $i=0 foreach ($opt in $Options) { $tabOptions += New-Object System.Management.Automation.Host.ChoiceDescription $opt, $HelpMessages[$i] $i++ } $Choices = [System.Management.Automation.Host.ChoiceDescription[]] ($tabOptions) return $host.ui.PromptForChoice($Title,$Prompt,$Choices,0) } function Read-PopUp { <# .SYNOPSIS Display a popup. .DESCRIPTION Provides a wrapper for MessageBox. Returns the value of the selected button. .EXAMPLE PS C:\> Read-PopUp -Title Information -Message 'information to display' Ok .EXAMPLE PS C:\> Read-PopUp -Title Question -Message 'Continue ?' -Buttons YesNo No .EXAMPLE PS C:\> Read-PopUp -Title Question -Message "What to do?" -Buttons AbortRetryIgnore -Icon Error -DefaultButton Button3 Retry .LINK https://dawan.fr #> [CmdletBinding()]param( [parameter(Mandatory)] [string]$Title, [parameter(Mandatory)] [string]$Message, [ValidateSet('Ok', 'AbortRetryIgnore','OkCancel','RetryCancel','YesNo','YesNoCancel')] [string]$Buttons='Ok', [ValidateSet('Asterisk','Error','Exclamation','Hand','Information','None','Question','Stop','Warning')] [string]$Icon='None', [ValidateSet('Button1','Button2','Button3')] [string]$DefaultButton='Button1' ) Add-Type -AssemblyName "System.Windows.Forms" $dlg = [System.Windows.Forms.MessageBox] $btn = [System.Windows.Forms.MessageBoxButtons]::$Buttons $icn = [System.Windows.Forms.MessageBoxIcon]::$Icon $dfb = [System.Windows.Forms.MessageBoxDefaultButton]::$DefaultButton return $dlg::Show($Message, $Title, $btn, $icn, $dfb) } function Remove-Diacritics { <# .SYNOPSIS Function Designed for remove special characters and provides a string usable for login. .LINK https://dawan.fr #> [CmdletBinding()]param( [parameter(Mandatory)] [string]$String ) return [Text.Encoding]::ASCII.GetString([Text.Encoding]::GetEncoding("Cyrillic").GetBytes($String)) } function Clear-String { <# .SYNOPSIS Function designed for remove special characters and provides a string usable for login. .LINK https://dawan.fr #> param( [parameter(Mandatory)] [string]$String, [string[]]$DeletedChars = @('-',' ',"'"), [string[]]$ReplacedChars = @('œ','æ'), [string[]]$ReplacedByChars = @('oe','ae'), [switch]$LowerCase, [ValidateRange(1,100)] [byte]$MaxLength = 20 ) foreach ($char in $DeletedChars) { $String = $String -replace $char,'' # opérateur de remplacement } $i = 0 foreach ($char in $ReplacedChars) { $String = $String -replace $char,$ReplacedByChars[$i] $i++ } if ($String.length -gt $MaxLength) { $String = $String.SubString(0, $MaxLength) } if ($LowerCase) { $String = $String.ToLower() } return $String } function Get-Password { <# .SYNOPSIS Function returning a password according to complexity required in AD domain. .DESCRIPTION You can change the numbers of letters, digits and non alpha-numeric characters required. You can modify the sets of allowed characters. .LINK https://dawan.fr #> param( [byte]$NumLetters = 5, [byte]$NumDigits = 2, [byte]$NumNonAlphaNum = 1, [switch]$AsSecureString, [string]$LettersAllowed = 'azertyuiopmlkjhgfdsqwxcvbn', [string]$DigitsAllowed = '0123456789', [string]$NonAlphNumAllowed = ':;,!*$=-' ) $LettersTab = $LettersAllowed -split '' $DigitsTab = $DigitsAllowed -split '' $NonAlphNumTab = $NonAlphNumAllowed -split '' # Tirer au sort dans un tableau : Get-Random $CharTab = $LettersTab | Get-Random -Count $NumLetters -ea Ignore # ea : ErrorAction $CharTab += $DigitsTab | Get-Random -Count $NumDigits -ea Ignore $CharTab += $NonAlphNumTab | Get-Random -Count $NumNonAlphaNum -ea Ignore # Mélanger le tableau : Sort-Objet avec Get-Random $CharTab = $CharTab | Sort-Object {Get-Random} $Password = $CharTab -Join '' # opérateur de rassemblement d'élément de tab if ($AsSecureString) { return (ConvertTo-SecureString $Password -AsPlainText -Force) } else { return $Password } } |