jeudi 10 juin 2021

Powershell foreach and if statement

I am trying to check against the credential manager for any "Domain" entry, If "Domain" entry DOESN"T exist then pop up a window for user to enter username and password. If it exist then simply stop script.

This PowerShell script is suppose to run at windows login in order to add domain credentials if they don't exist in the Windows Credential Manager, and then maps a network drive. If the script detects no domain credentials added in the Windows Credential Manager - then it will prompt the user to enter it.

I am confused at where i am wrong in my logic, when the code runs it ignores the IF statement and carries on even if there is a domain user in the Windows Credential Manager and run the cmdkey /add and net use command.

This is my code:

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
$error.clear()


try { 


cmdkey /list | foreach {
if($_ -match 'Domain')
    {
        [PSCustomObject]@{Account = $matches.1}
        [System.Windows.Forms.MessageBox]::Show("Already exist!")
        
    }

    

else {

    
# Captures username
#-------------------------------------------------------------------------


Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form 
$form.Text = "Enter Username"
$form.Size = New-Object System.Drawing.Size(300,200) 
$form.StartPosition = "CenterScreen"

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Enter Username: "
$form.Controls.Add($label) 

$textBox = New-Object System.Windows.Forms.TextBox 
$textBox.Location = New-Object System.Drawing.Point(10,40) 
$textBox.Size = New-Object System.Drawing.Size(260,20) 
$form.Controls.Add($textBox) 

$form.Topmost = $True

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $textBox.Text
    $x
}




# Captures password
#-------------------------------------------------------------------------

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form 
$form.Text = "Enter Password"
$form.Size = New-Object System.Drawing.Size(300,200) 
$form.StartPosition = "CenterScreen"

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Enter Password: "
$form.Controls.Add($label) 

$textBox = New-Object System.Windows.Forms.TextBox 
$textBox.Location = New-Object System.Drawing.Point(10,40) 
$textBox.Size = New-Object System.Drawing.Size(260,20) 
$form.Controls.Add($textBox) 

$form.Topmost = $True

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x2 = $textBox.Text
    $x2
}

 cmdkey.exe /add:ae7msd-dc /user:$x /pass:$x2
 net use U: "\\SERVER\FOLDER" /persistent:yes
 break

}

}
}




catch { [System.Windows.Forms.MessageBox]::Show("Not Working!") }
if (!$error) {
[System.Windows.Forms.MessageBox]::Show("IT'S DONE!")
}

This is the output i get on the Powershell editor:

PS C:\Users\hadi> C:\temp\add-cred.ps1

GAC    Version        Location                                                                                                                      
---    -------        --------                                                                                                                      
True   v4.0.30319     C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll       
hmohsen
Abc!2345

CMDKEY: Credential added successfully.
The command completed successfully.

OK

The logic is flawed, but how can i fix it please!

Aucun commentaire:

Enregistrer un commentaire