gpt4 book ai didi

c# - 将 C# 参数传递给 Powershell 脚本 - 无法验证参数 'Identity' 上的参数

转载 作者:太空狗 更新时间:2023-10-29 23:50:12 24 4
gpt4 key购买 nike

我正在尝试用 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

我认为所有 3 个应该是相同的。

如果这不是问题,那么调试问题的可能方法是将 C# 代码转换为 PS 脚本。至少对我而言,与使用 C# 相比,我感觉调试 PS 脚本更自在,我可以在其中快速更改内容(例如构建 myCommand 的位置)并检查它们(使用 get-member 和 select-object *)。

此外,对于调试,您还可以尝试组合所有单独的 PS 命令,以便以一次调用 AddScript() 结束,而不是将各种 AddCommand() 与 AddScript() 一起调用。我依稀记得多年前我编写类似代码时将两者混合使用时遇到的问题。

关于c# - 将 C# 参数传递给 Powershell 脚本 - 无法验证参数 'Identity' 上的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33642509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com