Passing C # parameter to Powershell Script - Unable to check argument in 'Identity' parameter

I am trying to write a Windows Form application in C # that displays AD Attributes for the specified user. The way I want it to work is for the user to enter a value (username) in a text field that is passed as a parameter to the Powershell script, and the output is displayed in the form.

My C # code to create a parameter and call the script is as follows:

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(); } 

My PowerShell script looks like this:

 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 

If I run a script in PowerShell, for example .. \ script.ps1 jsmith with the parameter "jsmith" as the parameter that it works, however, when using the C # parameter, it does not accept this parameter and spits out "

Is there something I did wrong in my C # code that calls this parameter so as not to go to the script and accept it as input?

thanks

+5
source share
1 answer

A few thoughts:

  • The parameter name in C # code is the username
  • The parameter name in the script is accountIdentity
  • Error message indicates Identity parameter

I would think that all 3 should be the same.

If this is not a problem, then a possible way to debug the problem is to turn your C # code into a PS script. For me, at least I would feel more comfortable debugging a PS script where I can quickly change things (for example, when you create myCommand) and check them (using get-member and select-object *) than with WITH#,

Also, for debugging, you can also try combining all the individual PS commands so that you end up with one call to AddScript () instead of the various AddCommand () s along with AddScript (). I vaguely remember the problems with mixing the two when I wrote several similar codes many years ago.

0
source

Source: https://habr.com/ru/post/1235655/


All Articles