我正在尝试在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#代码中是否有错误导致此参数无法传递给脚本并将其作为输入接受?
由于
答案 0 :(得分:0)
一些想法:
我认为所有3都应该是一样的。
如果这不是问题,那么调试问题的一种可能方法是将您的C#代码转换为PS脚本。对我来说,至少,我觉得调试一个PS脚本更舒服,我可以快速更改(比如你构建myCommand的地方)并检查它们(使用get-member和select-object *),而不是使用C#。
另外,对于调试,您也可以尝试组合所有单独的PS命令,以便以AddScript()的单个调用结束,而不是使用AddScript()而不是各种AddCommand()。我模糊地记得多年前我写过类似的代码时混淆两者的问题。