Maintenance Code Improvements

Hey everyone,

Hope this is the right place to post this as I’ve just joined the forum.

I’ve been out of work since the both of my twins pretty much and I’m now looking for new opportunities, I used to work within IT support but wanted to move into a more Dev role. So I needed to start learning how to code, which is why i decided to start learning PowerShell.

I’m working on a cross-platform maintenance script and wanted to share with everyone what I’m currently working on, in the hopes that it improves everyone’s working day and provides me with a better understanding of PowerShell. This started out as a basic clear temp files script and went from there.

The script is designed to run automatically and collect any errors.
It does perform various tasks, not just Maintenance tasks but overall the outcome is to keep the system at peak performance, while flagging potential issues to help desk.

I was wondering if anyone can suggest any performance improvements or additional tasks?
I already have a list of improvements I’ve made myself which I’m currently working on but would love to hear others opinions whether good or bad.

Each version of my code is pretty much different as i try different formats and commands.
I created a module for PSGallery to easily execute my script, but it currently hosts a old version of my script 0.1.1
I’ve also started a GitHub Respository which holds the latest version I’m working on 0.1.2
Hoping to release a final product soon, but I’m currently a weekend scripter.

This script isn’t complete, i know it isn’t perfect, it will obviously need its variables updated to work within your environment and I’m open to scrutiny.

Thanks guys.

Christopher,
Welcome to the forum. :wave:t3:

I have a strong doubt about this. :smirk:

You could have taken some time to look around and get a feeling what this forum is about and how it works. :man_shrugging:t3: :wink:

This forum is more about helping with specific issues you have with a particular piece of code you wrote yourself and less about complete scripts or modules. There are other forums dedicated to give reviews like

It will be hard to recommend something meaningful since you shared 2 different modules with different versions and you didn’t even share them directly. We have to go to another site and get it from there. :smirk:

The next thing is … I tend to think that asking for opinions in internet forums is always a really bad idea. Quite often this leads to discussions about irrelevant details.

But since you asked for I will try to give some general tips regarding your code or PowerShell in general.

First of all - your code is very badly formatted. That makes it hard to read and to understand. You may read the …

… before you continue to code on this projects.

You have way to many comments. And there is still a lot of code commented out. Please keep in mind that you ask people to help you for free in their spare time. You should make this task as convenient and easy as possible them. PowerShell code is quite self-commenting. If you know the basics you know what …

Clear-RecycleBin -Force

… does. Even if you’ve never seen this cmdlet before. So no need to add a comment like this:

#Empty Recycle Bin

Speaking of making things easy - Since PowerShell is around for a while already you are most likely not the very first one with a given taks or idea. It is not a shame to make good use of the work others have already done and shared with the world. I’d recommend to search for examples of code for the task you want to acconplish and use what you like. No need to re-invent the wheel again and again. :wink:

Your script/module is quite specific. If someone wants to use it and only needs some of the functions you’ve built in the script/module needs to be customized what costs a lot of
effort. The idea of having a module is to wrap up some functions belonging together to make them easier to use. You actually created a script for one specific purpose on one specific environment.

You may start over with lerning about how to create a good function …

Please always read the help topics completely including the examples and you may follow the links in the section “See also”

You should not use Get-WmiObject anymore as it is deprecated and not cross plattform.

Really often in your code you create variables using a cmdlet and piping it to select opbject … like this for example:

$WinSysLocale = (get-WinSystemLocale | Select-Object Name)

This way you limit yourself to this one particular property of the returned object without gaining any advantage. The better way would be to assign the complete object to the variable and use the so called dot notation later on to access the needed individual properties … like this:

$WinSysLocale = Get-WinSystemLocale 
$WinSysLocale.Name

If you know you really need the system locale name only once you still could use

(Get-WinSystemLocale).Name

without creating a variable at all.

You have a lot of “Single Line Functions” you use only once. This way you write at least 4 lines of code to run only one - why? :man_shrugging:t3: … here’s an example:
You create the function RunGPUpdate

function RunGPUpdate {
GPUpdate /Force
}

and then you run this only once.

# Group Policy Update
RunGPUpdate

… and you even added a comment …

Function names in PowerShell should have the same naming convention like cmdlets - verb, dash, noun. And you should only use approved verbs. You can get a list of approved verbs like this:

Get-Verb | Sort-Object -Property Verb

Using this best practice you could name the function Invoke-GPUpdate.

I’d recommend to only pay special attention to performance if performance really matters for the task you’re about to accomplish. And if it matters you should measure it ( Measure-Command).

OK … I think that’s enough for now. If you have further specific questions you’re very welcome to come back and ask them. :wink: :love_you_gesture:t3:

2 Likes

Thank you so much for your insight, its really appreciated.

I know what your saying about asking opinions but i take heed of what you said.

I can see i have a long way to go and will endeavour to correct my mistakes.

I will indeed look at the references you’ve provided before continuing.

I’m self taught, so valuable insight like this means more than you’ll know.

Wish i could buy you a coffee to say thank you!

You indicated you were interested in moving into more development. Have you thought about and researched what that might mean/look like? What type of development you are wanting to do, or are interested in?

The answer to that will drive which language you should focus on learning. Just tossing that out since you indicated an interest in more of a “dev role”.

1 Like