122 lines
7.4 KiB
PowerShell
122 lines
7.4 KiB
PowerShell
Function New-ComplexPassword-withLogging {
|
||
|
||
<#
|
||
.SYNOPSIS
|
||
Function will generate a new complex Password.
|
||
|
||
.DESCRIPTION
|
||
This Function will generate a complex Password with a minimum length of 4 an a maximum length of 64 digits/characters.
|
||
The generated Password will include digits, characters, characters witten in capital letters and special characters.
|
||
After generation the order of the Password will be randomly mixed.
|
||
|
||
.REQUIREMENT General
|
||
PowerShell V3
|
||
|
||
.REQUIREMENT Assembly
|
||
<NONE>
|
||
|
||
.REQUIREMENT Variables
|
||
PasswordCreationLenght, Counter1, UserPasswordPlainText, UserPasswordPlainTextTEMPCharArray, UserPasswordPlainTextTEMPScrambled
|
||
|
||
.REQUIREMENT Variables preSet
|
||
<NONE>
|
||
|
||
.REQUIREMENT Functions
|
||
Write-LogFile
|
||
|
||
.VERSION
|
||
Number: 1.0.0.0 / Date: 04.06.2018
|
||
|
||
.PARAMETER PasswordCreationLenght
|
||
Give the exact Password lenght this Script should generate ValidateRange(4 - 64). Default Value is "8".
|
||
|
||
.EXAMPLE
|
||
New-complexpassword-withlogging -PasswordCreationLenght 5
|
||
|
||
#>
|
||
|
||
[cmdletbinding()]
|
||
|
||
Param (
|
||
|
||
[Parameter(Position=0,Mandatory=$True,HelpMessage='Give the exact Password lenght this Script should generate ValidateRange(4 - 64). Default Value is "8".')]
|
||
[ValidateRange(4,64)]
|
||
[INT]$PasswordCreationLenght=8
|
||
|
||
) #end param
|
||
|
||
BEGIN {
|
||
|
||
#Clear Error Variable
|
||
$error.clear()
|
||
|
||
$FunctionName = $((($MyInvocation.MyCommand.Name) -split "\.")[0].ToString())
|
||
Write-Host "DEBUG Info - $($FunctionName): Begin Function."
|
||
|
||
#Checking if "Write-LogFile" Module was loaded
|
||
IF (Get-Module -All -Name "Write-LogFile") {
|
||
|
||
Write-Host "DEBUG Info - $($FunctionName): Write-LogFile - Module exists."
|
||
|
||
} #end if
|
||
|
||
ELSE {
|
||
|
||
Write-Host ""
|
||
Write-Host "DEBUG Info - $($FunctionName): Write-LogFile - Module does not exist!"
|
||
Write-Host "DEBUG Info - $($FunctionName): Please load the Module and try again, running this Function/Module!"
|
||
Write-Host "DEBUG Info - $($FunctionName): Exiting, because of this Issue."
|
||
EXIT
|
||
|
||
} #end else
|
||
|
||
} #end begin
|
||
|
||
PROCESS {
|
||
|
||
#Clear Error Variable
|
||
$error.clear()
|
||
|
||
#Prepare Variables
|
||
[INT]$Counter1 = 0
|
||
[STRING]$UserPasswordPlainText = $NULL
|
||
[ARRAY]$UserPasswordPlainTextTEMPCharArray = @()
|
||
[ARRAY]$UserPasswordPlainTextTEMPScrambled = @()
|
||
|
||
#Generate a Password
|
||
DO {
|
||
|
||
$UserPasswordPlainText += Get-Random a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
|
||
[int]$Counter1++ | Out-Null
|
||
$UserPasswordPlainText += Get-Random 0,1,2,3,4,5,6,7,8,9
|
||
[int]$Counter1++ | Out-Null
|
||
$UserPasswordPlainText += Get-Random A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
|
||
[int]$Counter1++ | Out-Null
|
||
$UserPasswordPlainText += Get-Random '!','§','$','%','&','/','(',')','?','#'
|
||
[int]$Counter1++ | Out-Null
|
||
|
||
} #end do
|
||
|
||
UNTIL (($Counter1 -eq $PasswordCreationLenght) -or ($Counter1 -gt $PasswordCreationLenght))
|
||
|
||
Write-Host "DEBUG Info - $($FunctionName): New User Password, after generation: $UserPasswordPlainText"
|
||
|
||
#Mix the Password
|
||
$UserPasswordPlainTextTEMPCharArray = $UserPasswordPlainText.ToCharArray()
|
||
$UserPasswordPlainTextTEMPScrambled = $UserPasswordPlainTextTEMPCharArray | Get-Random -Count $UserPasswordPlainTextTEMPCharArray.length
|
||
$UserPasswordPlainText = -Join $UserPasswordPlainTextTEMPScrambled
|
||
|
||
Write-Logfile -LogLine "Complex Password for User created."
|
||
Write-Host "DEBUG Info - $($FunctionName): New User Password, after scrambling: $UserPasswordPlainText"
|
||
|
||
Return [STRING]$UserPasswordPlainText
|
||
|
||
} #end process
|
||
|
||
END {
|
||
|
||
Write-Host "DEBUG Info - $($FunctionName): End Function."
|
||
|
||
} #end end
|
||
|
||
} #end function |