Commonly available examples of PowerShell SCRIPT based data-sources read in simple text files (.txt, .csv) created from Excel spreadsheets using the Excel "Save As..." command to create a copy of a native Excel worksheet into a simple text file. This article documents a method to create a Data sources that instead imports directly from native Excel worksheets (.xlsx files). The method utilizes the free Microsoft Access Control Entry (ACE) driver/provider and a corresponding PowerShell cmdlet called Get-ACEData.
- Search the Microsoft site for the "Microsoft Access Data Engine 2010 Redistributable"
- On that page download the AccessDatabaseEngine_x64.exe to your WFA server
- Run the .exe on the WFA server which installs the ODBC provider onto the WFA Server
To obtain and install the Get-ACEData cmdlet follow these steps:
Below is an data source that reads data directly from a Excel worksheet file named "depts.xlsx". The worksheet has a tab sheet named "tech" that contains 3 columns. The first row (row 1) of the tech table contains the column headings: Dept Code, Description, and Business Unit. These column headings become PowerShell object properties when importing with Get-ACEData cmdlet. Note that when column headings have spaces the PowerShell property names must be enclosed in quotes as seen below.
# ==================== MAINLINE FOR DATA SOURCE ========================
# Import-Module ace # The Import-Module is not needed if ace.psm1 code is included above the mainline data source code
$inFile = "C:\ProgramData\OnCommand\WFA\UserInput\depts.xlsx"
try {
$inTable = Get-ACEData -FilePath $inFile
} catch [System.Exception] {
$msg = "Data Source: Could not read input table from file path: $($_.Exception)"
Get-WFALogger -message $msg -Error ; Throw $msg
}
$outFile = ".\depts.csv" # WFA dictionary "depts" must exist which defines each field
try {
New-Item -Path $outFile -type file -force | Out-Null
} catch [System.Exception] {
$msg = "Data Source: Could not create output file path: $($_.Exception)"
Get-WFALogger -message $msg -Error ; Throw $msg
}
foreach ( $row in $inTable.tables["tech$"] )
{
[string] $deptCode = $row."Dept Code"
[string] $decription = $row.description
[string] $businessUnit = $row."Business Unit"
write-host $("Dept Code: " + $deptCode + "`t Description: " + $description ) # for debug-only
$outLine = $($deptCode + "`t" + $description + "`t" + $businessUnit + "`n" )
Add-Content $outFile ( [byte[]][char[]] "\N`t$outLine") -Encoding Byte
}