我正在尝试实现一个定制的PSHost类,以便为Power脚本创建GUI。我已经把这里的代码作为一个基础,并开始将它用于GUI项目https://msdn.microsoft.com/en-us/library/ee706557%28v=vs.85%29.aspx。
在我试图运行Get-凭据cmdlet并得到一个方法未实现的错误之前,一切都很好(Ooouuuu)。最初的代码是以下两种方法:
public override PSCredential PromptForCredential(
string caption,
string message,
string userName,
string targetName)
{
throw new NotImplementedException(
"The method or operation is not implemented.");
}
public override PSCredential PromptForCredential(
string caption,
string message,
string userName,
string targetName,
PSCredentialTypes allowedCredentialTypes,
PSCredentialUIOptions options)
{
throw new NotImplementedException(
"The method or operation is not implemented.");
}因此,经过一些研究,我实现了这样的对话:
[DllImport("ole32.dll")]
public static extern void CoTaskMemFree(IntPtr ptr);
[DllImport("credui.dll", CharSet = CharSet.Auto)]
private static extern int CredUIPromptForWindowsCredentials(ref CREDUI_INFO notUsedHere, int authError, ref uint authPackage, IntPtr InAuthBuffer, uint InAuthBufferSize, out IntPtr refOutAuthBuffer, out uint refOutAuthBufferSize, ref bool fSave, int flags);
[DllImport("credui.dll", CharSet = CharSet.Auto)]
private static extern bool CredUnPackAuthenticationBuffer(int dwFlags, IntPtr pAuthBuffer, uint cbAuthBuffer, StringBuilder pszUserName, ref int pcchMaxUserName, StringBuilder pszDomainName, ref int pcchMaxDomainame, StringBuilder pszPassword, ref int pcchMaxPassword);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct CREDUI_INFO
{
public int cbSize;
public IntPtr hwndParent;
public string pszMessageText;
public string pszCaptionText;
public IntPtr hbmBanner;
}
private enum PromptForWindowsCredentialsFlags
{
...
}
public override PSCredential PromptForCredential(
string caption,
string message,
string userName,
string targetName,
PSCredentialTypes allowedCredentialTypes,
PSCredentialUIOptions options)
{
CREDUI_INFO credui = new CREDUI_INFO();
credui.pszCaptionText = "Please enter the credentails";
credui.pszMessageText = "DisplayedMessage";
credui.cbSize = Marshal.SizeOf(credui);
uint authPackage = 0;
IntPtr outCredBuffer = new IntPtr();
uint outCredSize;
bool save = false;
int result = CredUIPromptForWindowsCredentials(ref credui, 0, ref authPackage, IntPtr.Zero, 0, out outCredBuffer, out outCredSize, ref save, 1 /* Generic */);
var usernameBuf = new StringBuilder(100);
var passwordBuf = new StringBuilder(100);
var domainBuf = new StringBuilder(100);
int maxUserName = 100;
int maxDomain = 100;
int maxPassword = 100;
if (result == 0)
{
if (CredUnPackAuthenticationBuffer(0, outCredBuffer, outCredSize, usernameBuf, ref maxUserName, domainBuf, ref maxDomain, passwordBuf, ref maxPassword))
{
//clear the memory allocated by CredUIPromptForWindowsCredentials
CoTaskMemFree(outCredBuffer);
SecureString secureString = new SecureString();
foreach (char c in passwordBuf.ToString())
{
secureString.AppendChar(c);
}
return new PSCredential(usernameBuf.ToString(), secureString);
}
}
return null;
}这很好,但是有一个问题,当运行Get-凭据cmdlet时,它提示为凭证参数输入一个值,而不管在点击返回后的任何输入,对话框会弹出,一切都按预期进行。在ISE中做同样的事情,对话直接弹出,没有任何提示。我认为这是由于方法的参数,但是通过定义默认值使它们是可选的并没有什么区别。这可能是由于cmdlet是如何在PS环境中定义的,所以我的问题是:如何才能使cmdlet的运行直接跳转到对话中?是否可以为要绑定的凭据参数定义默认值?我猜这就是ISE绕过这件事的方法,还是我遗漏了什么?
发布于 2015-06-10 11:01:06
我花了一些时间进行实验,但我发现,强制参数绑定在调用public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)之前由PSHostUserInterface接口中的PromptForCredential方法处理。
https://stackoverflow.com/questions/30260522
复制相似问题