मैं सी # में एक विंडोज फॉर्म ऐप लिखने की कोशिश कर रहा हूं जो निर्दिष्ट उपयोगकर्ता के लिए एडी गुणों को आउटपुट करता है। जिस तरह से मैं इसे काम करना चाहता हूं वह यह है कि उपयोगकर्ता एक टेक्स्ट बॉक्स में एक मान (उपयोगकर्ता नाम) इनपुट करता है, जिसे पावरहेल स्क्रिप्ट के पैरामीटर के रूप में पास किया जाता है और आउटपुट फॉर्म में प्रदर्शित होता है।पावरहेल स्क्रिप्ट को सी # पैरामीटर पास करना - पैरामीटर 'पहचान' 0 पर तर्क को मान्य नहीं कर सकता
पैरामीटर बनाने और स्क्रिप्ट लागू के लिए मेरे सी # कोड इस प्रकार है:
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 उदा में स्क्रिप्ट चलाने:
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 स्क्रिप्ट इस प्रकार है \ script.ps1 jsmith 'jsmith' के साथ पैरामीटर के रूप में काम करता है, हालांकि सी # पैरामीटर का उपयोग करते समय यह पैरामीटर स्वीकार नहीं करता है और "हर बार पैरामीटर 'पहचान' त्रुटि पर तर्क मान्य नहीं कर सकता है।
क्या मेरे सी # कोड में कुछ गलत है जो इस पैरामीटर को स्क्रिप्ट को पास नहीं कर रहा है और इसे इनपुट के रूप में स्वीकार कर रहा है?
धन्यवाद