2015-11-11 4 views
5

指定したユーザーのAD属性を出力するC#でWindowsフォームアプリケーションを作成しようとしています。私はそれを動作させる方法は、ユーザーがPowershellスクリプトにパラメータとして渡され、出力がフォームに表示されるテキストボックスに値(ユーザー名)を入力することです。PowershellスクリプトにC#パラメータを渡す - パラメータ 'Identity'の引数を検証できません

次のようにパラメータを作成し、スクリプトを呼び出すための私の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

いくつかの考え:

  • C#コードでのパラメータ名は、ユーザ名
  • あるスクリプト内のパラメータ名はaccountIdentity
  • は、エラーメッセージの参照パラメータであるアイデンティティ

私はすべて3が同じであるべきだと思います。

問題が解決しない場合は、C#コードをPSスクリプトに変換することができます。私にとっては、少なくとも、私はあなたがC#で行うよりも(myCommandをビルドする場所のように)素早く変更して(getメンバとselect-object *を使って)調べることができるPSスクリプトのデバッグをより快適に感じるでしょう。

また、デバッグでは、さまざまなAddCommand()の代わりにAddScript()を使用するのではなく、AddScript()を1回呼び出すことで終了するように、個々のPSコマンドを組み合わせることもできます。私は何年も前に幾分似たようなコードを書いていたときに、2つを混在させることでぼんやりと覚えています。

関連する問題