r/PowerShell • • 4d ago • Popcorn 1

Script Sharing [ Removed by moderator ]

[removed] — view removed post

145 Upvotes

30 comments sorted by

52

u/BlackV 4d ago

Try it safely first: -DryRun

sigh we live in a society man, -whatif has existed for like 15 years in powershell

other than that anything removing adobe with extreme prejudice is a great plan

7

u/KMzoo 4d ago edited 4d ago

Fair point. Added -WhatIf as an alias for -DryRun, so both work now.

Edit: not an alias anymore. As of 1.3.0 it's implemented properly with SupportsShouldProcess, and every .NET and native call sits behind its own ShouldProcess() check. -WhatIf and -Confirm are the real ones now. -DryRun still works and means -WhatIf.

17

u/BlackV 4d ago edited 4d ago

do you actually support -whatif in your code?

-13

u/KMzoo 4d ago

Not via SupportsShouldProcess, no. It's an alias for the script's own dry-run switch as of now. Every destructive step checks that flag and prints what it would do instead.

I went that way on purpose. Most of the registry and file work goes straight through .NET (Microsoft.Win32.Registry, MoveFileEx) and native tools like regsvr32 and takeown, and those don't honor $WhatIfPreference. With ShouldProcess the cmdlets would skip but the .NET calls would still delete, which is worse than no -WhatIf at all. One explicit flag checked everywhere was the only way I could be sure a dry run really changes nothing.

Wrapping each of those calls in $PSCmdlet.ShouldProcess() would be the proper way. Happy to take pointers if you think it's worth it.

17

u/JSChronicles 4d ago

If you're not using the proper coding language for the given coding language, you shouldn't be writing code in that language.

Powershell uses whatif, you do not use aliases of any kind for that.

Are you going to start using plan and apply mode as well just like terraform?

As I have said before, if a person writes code and doesn't use the proper language for the given code then I don't even bother using their stuff because they can't code. Or it might mean you're not even following other best practices for the given language. Do you understand for powershell that you might need to put null on the left side and not the right side? And it's for specific reasoning?

Also, if you had the shouldprocess it would absolutely honor it because that's how powershell works.

All these script kiddie AI vibers making scrapped together garbage...

4

u/Alaknar 4d ago

All these script kiddie AI vibers making scrapped together garbage...

Not an AI script kiddie myself but I always disliked how -WhatIf works so I always did a -TestMode 1/0 param instead. I throw more output when it's on, and surround everything that's supposed to do something in an neat little if that says what would be happening outside of the test mode.

The -WhatIf output just feels lacking.

2

u/BlackV 4d ago edited 4d ago

I throw more output when it's on, and surround everything that's supposed to do something in an neat little if that says what would be happening outside of the test mode.

so are you meaning like

if ($pscmdlet.ShouldProcess("$($DestModule)", "Copy module folder from $($SourceModule)")) {
    ...your actual code...
    }

if ($PSCmdlet.ShouldProcess($mb.UserPrincipalName, "Set-Mailbox -AuditEnabled `$true")) {
    Set-Mailbox -Identity $mb.DistinguishedName -AuditEnabled $true
    }

type of thing

1

u/Alaknar 4d ago

I have shite memory, especially when returning to old scripts. So, I would do something like:

if ($testMode -eq 1) {
    Write-Output "`n`tTEST | Doing stuff"
    Write-Output "`tTEST | Run-Cmdlet -Something or -Other 1"
} else {
    Write-Output "Doing stuff"
    Run-Cmdlet -Something or -Other 1
}

It's easier for me to read later on than what -WhatIf generates.

3

u/BlackV 4d ago

Ah I see, deffo writing meaningful messages with the what if makes life easier

1

u/Alaknar 4d ago

Using the test param allows me to do much more than would be logical with WhatIf. I often aim the script at different targets if it's running in test mode, for instance.

As for meaningful messages - it's difficult because of the constraints WhatIf places on you, where the message must be: "performing operation X on target Y". Not to mention that the entire output is 100% uniform and I like to add tabs, newlines, tags, etc.

0

u/JSChronicles 4d ago

I laughed on this reply because you are right. All these other people that are unable to read code and purposely write against the language because they "can read it easier". The lack of ability or drive to learn properly is what stagnants so much of the developer space.

→ More replies

-1

u/JSChronicles 4d ago

Gross on your test mode..

-3

u/KMzoo 4d ago

Did it properly now. 1.3.0 uses SupportsShouldProcess, and every step that changes the system calls $PSCmdlet.ShouldProcess() first. -WhatIf and -Confirm are the real ones now, no alias.

SupportsShouldProcess alone does not make .NET calls or native executables honor -WhatIf. I tested it: in a function with SupportsShouldProcess run with -WhatIf, Remove-Item is skipped, but [IO.File]::Delete() and cmd /c del still delete the file. Only calls wrapped in ShouldProcess() by hand are skipped.

2

u/JSChronicles 4d ago

Why are you using non-powershell commands? That's your first problem...

3

u/ShutUpAndDoTheLift 4d ago

Dropping into .net for performance loops has been standard practice for.. forever. (I have no idea what op is doing or not doing)

4

u/TheITMan19 4d ago

Op probs doesn’t know themselves. Made a script when a tool already exists to do the tool’s job, that’s made by the developers of the product they’re trying to remove. Reinventing the wheel, but poorly no doubt.

3

u/lastrosade 3d ago

Holy Slop!

2

u/ElidoroAlznauer_58 4d ago

yeah reinventing SupportsShouldProcess is a classic at this point

19

u/gadget850 4d ago

22

u/OpinionReasonable247 4d ago • Bravo

We don’t look for tools now we just make them with Claude first. 

8

u/LookAtMeInMyEyeball 4d ago

Seriously, this has been a common utility for many years.

2

u/gadget850 4d ago

Yep. I added it to Bomgar as a script.

2

u/BlackV 4d ago

Bomgar, now that's a classic tool

1

u/gadget850 4d ago

Much better than whatever we used before, and I manage the scripts.

3

u/InterstellarReddit 4d ago

OP was just trying to get people to run a random power show on their computers, even though they can just have AI do it for them and create the power show, or and like you mentioned, use the Adobe cleanup tool, which is proven through the industry

It’s a typical case of AI allowing people to do things, but people not thinking about whether they’re contributing to society or not, whether the things are creating.

This case, OP literally solved the problem that’s already been solved by a higher credibility tool that is also free

-12

u/KMzoo 4d ago

Honestly didn't even try it, I had so many leftovers from a handful of different Adobe apps and their many different versions that I found throwing this together the easiest and fastest way to blast them off my PC.

12

u/BunnyTub 4d ago

So you made a tool without checking that said tool already exists?

11

u/smilaise 4d ago

I love it. Adobe actually makes a tool of their own that supposedly does this because they understand how dogshit their software is, but can we really trust them? lol.

2

u/Aygul12345 4d ago

Thank you for this. Could delete the %appdata% Adobe and autostart. Thanks for the Adobe nuke tooling.