Minimizing code reuse. Noob for sure.

Hi all, please delete if I’m not following the rules. New to scripting, trying to figure out the correct words to use, etc. Proficient in 1 liners and piping stuff. I can get this done and do what I want, but I’m afraid there’s a TON of copying/pasting and re-use. I figure if I want to learn this stuff, I need to figure out how to minimize things like this.

You can see in the example what I’m trying to do…essentially it’s a prompt, where you’ll have choices based on the amount of hosts you indicate. Ignore the elseif/if confusion, just playing around and grabbed a snippet.

You can see, if you enter 1, you’re prompted for 1 details. If you enter 2, you enter 1 AND 2 details. By the time I get to 64 this will be rather large 🙂 Any help or pointers would be greatly appreciated!

What do I need to research/learn that will simplify this process?

 if($HostCount -eq '1') { $host1 = Read-host -Prompt 'Enter your FIRST Hostname' $host1wwpn = Read-host -Prompt 'Enter your FIRST WWPN' } elseif($HostCount -eq '2') { $host1 = Read-host -Prompt 'Enter your FIRST Hostname' $host1wwpn = Read-host -Prompt 'Enter your FIRST WWPN' $host2 = Read-host -Prompt 'Enter your SECOND Hostname' $host2wwpn = Read-host -Prompt 'Enter your SECOND WWPN' } if($HostCount -eq '3') { $host1 = Read-host -Prompt 'Enter your FIRST Hostname' $host1wwpn = Read-host -Prompt 'Enter your FIRST WWPN' $host2 = Read-host -Prompt 'Enter your SECOND Hostname' $host2wwpn = Read-host -Prompt 'Enter your SECOND WWPN' $host3 = Read-host -Prompt 'Enter your THIRD Hostname' $host3wwpn = Read-host -Prompt 'Enter your THIRD WWPN' } 

submitted by /u/Donald_trump_shit
[link] [comments]

Leave a Reply