Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

Get data from excel using OLEDB connection

$
0
0

Hi,

I have a script that gets a variable from a Web page, and then search for the content of the variable in an excel file, and displays the data contained in the next column.

Unfortunately, it used to throws this error: 

[ : Cannot index into a null array.

At C:\Users\ccormeni\Documents\scripts\final.ps1:17400 char:20

+ $assgrp = $test[0][ <<<< 1]

+ CategoryInfo : InvalidOperation: (1:Int32) [], RuntimeException

+ FullyQualifiedErrorId : NullArray

at this line:

 

$assgrp = $test[0][1]
When the file was improperly closed. I close it this way, but sometimes it stayed open for some reason:

 

$conn.close()
Now it ALWAYS throws the error. The code for the functions is this:

 

Function open-excel(){Param($excel="N:\ISC_GE\REFERENCE\OPERATIONAL\2_PROCESS_DOCUMENTATION\GE-EUS2-Service - macro - Copy.xlsx")Set-VariableConnString("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$excel;Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2")-ScopeGlobalset-variable Conn(new-objectSystem.Data.OleDb.OleDbConnection($connString))-scope Global
$conn.open()set-variable Tables($Conn.GetOleDbSchemaTable([System.Data.OleDb.OleDbSchemaGuid]::tables,$null))-ScopeGlobalset-variable cmd (new-objectSystem.Data.OleDb.OleDbCommand("Select * from [$($tables.rows[0].TABLE_NAME)]",$Conn))-ScopeGlobalset-variable da (new-objectSystem.Data.OleDb.OleDbDataAdapter($cmd))-ScopeGlobalset-variable ds (new-objectSystem.Data.dataset  )-ScopeGlobal
$da.fill($ds)set-variable test ($ds.tables[0].select("Location = '$loc'"))-Scope scriptSet-Variable assgrp ($test[0][1])-Scope script
echo "assignment group is $assgrp"if($test -eq $null){ show-msgbox -text "Location $loc doesn´t exist in file $excel. `n`nPlease add it, ***, or I´ll break your fucking legs."-title "Sucks!"}

Any help appeciated.


Viewing all articles
Browse latest Browse all 10624

Trending Articles