I hope you can help me with a little thing.
I currently have a project that works exclusively with a login. As soon as the program is started, a loginForm opens. When the credentials are submitted, the permissions are checked.
Now the problem: As soon as the mainForm opens after login, the loginForm does not close. How can I close the loginForm correctly?
Here is the scriptblock of the Returnbutton in the loginform:
Code: Select all
$buttonOK_Click={
[bool]$loginCheck = $true
$script:loginTries = 0
while ($loginCheck)
{
$loginTries++
if ($loginTries -le 3)
{
try
{
# Read credentials
$PWord = ConvertTo-SecureString -String $textboxPassword.Text -AsPlainText -Force
$script:userCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $textboxUsername.Text, $PWord
if (!$userCredential) { exit }
$username = $userCredential.username
$password = $userCredential.GetNetworkCredential().password
# Get current domain using logged-on user's credentials
$CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain, $UserName, $Password)
ADSession -DomainController $domaincontrollerLenze -Credentials $userCredential
}
catch { }
# Check the membership of the user in the AD group "ww-rol-AdminPanel"
$checkForGroup = Get-ADGroupMember -Identity "ww-rol-AdminPanel" | where { $_.SamAccountName -like $username } -ErrorAction SilentlyContinue
if (($domain.name -eq "company1" -or $domain.name -eq "comapny2") -and $checkForGroup)
{
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
if ((OnApplicationLoad -DomainController $domaincontrollerCompany1 -ExchangeServer $exchangeserverCompany2 -Credentials $userCredential) -eq $true)
{
if (Show-MainForm_psf -eq $true)
{
$loginCheck = $false
#$formLogon.Close()
}
}
}
else
{
$loginMsg = "Authentication failed!`r`nPlease check your username and password!`r`nDo you want to try again?"
if ($loginTries -lt 3)
{
if (!([System.Windows.Forms.MessageBox]::Show($loginMsg, "Warning", 'YesNo', 'Error', 'Button1', 'DefaultDesktopOnly') -eq "Yes")) { exit; $loginCheck = $false }
}
}
}
else { $loginCheck = $false }
}
$formLogon.Close()
Write-Host "test"
}
bhnuser