Whatif prevents export-csv. Any way to bypass?

Welcome Forums General PowerShell Q&A Whatif prevents export-csv. Any way to bypass?

This topic contains 10 replies, has 6 voices, and was last updated by

 
Participant
3 weeks, 5 days ago.

  • Author
    Posts
  • #123836

    Participant
    Points: 17
    Rank: Member

    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?

  • #123843

    Participant
    Points: 105
    Helping Hand
    Rank: Participant

    Remove the -WhatIf parameter

    • #123849

      Participant
      Points: 17
      Rank: Member

      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.

  • #123851

    Participant
    Points: 809
    Helping Hand
    Rank: Major Contributor

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

  • #123908

    Participant
    Points: 306
    Helping Hand
    Rank: Contributor

    Ditto to what kvprasoon has said.

    Use a try catch  instead of the WhatIf, and handle the output directly.

  • #123980

    Participant
    Points: 265
    Helping Hand
    Rank: Contributor

    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
    • #123986

      Participant
      Points: 17
      Rank: Member

      That is exactly what I was looking for.

      Thanks

  • #124122
    js

    Participant
    Points: 302
    Helping Hand
    Rank: Contributor

    You can record -whatif messages with start-transcript, but it won't be csv.

  • #124220

    Participant
    Points: 105
    Helping Hand
    Rank: Participant

    That is exactly what I was looking for.

    Thanks

    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.

  • #124251

    Participant
    Points: 265
    Helping Hand
    Rank: Contributor

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

  • #124253

    Participant
    Points: 105
    Helping Hand
    Rank: Participant

    Interesting! I didnt know that, thanks for the explanation.

You must be logged in to reply to this topic.