Run powershell scripts from Windows Forms C #?

0

I need to run the following command that is in an auth.ps1 file

$session = New-PSSession -ComputerName 'SRV2KDC.microsspr.local' -Credential (Get-Credential)
Invoke-Command $session -Scriptblock { Import-Module ActiveDirectory }
Import-PSSession -Session $session -module ActiveDirectory

From an interface in WindowsForms in C # to then execute other powershell scripts, for example create-users.ps1:

$UserAccounts =  -Path C:\tmp\NewUsers.csvImport -CSV

Foreach ($user in $UserAccounts) {
 $FullName = $user.Name
 $GivenName =  $user.GivenName
 $SurName  = $user.sn
 $DisplayName = $user.Displayname
 $SamAccountName = $user.SamAccountName
 $Description  = $user.Description
 $Department  = $user.Department
 $Title  = $user.Title
 $PostalCode  = $user.PostalCode
 $Company  = $user.Company
 $PostOfficeBox = $user.PostOfficeBox
 $State = $user.st
 $City  = $user.l
 $email = $user.mail
 $TelephoneNumber = $user.TelephoneNumber
 $UPN = $user.UserPrincipalName

 $OU = "OU=Nuevos Usuarios,OU=MyOrganization,DC=microsspr,DC=local"

New-ADUser -Name "$FullName" -Enabled $True -AccountPassword (convertTo-SecureString $user.Password -AsPlainText -Force) -ChangePasswordAtLogon $True -GivenName "$GivenName" -Surname "$Surname" -DisplayName "$DisplayName" -SamAccountName "$SamAccountName" -Description "$Description" -Department "$Department" -Title "$Title" -PostalCode "$PostalCode" -POBox "$PostOfficeBox" -Company "$Company" -State "$State" -City "$City" -EmailAddress "$email" -OfficePhone "$TelephoneNumber" -UserPrincipalName "$UPN" -Path $OU
}

Thanks for the help.

    
asked by Shadowcast 11.05.2018 в 18:58
source

1 answer

0

The System.Management.Automation library, which is located in the Windows SDK, allows you to instantiate Powershell and execute commands in a very simple way. First install the SDK

Then reference to System.Management.Automation:

using System.Management.Automation

Prepare the command you need to execute, like this:

 using (PowerShell PowerShellInstance = PowerShell.Create())
    {
        // use "AddScript" to add the contents of a script file to the end of the execution pipeline.
        // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
        PowerShellInstance.AddScript("param($param1) $d = get-date; $s = 'test string value'; " +
                "$d; $s; $param1; get-service");

        // use "AddParameter" to add a single parameter to the last command/script on the pipeline.
        PowerShellInstance.AddParameter("param1", "parameter 1 value!");
    }

And finally invoke the call to execute the command:

PowerShellInstance.Invoke();

You can get the results and execution errors in this way:

     Collection<PSObject> PSOutput = PowerShellInstance.Invoke();

        // loop through each output object item
        foreach (PSObject outputItem in PSOutput)
        {
            // if null object was dumped to the pipeline during the script then a null
            // object may be present here. check for null to prevent potential NRE.
            if (outputItem != null)
            {
                //TODO: do something with the output item 
                // outputItem.BaseOBject
            }
        }
 if (PowerShellInstance.Streams.Error.Count > 0)
    {
        // error records were written to the error stream.
        // do something with the items found.
    }

I recommend this article where it explains , among other things, how to perform the task asynchronously.

    
answered by 11.05.2018 в 19:10