Hello,
I’m looking to be able to pass a boolean value of true or false to be able to run the whatif function for disabling ad accounts.
I would like to run the following as a parameter as so .\whatif_function -Testing False.
I was able to get some help from another forum,but I’m only getting so far though.
The script will work if testing is set to true. However if it is set to false ,it will run the remove item command.
The end goal is to run my disable aduser account ant to be able to change $testing to $false so it will run. I want by default for whatif to run to verify disabling the accounts.
I know I probably shouldn’t apologize ,but I’m somewhat new to Powershell and I’m continuously learning and apologize if I forgot something.
Below is the code I’m testing that I’m going to add to my original code.I added my original disable-aduser account code along with code that I tried to use to add the parameter for adding true or false,but didn’t work.
$testing = $true
$testing = $false
function remove_test {
[CmdletBinding(SupportsShouldProcess)]
Param()
remove-item -path c:\testfolder\test.txt
}
if ($testing -eq $true)
{
remove_test -whatif
other stuff
}
else
{
remove_test
#other stuff
}
Original code for disabling user accounts:
import-module activedirectory
$list = Import-CSV “D:\Scripts\ADaccounts\Adaccounts.csv”
$logfile = “D:\Scripts\Output\ADaccounts.log”
forEach ($item in $list) {
$SAMAccountName = $item.SAMAccountName
$now = Get-Date -format “dd-MMM-yyyy HH:mm:ss”
$User = $Null
$User = (Get-ADUser -Identity $SAMAccountName)
If ($User -eq $Null) { add-content $logfile “$samAccountName Not Found!” }
Else {
Disable-ADAccount -Identity $SAMAccountName -whatif
add-content $logfile “$SAMAccountName ($User.DistinguishedName) disabled on $now”
} }
This is the code I tried to use to make the True/ False mandatory ,but it didn’t work :
Param(
[Parameter(Mandatory=$false)][ValidateSet(“true”, “false”)][string]$deployApp=“false”
)
$deployAppBool = $false
switch($deployPmCmParse.ToLower()) {
“true” { $deployAppBool = $true }
default { $deployAppBool = $false }
}
So now you can use it like this:
.\myApp.ps1 -deployAppBool True
.\myApp.ps1 -deployAppBool TRUE
.\myApp.ps1 -deployAppBool true
.\myApp.ps1 -deployAppBool “true”
.\myApp.ps1 -deployAppBool false
#and etc…