PowerShell Refresh Excel without opening excel fil

2019-08-05 03:10发布

I have an SSIS Package I have developed with an 'Execute Process Task' and 'Data Flow Task'. I am having an issue with execution of the package.

RESULTS: (Manual vs. Scheduled)

All processes are successful when run executed manually in 32-bit mode from VS and SSISDB when I login with a specific user profile 'GEORGES/BL0040EP'.

  1. 'Execute Process Task' -- Run a powershell script to refresh excel connections and save the excel file. (SUCCESS)
  2. 'Data Flow Task' -- Reads the excel data and inserts it to SQL Server table. (SUCCESS)

I am trying to run the package with SQL Agent (using a proxy account), and the process has some issues.

  1. 'Execute Process Task' -- Run a powershell script to refresh excel connections (DOES NOT WORK, NO ERROR MESSAGES). Save the excel file (SUCCESS).
  2. 'Data Flow Task' -- Reads the excel data and inserts it to SQL Server table. (SUCCESS)

PROBLEM:

The powershell script to refresh the Excel file seemingly does not get issued to the SSAS Server when run from SQL Agent. Not "query issues successfully". Not "query issues with permissions error". Simply "query is not at all executed". No permissions issues are logged or detected.

I can tell because I ran SQL Profiler on the server. When Agent calls the package there is no query activity. When VS/SSISDIB calls the package I can see the query being issued. Both successfully with user profile (GEORGES\bl0040ep); and unsuccessfully with permissions error with an unauthorized user profile (GEORGES\bl0040).

QUESTION:

Why would SQL Agent not run the query?

I even added to the posh command $env:UserName | Out-File -filepath to output a text file containing the user name. And the Proxy Account setup appears to be running under the expected user profile context. Content of the text file is bl0040ep.


Create Proxy Account

Creating a Proxy User to run an SSIS package in SQL Server Agent

USE master 
GO

-- Create a proxy credential for xp_cmdshell.
EXEC sp_xp_cmdshell_proxy_account 'GEORGES\bl0040ep', '!myPW!';--SELECT  * FROM [master].[sys].[credentials]

-- Grant execute permission on xp_cmdshell to the SQL Server login account. 
GRANT exec ON sys.xp_cmdshell TO [GEORGES\bl0040ep] 
GO

-- Create a credential containing the GEORGES account PowerGEORGES\PowerUser and its password
CREATE CREDENTIAL Credential_BL0040EP WITH IDENTITY = N'GEORGES\bl0040ep', SECRET = N'!myPW!'
GO

USE [msdb]
GO
-- Create a new proxy called SSISProxy and assign the PowerUser credentail to it
EXEC msdb.dbo.sp_add_proxy @proxy_name=N'Proxy_BL0040EP',@credential_name=N'Credential_BL0040EP',@enabled=1

-- Grant SSISProxy access to the "SSIS package execution" subsystem
EXEC msdb.dbo.sp_grant_proxy_to_subsystem @proxy_name=N'Proxy_BL0040EP', @subsystem_id=11

-- Grant the login testUser the permissions to use SSISProxy
EXEC msdb.dbo.sp_grant_login_to_proxy @login_name = N'GEORGES\bl0040ep', @proxy_name=N'Proxy_BL0040EP'
GO

DatabaseSSAS_UsageStats_xlsx_ExcelRefresh.ps1

enter image description here

# Refresh the excel workbook connections and save the updated file
$file = 'C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats.xlsx'
$x1 = New-Object -ComObject Excel.Application
$x1.Visible = $false
$x1.DisplayAlerts = $False
$enddate = (Get-Date).tostring("dd-MM-yy")
# $filename = 'C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_Usage Stats ' + $enddate + '.xlsx'
$filename = 'C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats.xlsx' 
$env:UserName | Out-File -filepath C:\SVN\BusinessAnalysts\ExcelTools\RefreshAll_process.txt
$wb = $x1.workbooks.Open($file)
$wb.refreshall() 

# REM: Use SLEEP to eliminate the message: "This will cancel a pending data refresh. Continue?"
Start-Sleep -Second 20
$wb.SaveAs($filename)
$wb.Close()
$x1.Quit()
Remove-Variable wb,x1

SQL Profiler

No activity is captured when running the package from SQL Agent. When run from VS and SSISDB, a login error is captured.

SQLProfiler_(VS/SSIDB-connectionerror_user-bl0040).png

enter image description here

SQLProfiler_(SQLAGENT-connection_no-activity).png

There is no image to attache. Simply there is no activity for the user (bl0040ep) when the package is run under the SQL Agent context.


Wed 02/06/2019 14:31:46.96

UPDATE 1: System Desktop Folder

I added ‘Desktop’ folder on System32, ran the job from SQL Agent, and the issues persists. This was recommended on a similar issue reported on TechNet Issues with simple script executed via SQL Server Agent... C:\Windows\System32\config\systemprofile\Desktop and C:\Windows\SysWOW64\config\systemprofile\Desktop. The folder already existed on SysWOW64.

UPDATE 2: PowerShell executable in 32-bit

I have also tried to directly invoke the 32-bit version of PowerShell: %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe. Still the excel refresh does not complete when run via SQL Agent with proxy account. Reference Run a 32 bit Powershell script on Sql Server Agent

UPDATE 3: Windows Task Scheduler instead of SQL Agent Job

I have tried a different scheduling method for the package execution (Windows Task Scheduler, instead of SQL Agent). The schedule completes but it behaves the exact same way that SQL Agent does... the query is not being sent to the datasource. Reference Nirav's Diary, Schedule SSIS Package Without Deploying, 2-Windows Schedule Task

1条回答
萌系小妹纸
2楼-- · 2019-08-05 03:37

I have solve this by removing Excel from the picture. Instead of Excel source I am now using the connector .Net Providers for OleDb\Microsoft OLE DB Provider for Analysis Services 2.0. There are a couple extra steps needed in the database to get the same dataset (using SQL Views instead of the Excel Powerquery M).

I lost this fight to Excel... it did not want to be automated. I did end up still using the same CREDENTIALS and PROXY to schedule the job.

enter image description here

查看更多
登录 后发表回答