I’ve been working on a script that will be used to update Microsoft’s Active Directory using the set-aduser command. This script creates a log file by using the export-csv command to write a csv file with all of the differences between the existing and new data. However, when attempting to use the whatif parameter, it doesn’t create the csv file. Is there anyway around this?
Remove the -WhatIf parameter
My goal with the whatif parameter is to allow me to run it against all of the users without updating anything, but still receive the csv file of what will change. This way I can validate the changes before committing.
-WhatIf is not supposed to be used in scripts, its for interactive use case for the end user to understand What will happen if he/she executes it.
Just to know,
run it against all of the users without updating anything
What will you do with this ?
Ditto to what kvprasoon has said.
Use a try catch instead of the WhatIf, and handle the output directly.
If you want to bypass a specified -WhatIf parameter applied to a script or function, you can do so. I would caution you to do so only very sparingly.
Import-Csv -Path $CsvFile -WhatIf:$false
That is exactly what I was looking for.
You can record -whatif messages with start-transcript, but it won’t be csv.
[quote quote=123986]That is exactly what I was looking for.
What is the difference between omitting -WhatIf and declaring it $false? Is there something I am missing here? It seems like 6 in one, half a dozen in the other.
so let’s say you create a function that calls Import-Csv and itself implements ShouldProcess.
Someone can call your function, and supply -WhatIf. This value will also be passed along to any cmdlets or functions that support ShouldProcess, such as Import-Csv. In this case, OP wants the -WhatIf to apply to their function, but not to Import-CSV because that provides the input, and for their function to be useful (and for the WhatIf output to be meaningful), it needs input to read.
Explicitly specifying the -WhatIf value in that manner prevents the caller of the function from affecting the function you don’t want them to. But as mentioned, it’s probably a bad idea to do too often.
I would generally advise you instead take in the input as parameters and call Import-Csv to import it before calling the function, but… there are always exceptions.
Interesting! I didnt know that, thanks for the explanation.