I have this little line of PowerShell code (running it in VS Code) that goes out to get the services on a remote machine. Once it get’s that dump of services from the remote machine I’m trying to store that in a var so filtering can be done with the data that’s stored rather than make several calls to the same server for the same information. And everything works as it should except for the IF (below).
The problem is that the var isn’t storing the dump from the remote box like I would think it would. Every time I run the script, it invokes the command to get the data. Here’s what I have:
Essentially, if the $GetServices var is empty, go get the information. If it’s not empty, skip past it and do the rest of the script.
I’m not nulling the var at any point in the script, and I do the filtering to find the services that are set to automatic start later in the script and it works. But $GetServices isn’t retaining that data even in the same session.
Would anyone have an bit of knowledge they can drop on me that would explain why this is happening?
Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working). If you can’t see the </> in your toolbar, you will find it under the gear icon.
I thought I was formatting the code correctly. I was told that in a previous post and thought I corrected it. Obviously I misunderstood how that’s done. Thank you for the additional correction.
I will give these both a try. If CIM-Instance is the new thing, then I’ll use that instead.