除了 Get-Credential 之外,还有其他方法可以在 Powershell 中请求凭据吗?

Is there another way to ask for credentials in Powershell but Get-Credential?

我的 Powershell 脚本使用以下语法请求凭据,它工作正常:

$credentials = (Get-Credential -Message "Enter Password")

但问题是弹出来输入用户名和密码的window很少space并且用户输入大用户名(大服务器名反斜杠)不舒服大用户名),并且 windows 不允许调整大小。

我想让我的用户能够在另一个更大的 windows 中输入用户名和密码。 有什么想法吗?

我有类似的东西所以我想为什么不分享它。试一试,它看起来不如 Get-Credential 但你可以调整它的大小。

它将return一个PSCredential对象,与Get-Credential相同。
如果 UsernamePassword 有文字,OK 按钮只会变成 Enabled

Add-Type -AssemblyName System.Windows.Forms

$refAssemblies = @(
    'System.Drawing'
)
if($PSVersionTable.PSVersion.Major -ge 6)
{
    $refAssemblies += 'System.Drawing.Common'
}

Add-Type -ReferencedAssemblies $refAssemblies -IgnoreWarnings -WarningAction Ignore -TypeDefinition @'
using System; 
using System.Runtime.InteropServices;
using System.Drawing;

public class DPI
{  
    [DllImport("gdi32.dll")]
    static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

    public enum DeviceCap
    {
        VERTRES = 10,
        DESKTOPVERTRES = 117
    } 

    public static float scaling()
    {
        Graphics g = Graphics.FromHwnd(IntPtr.Zero);
        IntPtr desktop = g.GetHdc();
        int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
        int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);

        return (float)PhysicalScreenHeight / (float)LogicalScreenHeight;
    }
}
'@

[void][System.Windows.Forms.Application]::EnableVisualStyles()
$DPI = [math]::round([dpi]::scaling(),2) * 100
$bounds = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea
$bounds.Width = ($bounds.Width / 100) * $DPI
$bounds.Height = ($bounds.Height / 100) * $DPI

$mainForm = [System.Windows.Forms.Form]::new()
$mainForm.StartPosition = 'CenterScreen'
$mainForm.FormBorderStyle = 'Sizable'
$mainForm.Text = 'Custom Get Credential'
$mainForm.WindowState = 'Normal'
$mainForm.KeyPreview = $True
$mainForm.Font = [System.Drawing.Font]::new('Calibri',12,[System.Drawing.FontStyle]::Regular)
$mainForm.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Get-Process -Id $PID).Path)
$mainForm.MinimumSize = [System.Drawing.Size]::new(($bounds.Width/4.5),($bounds.Height/4.5))
$mainForm.MaximumSize = [System.Drawing.Size]::new($bounds.Width,($bounds.Height/4.5))
$mainForm.MaximizeBox = $false
$mainForm.Size = $mainForm.MinimumSize

$credentialMsg = [System.Windows.Forms.Label]::new()
$credentialMsg.Location = [System.Drawing.Size]::new(10,10)
$credentialMsg.Size = [System.Drawing.Size]::new(($mainForm.Width-30),30)
$credentialMsg.Text = 'Supply values for the following parameters:'
$mainForm.Controls.Add($credentialMsg)

$userLbl = [System.Windows.Forms.Label]::new()
$userLbl.Location = [System.Drawing.Size]::new(10,50)
$userLbl.Size = [System.Drawing.Size]::new(80,30)
$userLbl.Text = 'Username'
$mainForm.Controls.Add($userLbl)

$userTxtBox = [System.Windows.Forms.TextBox]::new()
$userTxtBox.Location = [System.Drawing.Size]::new(($userLbl.Width+10),50)
$userTxtBox.Size = [System.Drawing.Size]::new(($mainForm.Width-120),60)
$mainForm.Controls.Add($userTxtBox)

$passwordLbl = [System.Windows.Forms.Label]::new()
$passwordLbl.Location = [System.Drawing.Size]::new(10,($userLbl.Location.Y+40))
$passwordLbl.Size = [System.Drawing.Size]::new(80,30)
$passwordLbl.Text = 'Password'
$mainForm.Controls.Add($passwordLbl)

$passwordTxtBox = [System.Windows.Forms.TextBox]::new()
$passwordTxtBox.Location = [System.Drawing.Size]::new(($passwordLbl.Width+10),($userTxtBox.Location.Y+40))
$passwordTxtBox.Size = [System.Drawing.Size]::new(($mainForm.Width-120),60)
$passwordTxtBox.UseSystemPasswordChar = $True
$passwordTxtBox.Anchor = 'top,left'
$mainForm.Controls.Add($passwordTxtBox)

$cancelBtn = [System.Windows.Forms.Button]::new()
$cancelBtn.Location = [System.Drawing.Size]::new(($mainForm.Width-110),($passwordTxtBox.Location.Y+40))
$cancelBtn.Size = [System.Drawing.Size]::new(80,35)
$cancelBtn.Text = '&Cancel'
$cancelBtn.Anchor = 'right,bottom'
$cancelBtn.Add_Click({
    $mainForm.DialogResult = 'Cancel'
})
$mainForm.Controls.Add($cancelBtn)

$okBtn = [System.Windows.Forms.Button]::new()
$okBtn.Location = [System.Drawing.Size]::new(($cancelBtn.Location.X-$cancelBtn.Width-5),$passwordTxtBox.Location.Y+40)
$okBtn.Size = $cancelBtn.Size
$okBtn.Text = '&OK'
$okBtn.Anchor = $cancelBtn.Anchor
$okBtn.Enabled = $false
$okBtn.Add_Click({
    $mainForm.DialogResult = 'OK'
})
$mainForm.Controls.Add($okBtn)

$okBtnEnableEvent = {
    if(
        [string]::IsNullOrWhiteSpace($userTxtBox.Text) -or 
        [string]::IsNullOrWhiteSpace($passwordTxtBox.Text)
    )
    {
        $okBtn.Enabled = $false
    }
    else
    {
        $okBtn.Enabled = $True
    }
}
$userTxtBox.Add_TextChanged($okBtnEnableEvent)
$passwordTxtBox.Add_Textchanged($okBtnEnableEvent)

$mainForm.Add_Resize({
    $userTxtBox.Size = [System.Drawing.Size]::new(($this.Width-120),60)
    $passwordTxtBox.Size = [System.Drawing.Size]::new(($this.Width-120),60)
})

$mainForm.AcceptButton = $okBtn
$mainForm.CancelButton = $cancelBtn
$mainForm.Add_Shown({ $this.Activate() })

function New-PSCredential {
param(
    [string]$Username,
    [string]$Password
)
    
    $passw = ConvertTo-SecureString $Password.Trim() -AsPlainText -Force
    [System.Management.Automation.PSCredential]::new($Username.Trim(),$passw)
}

if('OK' -eq $mainForm.ShowDialog())
{
    New-PSCredential -Username $userTxtBox.Text -Password $passwordTxtBox.Text
}

$mainForm.Dispose()

编辑: