Sunday, 5 April 2015

C# With PowerShell Import Module Active Directory



How to import Module ActiveDirectory in C#, This is just an example how can you integrate powershell with C#, If you are ITPro code looks simple with the help of powershell.



ConsoleApplication and Powershell


Saturday, 4 April 2015

Calling Powershell from C#.

Calling Powershell from C#



In the solution explorer, add a project reference to the System.Management.Automation. I always found this in 





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Provider;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;


namespace PowerShell_Call
{
    class Program
    {

        public static string name(string args)
        {

            using (PowerShell powershell = PowerShell.Create().AddCommand("Get-Process").AddArgument(args))
            {
             
                foreach (PSObject result in powershell.Invoke())
                {
                    return result.Members["id"].Value.ToString();
                 
                                 }
            }

            return "";
         

        }    
     
     
        static void Main(string[] args)
        {

            string x = "apnmcp";
            string v=name(x);          
            Console.WriteLine("The value is {0}", v);
            Console.ReadLine();

        }
    }
}

Output :

Console Application


Powershell Output.


Tuesday, 2 December 2014

PowerShell .NET OBJECT



I believe powershell V5 will close the gap between ITPRO and Developers and this is a chance to jump into the bus of Developers if we miss it some how.

Let see Method or Function overloading.

What is Function Overloading, Same name of the function and with multiple parameters.

Here the name of the class is Test and we have two function with the same name ADD but the number of parameter is different, that is what function overloading from the eye of ITPRO.
















Constructor A constructor initializes an Object when it is created, It has the same name as its class and similar to method ,However constructor have no explicit return type.



Thursday, 20 November 2014

Desired State configuration

Desired State configuration.




A simple scenario what felt me to understand it, A lots need to cover and learn, but thought sharing it will be worth for the beginner like me. I am just in love with DSC.  Making sense Ahha :)

Execution happen in three steps, Make the configuration, Call it and Make it so (pointer to MOF)

 - Install PowerShell Version 4.0 and enable PowerShell Remoting.

A common problem on laptops is that the initial PowerShell Remoting configuration will fail because the system is connected to a public network.








Step 1.

Enable-PSRemoting -SkipNetworkProfileCheck -Force

Step 2. Make the configuration
#=====================================

Configuration MyFirstDSC
{


    Node 'Anirban-PC'
    {
        Service VM
        {
            Name = 'VMwareHostd'
            State = 'Running'
            StartupType = 'Automatic'
         

        }
    }
}

MyFirstDSC

#=================================================
Step 3 Execute the Powershell and it will create the Folder MyFirstDSC having the MOF


Step 4 Make it So.



Saturday, 15 November 2014

Error-Handling Session in PSBUG

Thanks Ravi Sir for the DSC book and Deepak for giving the opportunity to speak in the group meet and Audience who come and join us today.


Error-Handling in PSBUG meet.


Sunday, 5 October 2014

Sync Error Series - An object with DN "CN=" already exists in management agent

Note : This series is for the Beginner and trouble shooting the Error With Sync Engine, Based on Test Lab, Highly advice do not do Full Sync of the Management Agent. Errors are Fun, Debug It...!!

The Management Agent Run and see the error message in the Stack information.

"Microsoft.MetadirectoryServices.ObjectAlreadyExistsException: An object with DN "CN=4,OU=FIMOU,DC=corp,DC=fim2010,DC=com" already exists in management agent "ADMATest".
   at Microsoft.MetadirectoryServices.Impl.ConnectorImpl.Commit()
   at Miis_Metaverse.MVExtensionObject.Microsoft.MetadirectoryServices.IMVSynchronization.Provision(MVEntry mventry)"



The error message arise because there is already an existing object with the same DN in provisioning connector space.















Step 1.  Search the user in metaverse and the object will not existing in MV as it fails during the provisioning, First challenge is to bring the Object in MV.

Step 2.  Disable the Provisioning Rule Extension ( Tool-Options)
             Unchecked the option Enable Provisioning Rule Extension.




















Step 3. Go to the connector space of the MA and commit preview, this will project the object in MV.

After Step 3,checked the option Enable Provisioning Rule Extension. See Step 2,
Step 4. See How to join connector Space Object to a MV object, the tech net link is below

Join a Connector Space Object to a Metaverse Object Link

 Go to Joiner Tab and Join the existing disconnect object to newly project object in MV.

This will fix the Error .. !! 




ServiceNow onboarding Sailpoint