Search This Blog

Wednesday, April 12, 2017

Execute Parameterize PowerShell Script With C#(Reset Possword )

  


param(
[string]$a
)
foreach ($i in $a
{
    write  "Error: $i" >>c:\logfile.txt
}
write  "Error: $a" >>c:\logfile.txt
write  "Error: first" >>c:\logfile.txt
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
       Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
write  "Error: pa" >>c:\logfile.txt
$username = 'NGO\ngoadmin'
$Password = 'NGO@dmin1' | ConvertTo-SecureString -Force -AsPlainText
$credential = New-Object System.Management.Automation.PsCredential($username, $Password)
try
{
   write  "Error: $a" >>c:\logfile.txt
   $job = Start-Job -scriptblock {
            $sk = 'CN=Exit User1,OU=ExitUsersOU,OU=NGO-TEST_OU,DC=ngo,DC=NGO,DC=com'
            Set-ADAccountPassword  $args[0] -Server "NGO.com" -Reset -NewPassword (ConvertTo-SecureString-AsPlainText "Vinay@4435" -Force)
         }-ArgumentList $a -credential $credential
         Receive-Job $job
         Wait-Job $job
         write  "Error: $job" >>c:\logfile.txt 
 write  "Error: r" >>c:\logfile.txt

  
}
catch [Exception] {

       write  "Error: $_.Exception.Message" >>c:\logfile.txt

       
}


C# Console code

public static string dynamicReplacementParameterInScript = "";
dynamicReplacementParameterInScript = GetCNUserWise(LoginName);
                        if (dynamicReplacementParameterInScript != null)
                        {
                            // start Exucute Powerhell Script
                         string PowerShellScriptOutPutResult = "";
                         try
                            {
                                PowerShell ps = PowerShell.Create();
                                ps.AddScript(@"C:\temp\Ram.ps1 '" + dynamicReplacementParameterInScript + "'");
                                ps.Invoke();
                                PowerShellScriptOutPutResult = "Success";
                            }
                         catch (Exception ex)
                            {
                                PowerShellScriptOutPutResult = "Failed";
                            }
                }



public static string GetCNUserWise(string loginName)
        {
            string strCN = "";
            try
            {
            string sa = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            txtEmp = sa.Split('\\')[1];
            string strDLdap = ConfigurationManager.AppSettings["myLDAPString"].ToString();
            DirectoryEntry connection = new DirectoryEntry(strDLdap);
            string strDLdAPUser = ConfigurationManager.AppSettings["myLDAPUser"].ToString();
            string strDLdAPPass = ConfigurationManager.AppSettings["myLDAPPassword"].ToString();
            connection.AuthenticationType = AuthenticationTypes.Secure;
            connection.Username = strDLdAPUser;
            connection.Password = strDLdAPPass;
            DirectorySearcher dssearch = new DirectorySearcher(connection);
            dssearch.Filter = "(sAMAccountName=" + txtEmp + ")";
            SearchResult sresult = dssearch.FindOne();
            DirectoryEntry dsresult = sresult.GetDirectoryEntry();
            string strfirstName = dsresult.Properties["givenName"][0].ToString();
            string strlastName = dsresult.Properties["sn"][0].ToString();
            string Name = strfirstName + "" + strlastName;
            string strRequestedBy = dsresult.Properties["mail"][0].ToString();
            string strComapnay = dsresult.Properties["company"][0].ToString();
            displayName = dsresult.Properties["displayName"][0].ToString();
            InsertUserInfo(displayName, strComapnay, DateTime.Now.ToString("MM-dd-yyyy"), strRequestedBy);

           }
           
             catch (Exception ex)
            {
                // SPUser user = web.CurrentUser;
                DBLayer_LogErrors objLogError = new DBLayer_LogErrors();
                Global_LogErrorsItem objLogErrorItem = new Global_LogErrorsItem();
                objLogErrorItem.Method = "GetCNUserWise()";
                objLogErrorItem.Class = "NGOTaskExecutionProcess" + "" + "Not get DistinguishedName from Active Directive ";
                objLogErrorItem.ErrorDescription = ex.Message;
                objLogErrorItem.StackTrace = ex.StackTrace;
                objLogErrorItem.UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                //objLogErrorItem.UserName = user.LoginName;
                objLogError.AddLogError(objLogErrorItem);
              
            }
            return strCN;
        }




No comments:

Post a Comment