Pass boolean value

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…

Well,

The script will work if testing is set to true. However if it is set to false ,it will run the remove item command.
Yes, because thats how the script is written.

if ($testing -eq $true)
{
remove_test -whatif
# other stuff
}
else #this will execute if $Testing is not True
{
remove_test
#other stuff
}

And what does MyApp.ps1 do, is it only for setting $deployAppBool value ?
I would suggest you to have basic understanding of PowerShell. You can start from MVA.

and , I would request you to use the code formatting option in the forum which makes others easy to understand your code, below link will help you.

If you are working with commands that support WhatIf, then it will be inherited\passed to the commands that support WhatIf:

Function Set-SomethingDangerous {
    [CmdletBinding(SupportsShouldProcess=$True)]
    param(
        [Parameter(
			Mandatory = $true,
            HelpMessage = 'The environment where stuff will be deployed.'
        )]	
        [string]$Environment
    )
    begin {}
    process {
        #WhatIf will be passed
        Remove-Item C:\Scripts\file1.txt -Force

    }
    end {}
}

Set-SomethingDangerous -Environment PROD -WhatIf

Output:

What if: Performing the operation "Remove File" on target "C:\Scripts\file1.txt".

If you are using custom code that you don’t want to run, then you need to wrap that code in ShouldProcess:

Function Set-SomethingDangerous {
    [CmdletBinding(SupportsShouldProcess=$True)]
    param(
        [Parameter(
			Mandatory = $true,
            HelpMessage = 'The environment where stuff will be deployed.'
        )]	
        [string]$Environment
    )
    begin {}
    process {

        if ($PSCmdlet.ShouldProcess($Environment)) {

            "Doing dangerous stuff"
            
        }

    }
    end {}
}

Set-SomethingDangerous -Environment PROD -WhatIf

Output:

What if: Performing the operation "Set-SomethingDangerous" on target "PROD".

You can create your own custom switch to emulate WhatIf, but why would you want to make something customized rather than using built-in functionality. If it needs to be boolean, you can accomplish that with WhatIf.

Set-SomethingDangerous -Environment PROD -WhatIf:$false

or splatting:

$splat =@{
    Environment = 'DEV'
    WhatIf = $true
}

Set-SomethingDangerous @splat

If you want to do something custom, there is no mixing built-in and custom functionality. You need to wrap everything in your custom if or switch. There is also the switch parameter:

Function Set-Something {
    [CmdletBinding()]
    param(
        [Parameter(
			Mandatory = $true,
            HelpMessage = 'If passed then code will do something.'
        )]	
        [switch]$MySwitch
    )
    begin {}
    process {
        if ($PSBoundParameters.ContainsKey('MySwitch')) {
            "Doing stuff"
        }
        
    }
    end {}
}

Set-Something -MySwitch

If you pass it in the parameters it does something, like -Deploy. If it’s not passed to the function, it’s not called. Lastly, these examples are NOT boolean in Powershell:

.\myApp.ps1 -deployAppBool True .\myApp.ps1 -deployAppBool TRUE .\myApp.ps1 -deployAppBool true .\myApp.ps1 -deployAppBool "true" .\myApp.ps1 -deployAppBool false

It needs to be $true or $false, otherwise, you are just comparing strings and have to do .ToLower() to make sure you get matches. That is a really antiquated way of doing things. If you are making changes to a system, like installing an application, you should use WhatIf. If you are adding functionality or options, you should use a [switch].

Thanks for replying. I do already have a beyond general understanding of Powershell. I’m also aware of what the script does.I’m not sure how to put this ,but I’ll try as professionally and politely as I can put this - Please don’t provide links to powershell training’s.I don’t mind links for similar issues that relate to my issue like the following relates a little to mine (scripting - How to pass boolean values to a PowerShell script from a command prompt - Stack Overflow).

 

 

I’m no expert by any means and as I mentioned I’m continuously learning powershell,but I’m not looking for answers such as the ones you provided. I’m looking for did you try this or see this article ,etc .

 

I looked over Don Jones post on how to format last night ,but didn’t understand it at first. I looked over the post again and figured out the [PRE][/PRE]. Thanks for providing the same link I looked at last night.

 

Before I post anything ,I also do researchon what I’m trying to do (look up how a boolean value is handled,how to pass a boolean as a parameter,etc) ,then from there I perform tests in my test server.

 

The reason I posted on this forum ,was to be able to determine how to pass the parameter of false to my script ,to be able to ignore the default option of running the what if parameter.

The problem originated from when I ran my script that disables ad user accounts.

I like to use the whatif parameter ,since this will tell me what accounts are valid before hand.

This helps me verify what is going to be disabled .

 

When I want to actually run the script for real(disabling the ad accounts) without the whatif parameter ,I comment it out.This is cumbersome and there are better ways to run the script .

When run the .\scriptodisableuseraccounts the default option is to run with the whatif parameter.

 

I would like to run -.\scriptodisableuseraccounts -testing false .The result would be that it will run without the -whatif parameter and disable the accounts.

I thought adding the following : Param([switch]$testing) I could run the .\scriptodisableuseraccounts without the -testing and it would make it internally $False,if I were to run the script as an argument with -testing ,it would internally be $True.

I think of course I’m missing something.

 

Here is what I have tried ,but it’s still not working:

$testing = $true
#$testing = $false

function remove_test {

[CmdletBinding(SupportsShouldProcess)]


Param([switch]$testing)
    
         
remove-item -path C:\Users\User1\Documents\foo.txt



}
 






if ($testing -eq $true) 

{
remove_test -whatif 
# other stuff
}
else 
{
remove_test 
#other stuff
}

 

 

 

The purpose of the code below was to show this is what I’m looking for

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...

 

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"

} }

 

Function I would like to add my code to :

$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
}

 

Thank you so much Rob for the quick reply. I briefly looked over your suggestions.I think what you put should give me enough to test out.

I’ll let you know what I come up woith.

 

Hi Rob,

Sorry for delay on this. I was able to use your sample code and run it.

When I want to test what the command is doing ,I just run the Set-SomethingDangerous -Environment PROD -WhatIf:$true

When I want to actually disable accounts I run the Set-SomethingDangerous -Environment PROD -WhatIf:$false.

 

One thing I did learn for functions to work is that it actually needs to be called first and you can’t run the argument for the function like .\script name -WhatIf:$false.It might be possible ,but that part of what I was doing wrong.

 

This may be a silly question ,but does there need to be the following parameter ([string]$Environment) in order for this to work?

 

I know of course Environment was just an example you used ,but is it required to be used for what I’m doing ?

 

Also how can I make it so if someone were to run Set-SomethingDangerous -Environment PROD ,it would by default run the -whatif parameter ?

 

Here is my code for disabling an AD account. It worked fine,but if there any additional suggestions that you have ,I would like to hear them

 

Thank you so much.

Function Set-SomethingDangerous {
    [CmdletBinding(SupportsShouldProcess=$True)]
    param(
 [Parameter(
Mandatory = $true,
 HelpMessage = 'The environment where stuff will be deployed.'
        )]	
        [string]$Environment
    )
    begin {}
    process {
        #WhatIf will be passed
        import-module activedirectory

$list = Import-Csv 'C:\Users\User1\Documents\DisabledAccounts.csv'
$logfile = "C:\Users\C:\Users\User1\Documents\DisabledAccounts.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  

 add-content $logfile "$SAMAccountName ($User.DistinguishedName) disabled on $now"

} }


    }
    end {}
}

Set-SomethingDangerous -Environment PROD -WhatIf