How to update a connection string of an excel file from a script (PS) - excel

We have an excel file which contains a connection to a database to retreive data (with a select statement).
We want to update via a (preferrably powershell) script the connection string of that file to make it query another server instead.
So for exemple :
I have report.xlsx file which connects to server A.
I run update-connection.ps1
And when I open report.xlsx it now connects to server B.
Any idea how we could do that?
Thanks.

It should be fairly easy if you decide (are allowed) to store the connection (server name) in a worksheet. Your VBA code can dynamically build the connection string based on the value of a cell. (I would probably create a named range and use it in the code).
I don't know PowerShell but the code can look something like:
$workbook.Range("Server").Value2 = "PROD_01"
You can make the worksheet hidden if you wish, but it is not a serious security.

You could try automating Excel via PowerShell, as in this article: http://kentfinkle.com/PowershellAndExcel.aspx
If you don't want to automate Excel then you could try using something like ClosedXML in your PowerShell script: http://closedxml.codeplex.com/

You can parse the connectionstring with System.Data.Common.DbConnectionStringBuilder. Check this SO thread:
Powershell regex for connectionStrings?

Related

SSIS - Power Query Source: setting connection at runtime

I'm trying to use the Power Query source component in a generic way from SSIS (VS2019).
The idea would be to use a for each loop to load and transform Excel files. At run time, I need to set the connection manager properties for each file as well as the PQY script to be executed on the file.
What I did so far is trying to create a JSON connection string inside a script component and assign the connection string to the connection manager. It keeps on saying that the file requires credentials.
Would someone already experienced that kind of dev? All the files do have the same structure so far, do meta-data need to be refreshed too?
[Edit]
1. In the control flow, I'm retrieving the PQY script I want to apply from a DB.
Before transormations, script starts like this:
let Source = Excel.Workbook(File.Contents("path_to_a_file.xlsx"),null,true),RawData_Sheet = Source{[Item="Table1",Kind="Table"]}[Data]..."
In the C# script task, I'm replacing the path to excel file by the current file variable. M Script is stored in a variable used in the PQY component.
C# Script is then updating the PQY connection manager to target the appropriate file:
ConnectionManager _conn = Dts.Connections["Power Query Connection Manager"];
String _ConnectString = "[{kind:File,path:path_to_a_file.xlss,AuthenticationKind:Windows,Username:myusername,Password:mypassword}]";
_conn.ConnectionString = _ConnectString;
The PQY component is left has it is, connected to ["Power Query Connection Manager"] and getting its script from the variable I set.
PQY configuration screen
Thanks for any tip on this,
Olivier
I can't address the specifics of the PQ but generic anything in a Data Flow will not work.
The Data Flow task works because it makes a strict contract between the source(s) and the destination(s). These columns with these data types will be in play during the run. It's a design-time contract because that allows the run-time engine to allocate resources based on how many buffers of data the system can support. Each row is X bytes, we have Y bytes of memory available, so Z buffers worth of data plus parallelism stuff.
Wish I had a better story to tell you.

Generic User for Excel Connection String

Is it possible to have a generic user reference in the data connection string of an Excel data connection?
I have a external reference to another workbook that I would like to share with my colleagues, and in the collaboration software that we use there is a path for each user's synced folder, such as:
C:\Users\"Username"\....
Is there a prompt similar to %USERPROFILE% that I could use in this instance? Here is a full connection string example:
DSN=Excel Files;DBQ=C:\Users\"Username"\...\File.xlsx;DefaultDir=C:\Users\"Username"\...;DriverId=1046;MaxBufferSize=2048;PageTimeout=5;
To generate C:\Users\Username use: environ("HOMEDRIVE") & environ("HOMEPATH")
see Environ Function
and also Windows APIs & Wscript at Daily Dose of Excel - Get the Path to My Documents in VBA

Select the connection manager to be used in SSIS source by means of a parameter

I created 3 different connection managers in an SSIS package, say connA connB and ConnC. How can I select the connection to use in an ado.net source using a parameter?
Thank you in advance
Carlo
The way that I would go about it is to wrap your logic in a Script Task with a simple IF Else logic
Return your source via SQLDataAdapter and store in an object Variable.
This variable is then available as a table to read from in in any part of your package. Assuming that you assign the scope of the variable correctly.

VBA odbc connection accessing a single library only

I have successfully connected to an as400 server. But whenever i execute an sql statement
select * from nosd0
It doesn't work, because nosd0 is in lib1/fil1(nosd0)
it gives an error saying nosd0 is not in lib2.
When I execute the query on STRSQL on as400 it works fine.
I tried creating an alias and it's malfunctioning. Please I really need some help on this one
Alias is working, I am accessing the wrong file.
Ok I figured out the problem, this will also help all those who wants to connect to their AS400 iSeries using VBA. ;)
My problem above is that when I try my Query on the box, it accesses lib1/nosd0, and in VBA, I was trying to get lib2/fil1(nosd0) which is a description of the table nosdo itself. The simple solution is to query
select * from lib1.nosd0
More on that when connecting to an AS400 iSeries using ODBC, there is a parameter called DBQ
Connection String Parameters
My final connection string would be.
ConnectString = "Driver={ISeries Access ODBC Driver};System=" & DCServer(I) & ";Uid=--;Pwd=--;NAM=0;DBQ=lib1,*ALL;"

SSIS: Why when I add expression variable to Connection Manager properties I lose connection?

I have a SSIS package that needs to enumerate Excel files in Sharepoint. When I setup the For Each loop container at the package level and then set up the file path in my Excel Source at the task level everything is fine. When I add my expression variable to the Excel Connection Manager properties I lose the connection and can't view my input table anymore. I have "Delay Validation" set to true on the Excel Connection Manager.
Has anybody experienced this?
EDIT:
The following are screenshots of my ForEach Loop Container configuration:
The following is a screenshot of where I am passing the variable. It is in the Expression box of the Excel Connection Manager:
Do you have a default value set for the variable to point to a sample file? Even though you have set the Delay validation to be True it just means that when the package runs it would not do a validation on the connection. It does not mean that while you are working on the package it would not use the variable to translate to the connection. So you should go ahead and set the default value of your connection variable to the path of the file you are working on.
I had set the DelayValidation at the connection level, at the dataflow level, and at the for-each loop in control flow level but none were working. I had spent 3-4 hours until I found this thread and setting a default value of the variable finally worked.
So setting a default value of the connection variable does solve the problem if you have a similar issue.

Resources