I am a complete Powershell newbie. But I am trying more and more to learn this.
I have a question regarding a task that I would like to solve but have no idea how to proceed and need support here.
I want to create a powershell script which I need to give an input (a number). And based on this number, I want to output all divisors of it (this from the smallest to the largest number). This works very well with a 12, but of course not with a 17. The input should always be greater than 1.
If it is a number like 17, it should then output the number and its remainder.
Additionally I should have the possibility to decide if I can start again from the beginning with a new number or end the script.
I really have no idea how to set this up and would appreciate any helpful comments.
If this is an internship assignment or homework question we try avoid assisting with those as the first port of call should be your tutor, supervisor or teacher.
In any other case - as we do not like to deliver ready to use code or solutions on request - you may start reading the help you can find in the internet.
You should always read the complete help imcluding the examples to learn how to use the commands.
If you have started trying and just got stuck with your code you should share it here (formatted as code How to format code on PowerShell.org forum ) and we will try to solve your issues together with you.
First of all, thank you very much for your answer and the time!
I have a code that now does exactly what I want. But I want to be able to specify the int as input, which then passes this to the function. Unfortunately I only got as far as here
You may cast your function variable to the type [int] like this:
function show-divisor ([int]$n) {
But your logic seems faulty anyway. When you provide 8 as input you get 1 and 2. … it misses the 4!
The code I suggested is just the function declaration. When you want to run it when you call the script you have to put a function call (Show-Divisor) at the end of the script.
I thank you incredibly much for your explanation and time. I have now learned what I need to pay attention to. And you are right! In my code it doesn’t output the 4… Must find out why. I tested your code and specified $(Show-Divisor). This works :)! But here the number 1 is always missing. I will have a closer look.
Your design is top :)! I have still adjusted the number and now it also gives me the 1 with. Now I want to try to write this so that I can say “Enter a number” and then comes the output. After that, the option should come whether you want to specify another number.
My code now looks like this. It still looks a bit nasty with the repeat option (I want this as output from the script in the shell and not as a GUI option.) But it does its job :).
I would have expected that to be impossible but anyway - if you don’t like that you still could change your logic to the approach I recommended in my last answer.