Can I Use a Variable for -WhatIf in Set-ADUser?

0
0
Asked By CuriousCoder42 On

I've been trying to figure out how to use a variable with the -WhatIf switch for the Set-ADUser cmdlet. I know that I can run the command like this to test: `Set-ADuser $Actions -WhatIf` while I'm testing, but I'm curious if I can combine it with a variable approach. Specifically, could I write something like `Set-ADuser $Actions -WhatIf:$DoWork`, or would I need to do a conditional statement like `if($DoWork) {Set-ADuser $Actions} else {Set-ADuser $Actions -WhatIf}`? Any insights would be appreciated!

5 Answers

Answered By SystemScripter On

Another approach could be setting a variable for -WhatIf itself, like `Set-ADuser $Actions -WhatIf:$WhatIfPreference`. This way, you streamline it a bit depending on your preferences.

Answered By TechieTimmy On

If you're planning to track changes in AD, using something like `ShouldProcess` can be beneficial. Just remember that if you're relying on it, some sequential commands might not execute if you're in -WhatIf mode, since those changes won't apply until you're ready to go!

Answered By PowershellPro99 On

The -WhatIf switch is great for testing what would happen without making any actual changes. Just a heads up, it’s essential to remember that sometimes it might not function as expected, and you could still end up making changes, so use it carefully!

Answered By DebuggerDude On

It looks like you might want to implement a try/catch structure as well. This will allow your commands to attempt execution, catch any errors, and perform a final action regardless of success. However, from what you mentioned, if you're looking for an audit mode with -WhatIf, you're on the right track!

Answered By ScriptSavant88 On

You can definitely work with conditionals! Since $DoWork is a boolean, you could use something like `Set-ADUser $Actions -WhatIf:(-not $DoWork)`. This way, if $DoWork is true, it runs normally; if false, it shows what would have happened without actually applying changes.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.