This topic contains 3 replies, has 3 voices, and was last updated by
March 29, 2018 at 5:32 pm #97472ParticipantTopics: 16Replies: 29Points: 112Rank: Participant
I find I am using PowerShell very heavily at the moment, converting all our old scripts to PowerShell and I love the flexibility and power the language provides, however my scripts are not written in the cmdlet style, they are written to get the job in question done, much like a batch file or vbscript would be written, is this wrong? Should I be aiming to write everything in the cmdlet style?
Currently reading 'Learn Powershell Scripting in a Month of Lunches' and it got me thinking....
Thanks for any guidance you can provide.
March 29, 2018 at 5:51 pm #97479KeymasterTopics: 13Replies: 4872Points: 1,811Rank: Community Hero
Yes. You should.
Learn PowerShell Scripting in a Month of Lunches will be the way to start that :).
March 29, 2018 at 8:11 pm #97484ParticipantTopics: 2Replies: 428Points: 835Rank: Major Contributor
I would first start trying to generalise and compartmentalise portions of your scripts. There are often things we do that can be reused elsewhere with small modifications. Start there, put that code into its own function, and reuse that instead of coding the same thing multiple times.
Then, look at it, and ask yourself: "Can I generalise this further to make it useful in more situations by adding a few parameters that can be modified later?"
Then just get heavy into validating your parameters, and if you want you can look at dynamic parameters (though frankly I avoid those where possible and haven't found many good use cases for them as yet). At that point, you're pretty close to what you'd call a cmdlet.
Check out Get-Help about_Advanced_Parameters and see what you can do with them.
After that, it's a matter of looking critically about what output your functions have, and how you can make it more useful.
At that point, you should start considering turning groups of your functions into a module. Some of KevMar's blog posts are quite good on that topic. 🙂
March 29, 2018 at 10:58 pm #97506ParticipantTopics: 16Replies: 29Points: 112Rank: Participant
Thanks very much for the responses – I guess I need to start looking at the scripts I am writing and turning them into something re-usable – like you say, first put them into functions and then move on from there instead of literally just writing a script as a would a batch file or something.
The topic ‘PowerShell - am I doing it wrong?’ is closed to new replies.