Eric Greene
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:
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
}
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This worked great! Thank you Dustin.
ASKER