I am trying to write a password generator with gui in powershell. I wrote the gui first and the random password generation funktion seperately. When i fused my funktion and the program together i ended up with this error message:
Für "ToInt32" und die folgende Argumenteanzahl kann keine Überladung gefunden
werden: "0".
In C:\Users\sh\Desktop\Test Powershell PWG_01.ps1:130 Zeichen:5
$a = -join ($UpperCaseLetters
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [], MethodException
FullyQualifiedErrorId : MethodCountCouldNotFindBest
It accured after i "fixed" an other error saying i can not convert an object to an int. The error was in line 59 My possibly wrong fix for that was to put an [int]before the $length:
$ActionButton.Add_Click({Get-RandomPassword (([int]$length =
$Zeichen.SelectedIndex), $chkB1, $chkB2, $chkB3 )})
This is my complete code: I used 1 Image and an icon for my gui so if u want to recreate the error be sure to remove them from (my) line 6 & 10
Add-Type -AssemblyName System.Windows.Forms
Add-Type -a System.Drawing #lädt die .NET-Erweiterungen für GUI # -a ist ein Kürzel oder -an
$objForm = New-Object System.Windows.Forms.Form # Fenster erstellen
$objForm.Text = "Password Manager Ultimate"
$objForm.Icon="C:\Users\sh\Desktop\PWMicon.ico" # Pfad für Icon
$objForm.Backcolor="white"
$objForm.Size = New-Object System.Drawing.Size(600,700)
$objForm.BackgroundImageLayout = 2
$objForm.BackgroundImage =[System.Drawing.Image]::FromFile('C:\Users\sh\Desktop\schloss.jpg') # Pfad für bgimage
$objLabel = New-Object System.Windows.Forms.Label # Objektklasse "Label aus der Forms Assembly
$objLabel.Location = New-Object System.Drawing.Size(200,480)
$objLabel.Size = New-Object System.Drawing.Size(150,20)
$objLabel.Text = "Email Adresse hinzufügen"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox # Eingabefeld
$objTextBox.Location = New-Object System.Drawing.Size(200,500)
$objTextBox.Size = New-Object System.Drawing.Size(200,20)
$objForm.Controls.Add($objTextBox)
$Description = New-Object system.Windows.Forms.Label # Beschreibung Zeichenlänge
$Description.text = "Zeichenlänge"
$Description.AutoSize = $false
$Description.width = 75
$Description.height = 20
$Description.location = New-Object System.Drawing.Point(20,120)
$objForm.Controls.Add($Description)
$Zeichen = New-Object system.Windows.Forms.ComboBox
$Zeichen.text = ""
#$Zeichen.width = 170
$Zeichen.autosize = $true
# Add the items in the dropdown list
@(8..32) | ForEach-Object {[void] $Zeichen.Items.Add($_)} ##
# Select the default value
$Zeichen.SelectedIndex = 0
$Zeichen.location = New-Object System.Drawing.Point(20,150)
$Zeichen.Font = 'Microsoft Sans Serif,10'
$Zeichen.Width = 45
$objForm.Controls.Add($Zeichen)
$txtHost = New-Object Windows.Forms.TextBox # Ausgabefeld für generiertes PW
$txtHost.TabIndex = 0 # Tab order
$txtHost.Top = 150; $txtHost.Left = 200; $txtHost.Width = 200;
$txtHost.height = 20
$txtHost.Text = $a
$objForm.Controls.Add($txtHost)
$ActionButton = New-Object System.Windows.Forms.Button # Generieren Button
# Die nächsten beiden Zeilen legen die Position und die Größe des Buttons fest
$ActionButton.Location = New-Object System.Drawing.Size(262,120)
$ActionButton.Size = New-Object System.Drawing.Size(76,23)
$ActionButton.Text = "Generieren"
$ActionButton.Name = "Generieren"
$ActionButton.DialogResult = "Cancel"
#Die folgende Zeile ordnet dem Click-Event die Schließen-Funktion für das Formular zu
$ActionButton.Add_Click({Get-RandomPassword (([int]$length = $Zeichen.SelectedIndex), $chkB1, $chkB2, $chkB3 )})
$objForm.Controls.Add($ActionButton)
$SaveButton = New-Object System.Windows.Forms.Button # Speichern Button
# Die nächsten beiden Zeilen legen die Position und die Größe des Buttons fest
$SaveButton.Location = New-Object System.Drawing.Size(450,500)
$SaveButton.Size = New-Object System.Drawing.Size(75,23)
$SaveButton.Text = "Speichern"
$SaveButton.Name = "Speichern"
$SaveButton.DialogResult = "Cancel"
#Die folgende Zeile ordnet dem Click-Event die Schließen-Funktion für das Formular zu
$SaveButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($SaveButton)
$chkB1 = New-Object Windows.Forms.checkbox
$chkB1.Left = 20; $chkB1.Width = 200; $chkB1.Top = 10
$chkB1.Text = "Groß - und Kleinbuchstaben"
$chkB1.Checked = $true
$chkB1.TabIndex = 2
$objForm.Controls.Add($chkB1)
$chkB2 = New-Object Windows.Forms.checkbox
$chkB2.Left = 20; $chkB2.Width = 200; $chkB2.Top = 35
$chkB2.Text = "mit Zahlen"
$chkB2.Checked = $true
$chkB2.TabIndex = 2
$objForm.Controls.Add($chkB2)
$chkB3 = New-Object Windows.Forms.checkbox
$chkB3.Left = 20; $chkB3.Width = 200; $chkB3.Top = 60
$chkB3.Text = "mit Zahlen und Sonderzeichen"
$chkB3.Checked = $false
$chkB3.TabIndex = 2
$objForm.Controls.Add($chkB3)
$CancelButton = New-Object System.Windows.Forms.Button # Schließen Button
# Die nächsten beiden Zeilen legen die Position und die Größe des Buttons fest
$CancelButton.Location = New-Object System.Drawing.Size(450,600)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Backcolor="yellow"
$CancelButton.Text = "Beenden"
$CancelButton.Name = "Beenden"
$CancelButton.DialogResult = "Cancel"
#Die folgende Zeile ordnet dem Click-Event die Schließen-Funktion für das Formular zu
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
# 2 Funktionen zum generieren des PW #Funktion1 : Chars ##gb $chkB1.Checked ## $Zeichen.SelectedIndex ######################################
function Get-RandomPassword ($length, $chkB1, $chkB2, $chkB3){
if ($chkB1){$UpperCaseLetters = (65..90)}
else {$UpperCaseLetters = (97..122)}
if ($chkB2){$NumbersZeroToNine = (48..57)}
else {$NumbersZeroToNine = (97..122)}
if ($chkB3){$SpecialCharacters = (58..64)}
else {$SpecialCharacters = (97..122)}
$a = -join ($UpperCaseLetters
$LowerCaseLetters
$NumbersZeroToNine
$SpecialCharacters |
Get-Random -Count $length.ToInt32() | % {[char]$_} )
$a
$a |clip
return $a
}
# Get-RandomPassword ($length)
# Funktion zum Prüfen des Benutzerkontos
function Test-Cred {
[CmdletBinding()]
[OutputType([String])]
Param (
[Parameter(
Mandatory = $false,
ValueFromPipeLine = $true,
ValueFromPipelineByPropertyName = $true
)]
[Alias(
'PSCredential'
)]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credentials
)
$Domain = $null
$Root = $null
$Username = $null
$Password = $null
If($Credentials -eq $null)
{
Try
{
$Credentials = Get-Credential "domain\$env:username" -ErrorAction Stop
}
Catch
{
$ErrorMsg = $_.Exception.Message
Write-Warning "Failed to validate credentials: $ErrorMsg "
Pause
Break
}
}
# Checking module
Try
{
# Split username and password
$Username = $credentials.username
$Password = $credentials.GetNetworkCredential().password
# Get Domain
$Root = "LDAP://" ([ADSI]'').distinguishedName
$Domain = New-Object System.DirectoryServices.DirectoryEntry($Root,$UserName,$Password)
}
Catch
{
$_.Exception.Message
Continue
}
If(!$domain)
{
Write-Warning "Ups.. Etwas ist schief gelaufen."
}
Else
{
If ($null -ne $domain.name) # Fehlerhinweis vscode null soll links stehen. jetzt ist es Angepasst
{
return "Authenticated"
}
Else
{
return "Not authenticated"
}
}
} # Ende Einlogg Funktion
$CredCheck = 0
$Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$CredCheck = $Credentials | Test-Cred togrund\$env:USERNAME # einloggen # domain überprüfen später
Clear-Host
If($CredCheck -eq "Authenticated")
{
[void] $objForm.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true })) # Fenster im Vordergrund anzeigen
}
else #wenn pw inkorrekt
{
Write-Warning "Login fehlgeschlagen"
Break
}
CodePudding user response:
Focusing only on the error and how to solve it, the Windows Forms code is completely irrelevant to the question and, for future note, you should only ask a specific question on a specific problem.
It's also unclear why you're trying to do such thing. To simplify your function, and make it work properly:
function Get-RandomPassword {
param(
[Parameter(Mandatory)]
[int] $Length,
[Parameter()]
[switch] $ChkB1,
[Parameter()]
[switch] $ChkB2,
[Parameter()]
[switch] $ChkB3
)
end {
$charsToPwd = @(
97..122
if($chkB1.IsPresent) { 65..90 }
if($chkB2.IsPresent) { 48..57 }
if($chkB3.IsPresent) { 58..64 }
) | Get-Random -Count $Length
$newPwd = [string]::new([char[]] $charsToPwd)
Set-Clipboard -Value $newPwd
$newPwd
}
}
Testing how it works:
Get-RandomPassword -Length 20 # wmndzheavfgjuxqblcrs
Get-RandomPassword -Length 20 -ChkB1 # XsVrdycpoHTlNvqjZhGn
Get-RandomPassword -Length 20 -ChkB2 # 3b84chjnx5zaflkyri19
Get-RandomPassword -Length 20 -ChkB3 # r?dox:v=pz@hmucnjf>y
Get-RandomPassword -Length 20 -ChkB1 -ChkB2 -ChkB3 # WLCxJ694jcdEhI=a8VP1
Implementing it into your .Add_Click
event:
$ActionButton.Add_Click({
$params = @{
Length = $Zeichen.Text
ChkB1 = $chkB1.Checked
ChkB2 = $chkB2.Checked
ChkB3 = $chkB3.Checked
}
Get-RandomPassword @params
})