将C#参数传递到Powershell脚本-无法验证参数';上的参数;身份';
本文关键字:参数 身份 验证 参数传递 Powershell 脚本 | 更新日期: 2023-09-27 18:21:36
我正在尝试用C#编写一个Windows窗体应用程序,为指定用户输出AD属性。我希望它的工作方式是,用户在文本框中输入一个值(用户名),该值作为参数传递给Powershell脚本,输出显示在表单中
我用于创建参数和调用脚本的C#代码如下:
private string RunScript(string scriptText)
{
// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add(new Command("Set-ExecutionPolicy Unrestricted -Scope Process", true));
// "Get-Process" returns a collection of System.Diagnostics.Process instances.
pipeline.Commands.Add("Out-String");
//Create parameter and pass value to script
String username = textBox3.Text;
String scriptfile = @"c:''scripts''getpasswordexpirydate.ps1";
Command myCommand = new Command(scriptfile, false);
CommandParameter testParam = new CommandParameter("username", username);
myCommand.Parameters.Add(testParam);
pipeline.Commands.Add(myCommand);
// execute the script
Collection<PSObject> results = pipeline.Invoke();
// close the runspace
runspace.Close();
// convert the script result into a single string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
// return the results of the script that has
// now been converted to text
return stringBuilder.ToString();
}
我的PowerShell脚本如下:
param([string]$username)
function Get-XADUserPasswordExpirationDate() {
Param ([Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, HelpMessage="Identity of the Account")]
[Object] $accountIdentity)
PROCESS {
$accountObj = Get-ADUser $accountIdentity -properties PasswordExpired, PasswordNeverExpires, PasswordLastSet
if ($accountObj.PasswordExpired) {
echo ("Password of account: " + $accountObj.Name + " already expired!")
} else {
if ($accountObj.PasswordNeverExpires) {
echo ("Password of account: " + $accountObj.Name + " is set to never expires!")
} else {
$passwordSetDate = $accountObj.PasswordLastSet
if ($passwordSetDate -eq $null) {
echo ("Password of account: " + $accountObj.Name + " has never been set!")
} else {
$maxPasswordAgeTimeSpan = $null
$dfl = (get-addomain).DomainMode
if ($dfl -ge 3) {
## Greater than Windows2008 domain functional level
$accountFGPP = Get-ADUserResultantPasswordPolicy $accountObj
if ($accountFGPP -ne $null) {
$maxPasswordAgeTimeSpan = $accountFGPP.MaxPasswordAge
} else {
$maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
}
} else {
$maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
}
if ($maxPasswordAgeTimeSpan -eq $null -or $maxPasswordAgeTimeSpan.TotalMilliseconds -eq 0) {
echo ("MaxPasswordAge is not set for the domain or is set to zero!")
} else {
echo ("Password of account: " + $accountObj.Name + " expires on: " + ($passwordSetDate + $maxPasswordAgeTimeSpan))
}
}
}
}
}
}
Get-XADUserPasswordExpirationDate $username
Get-ADUser $username -Properties * | Select-Object DisplayName,LockedOut,LastLogonDate,kPMG-User-GOAccountType,kPMG-User-GOCompanyGroup,kPMG-User-GOFunction,kPMG-User-GOGrade,kPMG-User-GOManagementLevel,kPMG-User-GOMemberFirmGroup,kPMG-User-GPID,kPMG-User-GOMailDisclaimer,kPMG-User-GOMailSync
如果我在PowerShell中运行脚本,例如.''script.ps1 jsmith,并将"jsmith"作为参数,则它可以工作,但当使用C#参数时,它不接受该参数,并且每次都会抛出"无法验证参数"Identity"上的参数"错误。
我的C#代码中有没有做错什么,导致这个参数不能传递到脚本并接受它作为输入?
感谢
几个想法:
- C#代码中的参数名称是username
- 脚本中的参数名称为accountIdentity
- 错误消息引用参数Identity
我认为这三个都应该是一样的。
如果这不是问题所在,那么调试问题的一种可能方法是将C#代码转换为PS脚本。至少对我来说,我会觉得调试PS脚本比使用C#更舒服,在那里我可以快速更改内容(比如在哪里构建myCommand)并检查它们(使用get member和select object*)。
此外,为了进行调试,您还可以尝试组合所有单独的PS命令,以便以AddScript()的单个调用结束,而不是将各种AddCommands()与AddScripts()一起调用。我模糊地记得多年前我写类似代码时,将两者混合在一起的问题。