0

I am developing a script which will be used to disable people's AD accounts in PowerShell as well as perform a lot of other functions.

I wanted the script to give the option of bulk mode or single mode. For example, as soon as you run the script, a parameter is enforced that asks if you want to run it in single mode or bulk mode. If bulk mode is selected, then it should load the function BULKmode which has already been defined and imports a csv.

If Single mode is selected, then it runs in single mode and selects the function Singlemode.

However the problem with PowerShell is that both parameters and functions go at the top of your script. I have tried both orders and the error I receive when I put the functions before the parameter is that there is

no such term as param

When i put the parameter first then it loads the parameter but then says

no such function

Below is a snippet

Param(
[Parameter (Mandatory=$true)]
[String]$PressYforBulkMode)

If ($PressYforBulkMode -eq "Y") {
Bulkmode
}

Else {
Singlemode
}
2
  • Hope this makes sense, please advise if you need more information Commented Jul 21, 2017 at 15:47
  • This sounds like something that might be better implemented with two parameter sets. One parameter set takes an array (probably) of SamAccountName, among other things. The other parameter set takes a single filename (probably), among other things. Commented Jul 21, 2017 at 18:34

2 Answers 2

1

The best solution is to break apart your functions so that you can just use a loop to use the function that does the actual work.

The way to do this would be to pass your information into the main function as an array. If you have one entry or many entries it won't matter. Then use foreach to loop through the array and call the function that actually disables the AD account and any other actions required.

This would mean you wouldn't have to ever check for 'bulk' or 'single' mode. It would just work.

Example:

function MainFunction
{
param (pass in array here)

    foreach(entry in $myArray)
    {
        DoWork $entry
    }
}

function DoWork
{
    //do things here
}
Sign up to request clarification or add additional context in comments.

Comments

0

It seems you want to prompt for user input. I prefer using Parameters but if you want to prompt for input try this:

Function BulkMode
{
    "Running BulkMode"
}

Function SingleMode
{
    "Running SingleMode"
}


$Input = Read-Host "Press Y for BulkMode"

if ($Input -eq "Y")
{
    Bulkmode
}

else
{
    SingleMode
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.