Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Hi,
I am trying to write a script that check each server to see if it was rebooted - function call / command. if the server was rebooted
Then: store the server name in some place
Else: wait some time and check again - timeout if it exceeds certain amount of time
I am using FOR loop but I am thinking this is not the powershell way can you look at my script below and let me know what I am doing wrong?
Thanks but I am trying to continuously loop through checking for a certain allotted time. What you gave was a one time check. I am doing this because I am waiting for a reboot to happen for a x amount of machines before I move on to next part of the script. That why with every check I am building another array. I noticed that PowerShell does deal with arrays well so I changed the script around by making it more of a PowerShell script but still had to call C#. But for some reason I still cant get the List Array to be built. I will look a the documentation given as well.
The example was to set you in the right direction. You will need to learn the basics to understand the example and then see how to implement your plan.
I can also suggest searching for scripts and modules that are proven and that will do what you need. There are dozens available that do what you ask in more standard, flexible and useful ways. Instead of reinventing the wheel start by using what already has been invented. Experience will help you to learn how to use coding and technology together to get your tasks completed.
You could also just ask a specific question about your script such as posting any errors or telling us what doesn't work. We can answer questions that are of a specific nature.
When it loops if the computer rebooted it is suppose to add to the new array and remove from the old array and output but it is not doing it. I am debugging but I cant seem to figure out why it is not outputting to these lines.
Why are you using arrays? This is why I ask you to read teh book link and learn the basics of PowerShell. WHat you are trying to do just doesn't make any sense to me.
You have failed to state what you are trying to accomplish and the code only makes discovering this more difficult.
To design a solution it is necessary to first state in non-technical language what the goal of the script is.
Example: "I want to reboot all servers every 12 hours."
Once you have the simplest statement of intent then you can start writing the code.
Ok in the beginning of this this post I stated what I wanted to do asking what is the best PowerShell way to do it. If we ignore the array part all that I am asking is.
How can can I get a list of servers that I rebooted if the reboot did not happen how can continually check until it does? Then get a list back of all the servers that completed.
Your beginning does not state anything clearly it is just some comments about what you have tried and then some code. You have to be able to say what you want without code or any statement of method. Just think of how to state what you need to accomplish in simple terms.
This is why I recommend that you first learn basic PowerShell. It will help you understand how to start and will give you the tools and understanding you need to code with PowerShell.
Ok thanks for your help. I figured this out. I am using two separate arrays for failure and success. When I have these two separate arrays of servers I want to use the data. I think something got lost in my explanation. All good once again thanks.
This topic is 3 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.