簡體   English   中英

將C#參數傳遞給Powershell腳本 - 無法驗證參數'Identity'的參數

[英]Passing C# Parameter to Powershell Script - Cannot validate argument on parameter 'Identity'

我試圖在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腳本。 對我來說,至少,我覺得調試一個PS腳本更舒服,我可以快速更改(比如你構建myCommand的地方)並檢查它們(使用get-member和select-object *),而不是使用C#。

同樣,對於調試,您可能還嘗試組合所有單獨的PS命令,以便以AddScript()的單個調用結束,而不是使用AddScript()而不是各種AddCommand()。 我隱約記得多年前編寫類似代碼時混合二者的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM