简体   繁体   English

无法使用 SQL Server 代理 Powershell CmdExec 步骤处理 Power BI Premium 模型中的分区

[英]Unable to Process Partition in Power BI Premium Model Using SQL Server Agent Powershell CmdExec Step

I am trying to use Analysis Services Cmdlets to process partitions on a Power BI Premium Model.我正在尝试使用Analysis Services Cmdlet来处理 Power BI Premium 模型上的分区。 My PowerShell script works fine when run from ISE, the command line, and when scheduled using windows task scheduler;我的 PowerShell 脚本在从 ISE、命令行运行以及使用 Windows 任务调度程序进行调度时工作正常; however, when I try to schedule the PowerShell script using a SQL Server 2019 Agent job using a step type of Operating System (CmdExec)" the following error message is encountered.但是,当我尝试使用 SQL Server 2019 代理作业使用操作系统 (CmdExec) 的步骤类型来安排 PowerShell 脚本时,会遇到以下错误消息。

Message Executed as user: MyDomain\\MyUser.消息以用户身份执行:MyDomain\\MyUser。 Invoke-ProcessPartition : The connection string is not valid. Invoke-ProcessPartition :连接字符串无效。 At C:\\Users\\MyUser\\Desktop\\PS1\\SSAS\\wtf.ps1:15 char:11 + $asResult = Invoke-ProcessPartition -Credential $UserCredential -Server...+在 C:\\Users\\MyUser\\Desktop\\PS1\\SSAS\\wtf.ps1:15 char:11 + $asResult = Invoke-ProcessPartition -Credential $UserCredential -Server...+
CategoryInfo : NotSpecified: (:) [Invoke-ProcessPartition], ConnectionException + FullyQualifiedErrorId : Microsoft.AnalysisServices.ConnectionException,Microsoft.AnalysisServices.PowerShell.Cmd lets.ProcessPartition. CategoryInfo : NotSpecified: (:) [Invoke-ProcessPartition], ConnectionException +fullyQualifiedErrorId : Microsoft.AnalysisServices.ConnectionException,Microsoft.AnalysisServices.PowerShell.Cmdlets.ProcessPartition。

I have followed the steps in this blog article to setup the job.我已按照此博客文章中的步骤设置作业。 The same windows user is used in all three run scenarios.所有三个运行方案都使用相同的 Windows 用户。 The SQL server is my local development SQL server of which the windows user is SA on the SQL Server and Windows Admin. SQL Server 是我本地开发的 SQL Server,其 Windows 用户是 SQL Server 和 Windows Admin 上的 SA。 The same machine the SQL Instance is on is being used for successfully executing the other three ways of running the PS scripts, (ISE, Command Line, & Windows Task Scheduler) SQL 实例所在的同一台机器用于成功执行其他三种运行 PS 脚本的方式(ISE、命令行和 Windows 任务计划程序)

If I run the following from the command line on the same machine as the SQL server, my local host, the PowerShell script runs successfully.如果我在本地主机 SQL 服务器所在的同一台计算机上从命令行运行以下命令,PowerShell 脚本将成功运行。

PowerShell -File "C\Users\MyUser\Desktop\PS1\SSAS\wtf.ps1"

Below is my PowerShell script modified to be as small as possible to demonstrate issue and of course sensitive information redacted.下面是我的 PowerShell 脚本修改为尽可能小以演示问题,当然敏感信息也被编辑。 Thanks in advance for any help, I'm at a loss at how to proceed.在此先感谢您的帮助,我不知道如何继续。 I really need this to work from the SQL agent so I don't have to guess when steps that the processing depends on have completed.我真的需要它从 SQL 代理工作,所以我不必猜测处理所依赖的步骤何时完成。

$ErrorActionPreference=”Stop”

Import-Module "SqlServer"

$User = "MyUser@MyDomain.com"
$PWord = ConvertTo-SecureString -String "MyPassword" -AsPlainText -Force
$UserCredential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PWord

$server = "powerbi://api.powerbi.com/v1.0/myorg/MyWorkspace"
$db = "MyModel"

$asResult = Invoke-ProcessPartition -Credential $UserCredential -Server $server -PartitionName "DimDate" -TableName "DimDate" -Database $db  -RefreshType "Full"

The step code in my job is:我工作中的步骤代码是:

powershell -File "C:\Users\MyUser\Desktop\PS1\SSAS\wtf.ps1"

UPDATE: I have discovered that if I target an on-prem SSAS server, the exact same code, with the exception of the server variable, does work using the SQL Agent method, but of course switching to on-prem is not what I desire.更新:我发现,如果我以本地 SSAS 服务器为目标,则完全相同的代码(服务器变量除外)确实可以使用 SQL Agent 方法工作,但当然切换到本地并不是我想要的.

UPDATE 2: When I right click on a job within SSMS, and click "Start PowerShell" this opens up a SQL Server PowerShell Window.更新 2:当我右键单击 SSMS 中的作业并单击“启动 PowerShell”时,这会打开一个 SQL Server PowerShell 窗口。 If I try executing the script this way I get the below error message.如果我尝试以这种方式执行脚本,则会收到以下错误消息。 The account being used does not have MFA enabled.正在使用的帐户没有启用 MFA。

Unable to obtain authentication token using the credentials provided.无法使用提供的凭据获取身份验证令牌。 If your Active Directory tenant administrator has configured Multi-Factor Authentication or if your account is a Microsoft Account, please remove the user name and password from the connection string, and then retry.如果您的 Active Directory 租户管理员已配置多重身份验证,或者您的帐户是 Microsoft 帐户,请从连接字符串中删除用户名和密码,然后重试。 You should then be prompted to enter your credentials.然后应该会提示您输入凭据。

If I don't pass a credential to the Invoke-ProcessPartition command using the SQL Server PowerShell window, I get prompted for credentials and the call works.如果我不使用 SQL Server PowerShell 窗口将凭据传递给 Invoke-ProcessPartition 命令,系统会提示我输入凭据并且调用有效。 Of course I can't use this as a workaround because I need it to run unattended.当然,我不能将其用作解决方法,因为我需要它在无人看管的情况下运行。

I also tried opening PowerShell ISE as the account being used for authentication to the Workspace in the PS script and it also gives the same error as the SQL Server Agent Job does.我还尝试打开 PowerShell ISE 作为用于在 PS 脚本中对工作区进行身份验证的帐户,并且它也给出与 SQL Server 代理作业相同的错误。

The connection string is not valid.连接字符串无效。

I have found a solution to the issue.我找到了解决问题的方法。 The resolution was twofold.决议是双重的。

First problem was that when PowerShell was being run from the SQL Server Agent, the Version of the SqlServer module was an older outdated version.第一个问题是,当从 SQL Server 代理运行 PowerShell 时,SqlServer 模块的版本是较旧的过时版本。 I found this out by executing a ps1 file from the SQL Server Agent Job using the following code and looking in the job history results.我通过使用以下代码从 SQL Server 代理作业执行 ps1 文件并查看作业历史记录结果发现了这一点。

Get-Command -module sqlserver invoke*

I tried running我试过跑步

Install-Module sqlserver -AllowClobber -Scope AllUsers  

as an admin but it did not update the SqlServer module the SQL agent was running.作为管理员,但它没有更新 SQL 代理正在运行的 SqlServer 模块。 Instead I created a ps1 file that simply runs相反,我创建了一个简单运行的 ps1 文件

 Install-Module sqlserver -AllowClobber -Scope CurrentUser -Force 

and called the script using a SQL Agent CMD task and this updated the SqlServer module version.并使用 SQL Agent CMD 任务调用脚本,这更新了 SqlServer 模块版本。 After this I started getting a more useful error message:在此之后,我开始收到一条更有用的错误消息:

Unable to obtain authentication token using the credentials provided.无法使用提供的凭据获取身份验证令牌。 If your Active Directory tenant administrator has configured Multi-Factor Authentication or if your account is a Microsoft Account, please remove the user name and password from the connection string, and then retry.如果您的 Active Directory 租户管理员已配置多重身份验证,或者您的帐户是 Microsoft 帐户,请从连接字符串中删除用户名和密码,然后重试。 You should then be prompted to enter your credentials然后应该会提示您输入凭据

With this new error message, I decided to try an alternate way of supplying credentials by utilizing Azure Service Principals.对于这条新的错误消息,我决定尝试使用 Azure 服务主体提供凭据的替代方法。 This new method resulted in success from all PowerShell methods including SQL Server Agent Job.这种新方法在包括 SQL Server 代理作业在内的所有 PowerShell 方法中都取得了成功。 The steps implemented are outlined in this Power BI Documentation .Power BI 文档中概述了实施的步骤。

The final PS code looks like this.最终的 PS 代码如下所示。

$ErrorActionPreference=”Stop”
Import-Module "SqlServer"
$AppId = "AAD_App_Registration_Application_Client_Id"
$TenantId = "AAD_App_Registration_Directory_Tenant_Id"
$AppSecret = "AAD_App_Registration_CertificatesAndSecrets_ClientSecret"
$PWord = ConvertTo-SecureString -String $AppSecret -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $AppId, $PWord

Invoke-ProcessPartition -Server "powerbi://api.powerbi.com/v1.0/myorg/MyModel" -PartitionName "DimDate" -TableName "DimDate" -Database "MyModel" -RefreshType "Full" -ServicePrincipal -ApplicationId $AppId -TenantId $TenantId -Credential $Credential

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM