Search code examples
powershellimport-csv

Read CSV in PowerShell and Map Drives


I'm Looking to read from a .CSV and then use the data to map a drive. The file looks like this:

Location, UNC1, UNC2      
USA, \\Server1\Data, \\Server1\Shared
EMEA, \\Server2\Data, \\Server2\Shared

Tricky part is I wanna read the data from a CSV and then present in a list box (guess I have to summon VBS?) but only show the region in the box. Then when I choose the region, it maps the 2 UNC's to the local machine.

I've read a few posts and will need Import-CSV - just looking for a jumping off point.

Any suggestions?


Solution

  • Normally we ask that you try to do something yourself, then we are here to help troubleshoot along the way. Next time you ask a question please make an attempt to code it yourself then come asking for help when you get stuck.

    I just did this for fun real quick because it was pretty easy, enjoy.

    function Map-Drive(){
        if($ComboBox1.text -eq "USA"){
            New-PSDrive -Name K -PSProvider FileSystem -Root $csv.UNC1[0] -Persist -Scope Global
            New-PSDrive -Name L -PSProvider FileSystem -Root $csv.UNC2[0] -Persist -Scope Global
        }
        if($ComboBox1.text -eq "EMEA"){
            New-PSDrive -Name M -PSProvider FileSystem -Root $csv.UNC1[1] -Persist -Scope Global
            New-PSDrive -Name N -PSProvider FileSystem -Root $csv.UNC2[1] -Persist -Scope Global
        }
    }
    
    $csv = import-csv "\\server\folder\test.csv" -Delimiter ","
    
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Application]::EnableVisualStyles()
    
    $Form                            = New-Object system.Windows.Forms.Form
    $Form.ClientSize                 = '319,63'
    $Form.text                       = "Regional Drive Map"
    $Form.TopMost                    = $false
    
    $ComboBox1                       = New-Object system.Windows.Forms.ComboBox
    $ComboBox1.text                  = "comboBox"
    $ComboBox1.BackColor             = "#d7ebff"
    $ComboBox1.width                 = 143
    $ComboBox1.height                = 54
    $ComboBox1.location              = New-Object System.Drawing.Point(20,18)
    $ComboBox1.Font                  = 'OCR A,12,style=Bold'
    
    $Button1                         = New-Object system.Windows.Forms.Button
    $Button1.BackColor               = "#eaeaea"
    $Button1.text                    = "Map Drive"
    $Button1.width                   = 107
    $Button1.height                  = 30
    $Button1.location                = New-Object System.Drawing.Point(174,14)
    $Button1.Font                    = 'OCR A,10'
    
    $ComboBox1.DataSource = [system.Collections.ArrayList]$csv
    $ComboBox1.DisplayMember = 'Location'
    
    $Button1.Add_Click({Map-Drive})
    
    $Form.controls.AddRange(@($ComboBox1,$Button1))
    #Finally Show our GUI
    $Form.ShowDialog()