It depends pretty much on how you want to do it. You can use an input file and import it with Get-Content for example. Or you simply have it in your script.
Cool. Please show it to the world!
We all started once. But it is beyond the scope of a forum like this to teach you basic PowerShell. You should always read the help for the cmdlets you’re about to use completely including the examples to learn how to use them. Then - if you get stuck - it’s best to ask a specific question about a particular piece of code you wrote. And for that you should share this piece of code.
When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Regardless of all that - in the vast majority of the cases you’re not the very first one with a given task. So it is pretty likely that you can find some helpful examples you can adapt to your particlar requirements. Please do at least your basic research before you come to forum and ask for help.
I’m not sure the reason for the % just before the Invoke command, the rest I understand.
That command could get very long with exclusions and I would like to get clever in the future to exclude specific services on specific servers. But that’s for another day…
I see two options, import an array from text file or use get-content…replace the -notlike entries with either of those two…assume an array would be quicker as it’s in memory and not reading a file every time.
I’ll have a little play to see what I can come up with.
I’ve been batch scripting for many years and fine with that, but I need to move away and onto PS, but my old brain keeps saying “How would I do this in batch script” lol
Thanks again for your response, no doubt be back soon with even less hair than I already have.
You should format your code nicely to make it easier to read and therefore easier to maintain. AND … since Invoke-Command can take an array of computernames for its parameter -ScriptBlock wich alias is -command you don’t actually need a loop.
Please always read the help for the cmdlets you’re about to use completely including the examples to learn how to use them
The % is an alias for the cmdlet Foreach-Object and it is common sense not to use aliasses in scripts. They are fine on the console but they do make your code harder to read. The same is valid for where instead of Where-Object or select instead of Select-Object … and with using tab completion it does not even requires a lot of typing.
You can list all existing aliasses on you system with
I’d say it depends on the amount of exceptions you want to add and on the way you’re providing them. If you use the correct names for them you could use an array and the operator -contains or ‘-in’ or actually -notcontains and -notin to compare the services found against the list you provide.
Here you can read more about:
This is understandable but often misleading I think. I’d like to encourage you to think outside of the box and in case search for inspiration online before you implement a solution you a kind of translated from batch to PowerShell. In the vast majority of the cases you’re not the first one with a given task and you can find a lot of examples for almost every existing problem online.
Here’s how I would tweak your code to run faster and look better: