Create sessions spreadsheet or delimited file?

Create sessions spreadsheet or delimited file?

avatar

Is there a way to create seessions based off of a excel spreadsheet or a delimited file. I support multiple locations and rather then manually creating a connection for each site I would like to supply a list of IPs and have a connection created for each one. Any help would be appreciated.

All Comments (2)

avatar

You can use the (File -> Import -> Import Session csv wizard) for simple csv imports. If it's not a simple csv, PowerShell is the way to go.

Stéfane Lavergne

avatar
debell6701
Disabled

I hope this does complicate things even more but if you want a function that will import an excel document directly this is one I found about a year a go. In other words I didn't write this but have found it very useful.

function Import-Excel

{

param (

[string]$FileName,

[string]$WorksheetName,

[bool]$DisplayProgress = $true

)

if ($FileName -eq "") {

throw "Please provide path to the Excel file"

Exit

}

if (-not (Test-Path $FileName)) {

throw "Path '$FileName' does not exist."

exit

}

$FileName = Resolve-Path $FileName

$excel = New-Object -com "Excel.Application"

$excel.Visible = $false

$workbook = $excel.workbooks.open($FileName)

if (-not $WorksheetName) {

Write-Warning "Defaulting to the first worksheet in workbook."

$sheet = $workbook.ActiveSheet

} else {

$sheet = $workbook.Sheets.Item($WorksheetName)

}

if (-not $sheet)

{

throw "Unable to open worksheet $WorksheetName"

exit

}

$sheetName = $sheet.Name

$columns = $sheet.UsedRange.Columns.Count

$lines = $sheet.UsedRange.Rows.Count

Write-Warning "Worksheet $sheetName contains $columns columns and $lines lines of data"

$fields = @()

for ($column = 1; $column -le $columns; $column ++) {

$fieldName = $sheet.Cells.Item.Invoke(1, $column).Value2

if ($fieldName -eq $null) {

$fieldName = "Column" + $column.ToString()

}

$fields += $fieldName

}

$line = 2

for ($line = 2; $line -le $lines; $line ++) {

$values = New-Object object[] $columns

for ($column = 1; $column -le $columns; $column++) {

$values[$column - 1] = $sheet.Cells.Item.Invoke($line, $column).Value2

}

$row = New-Object psobject

$fields | foreach-object -begin {$i = 0} -process {

$row | Add-Member -MemberType noteproperty -Name $fields[$i] -Value $values[$i]; $i++

}

$row

$percents = [math]::round((($line/$lines) * 100), 0)

if ($DisplayProgress) {

Write-Progress -Activity:"Importing from Excel file $FileName" -Status:"Imported $line of total $lines lines ($percents%)" -PercentComplete:$percents

}

}

$workbook.Close()

$excel.Quit()

}