Search code examples
powershellnetwork-programmingautomation

Getting user input to match items in an array


I am very new to PowerShell and trying to work through something.

The logic I am working through seems straight-forward;

  1. user selects name to store output to the location on the machine
  2. available devices to choose from device list (array)
  3. select the site identifier - if the site identifier matches the network devices ($switches) output the desired.
  4. when site is selected it will output to a text file with the configuration script for that site. (site may contain multiple network devices so will need to output multiple config snippets)

$file = read-host "Please enter the file name to be saved. Location of file: C:\Temp\"
$user = read-host "Please enter the site identifier (ex: AB001)"
$switches = @("AB001-SW1", "AB001-SW2", "CD002-SW1," "CD002-SW2"....) <-more sites then this


if ($user (SOMETHING TO KEY OFF OF THE SITE NAME) *exist in or something like that* $switches)  
{
Write-Output "This site exists"
}
else {
Write-Output "This site doesn't exist"
}

*FOR LOOP?*

 $cp1 = 'set system radius-server x.x.x.x source-address' 
 $cp1a = 'set system radius-server x.x.x.x secret xyz'
 $cp1b = 'set system radius-server x.x.x.x timeout 5'                                          
 $cp1c = 'set system radius-server x.x.x.x retry 3'
 $cp1d = 'set system radius-server x.x.x.x port 1812'

 $output1 = -join @($cp1; *$OUTPUTVARIABLE*)
 $output1a = $cp1a
 $output1b = $cp1b
 $output1c = $cp1c
 $output1d = $cp1d
 $endoutput1 = $output1, $output1a, $output1b, $output1c, $output1d
 $endoutput1 | Out-File -FilePath C:\Script-Out\$file.txt -Append

 write-host -ForegroundColor green "You can find your file under C:\TEMP\$file.txt"
 Read-Host "Please press enter to exit!!"

Any suggestions or feedback would be spectacular! Thanks.


Solution

  • As i understand, what you want to do.

    $Sites = 'AB001', 'AB002' # etc.
    $Switches = 'AB001-SW1', 'AB001-SW2', 'CD002-SW1', 'CD002-SW2'
    
    do {
        $file = read-host "Please enter the file name to be saved. Location of file: C:\Temp\"
        $file = "C:\Temp\$file"
    
        write-host 'Device names:'
        write-host "$($Switches -join ', ')"
        $SelectedDevice = read-host "Please enter the device name"
        $DeviceSite = $SelectedDevice.split('-') | select-object -first 1
    
        write-host 'Site names:'
        write-host "$($Sites -join ', ')"
        $SelectedSite = read-host "Please enter the site name"
    
        if ( $DeviceSite -eq $SelectedSite ){
            $cp1 = 'set system radius-server x.x.x.x source-address' 
            $cp1a = 'set system radius-server x.x.x.x secret xyz'
            $cp1b = 'set system radius-server x.x.x.x timeout 5'                                          
            $cp1c = 'set system radius-server x.x.x.x retry 3'
            $cp1d = 'set system radius-server x.x.x.x port 1812'
    
            $output1 = @( $cp1, $SelectedDevice ) -join ' '
            $output1a = $cp1a
            $output1b = $cp1b
            $output1c = $cp1c
            $output1d = $cp1d
            $endoutput1 = @( $output1, $output1a, $output1b, $output1c,  $output1d ) -join "`n"
    
            $endoutput1 | Out-File -FilePath $file -Append
    
            write-host "You can find your file under $file" -ForegroundColor green
        }
        
        $Answer = read-host "Do you want to continue(Y/N)?"
    } until ( $Answer -ne "Y")