Link to home
Start Free TrialLog in
Avatar of Eric Greene
Eric GreeneFlag for United States of America

asked on

Call Powershell Script (.ps1)--that requires parameters--from C#

I have a Powershell script that uses a parameter which is basically a string parameter for varying purposes.  I want to call the script from C# and supply the parameter.  All of the examples I have found have me either inhaling the contents of the file as text and adding parameters to specific commands, or inhaling the contents and not discussing parameter requirements for the script.

How do I execute the script and supply the user-defined parameter of the script?  Here is a how the script file is setup:
param(
    [Parameter(Position=0,Mandatory=$true)][String]$FileName
)

$ADCreds = Get-Credential

Import-Module ActiveDirectory

Import-Csv $FileName | ForEach-Object {
    
    If ($_.status -ne 'Cleared') {
        foreach($user in Get-ADUser -filter '*' -Credential $ADCreds -SearchBase 'ou=Users,DC=MYCOMPANY,DC=LAN' -Properties * | Select-Object givenName,sn,sAMAccountName) {
            Set-ADObject -Identity $user.sAMAccountName -Remove @{upnsuffixes="Cleared.MyCompany.LAN"} -Credential $creds
        }
    }
    else {
        foreach($user in Get-ADUser -filter '*' -Credential $ADCreds -SearchBase 'ou=Users,DC=MYCOMPANY,DC=LAN' -Properties * | Select-Object givenName,sn,sAMAccountName) {
            Set-ADUser -Identity $user.sAMAccountName  -UserPrincipalName ($user.sAMAccountName + "@Cleared.MyCompany.LAN") -Credential $creds
        }
    }
}

Open in new window

I already know that I need to do something about the ADCreds, my specific topic is how to call this file from C# and supply the $FileName parameter.
ASKER CERTIFIED SOLUTION
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eric Greene

ASKER

I am sorry for the delay in response. Unfortunately my question came late Thursday and I have been away from my office since. I am attempting to implement your idea, Dustin, and will report back today with my results.
This worked great! Thank you Dustin.