Azure 无法使用SQL Server作业代理运行Powershell脚本
我正在尝试使用SQL Server作业代理执行PowerShell脚本,但无法执行。我能够通过PowerShell提示符成功执行脚本。在代理中,我是操作系统(CMDEXEC)并以服务帐户的身份运行。作业已成功执行,但出现以下错误 我正在命令窗口中使用以下命令:Azure 无法使用SQL Server作业代理运行Powershell脚本,azure,powershell,sql-agent-job,azure-data-lake-gen2,Azure,Powershell,Sql Agent Job,Azure Data Lake Gen2,我正在尝试使用SQL Server作业代理执行PowerShell脚本,但无法执行。我能够通过PowerShell提示符成功执行脚本。在代理中,我是操作系统(CMDEXEC)并以服务帐户的身份运行。作业已成功执行,但出现以下错误 我正在命令窗口中使用以下命令: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "& 'D:\Powershell\triggersinfo.ps1'" Message
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "& 'D:\Powershell\triggersinfo.ps1'"
Message
Executed as user: NT Service\SQLSERVERAGENT. ...because it does not exist. At D:\Powershell\triggersinfo.ps1:4 char:98 + ... To-SecureString -Key (Get-Content D:\Triggers_Test\passwords\aes.key) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (D:\Triggers_Test\passwords\aes.key:String) [Get-Content], ItemNotFoundE xception + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommandConvertTo-SecureString : Input string was not in a correct format. At D:\Powershell\triggersinfo.ps1:4 char:69 + ... sword.txt | ConvertTo-SecureString -Key (Get-Content D:\Triggers_Test ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [ConvertTo-SecureString], FormatException + FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand New-Object : Exception calling ".ctor" with "2" argument(s): "Cannot process argument because the value of argument "password" is null. Change the value of argument "password" to a non-null value." At D:\Powershell\triggersinfo.ps1:5 char:16 + ... redential = New-Object System.Management.Automation.PsCredential($use ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommandConnect-AzAccount : Cannot bind argument to parameter 'Credential' because it is null. At D:\Powershell\triggersinfo.ps1:7 char:32 + Connect-AzAccount -Credential $credential + ~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Connect-AzAccount], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Commands.Profile.ConnectA zureRmAccountCommandGet-AzDataFactoryV2Trigger : Run Connect-AzAccount to login. At D:\Powershell\triggersinfo.ps1:16 char:11 + $triggers=Get-AzDataFactoryV2Trigger -DataFactoryName $dataFactoryNam ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Get-AzDataFactoryV2Trigger], PSInvalidOperationException + FullyQualifiedErrorId : Microsoft.Azure.Commands.DataFactoryV2.GetAzureDataFactoryTriggerCommandGet-AzStorageAccount : No subscription found in the context. Please ensure that the credentials you provided are authorized to access an Azure subscription, then run Connect-AzAccount to login. At D:\Powershell\triggersinfo.ps1:70 char:13 + $account = Get-AzStorageAccount -ResourceGroupName 'DataLake-Gen2' - ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Get-AzStorageAccount], ApplicationException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Management.Storage.GetAzureStorageAccountCommandNew-AzStorageAccountSASToken : Could not get the storage context. Please pass in a storage context or set the current storage context. At D:\Powershell\triggersinfo.ps1:71 char:8 + $sas= New-AzStorageAccountSASToken -Service Blob -ResourceType Serv ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [New-AzStorageAccountSASToken], InvalidOperationException + FullyQualifiedErrorId : InvalidOperationException,Microsoft.WindowsAzure.Commands.Storage.Common.Cmdlet.NewAzure StorageAccountSasTokenCommandInvoke-RestMethod : {"error":{"code":"AuthenticationFailed","message":"Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:c5b73384-401f-0061-7f95-b2677600... Process Exit Code 0. The step succeeded.
知道我为什么会犯这个错误吗?如果需要,我也可以在这里粘贴整个PS脚本。谢谢
使用的脚本:
# Connect to Azure Account
$username = "xyz@abc.com"
$password = Get-Content D:\Triggers_Test\passwords\password.txt | ConvertTo-SecureString -Key (Get-Content D:\Triggers_Test\passwords\aes.key)
$credential = New-Object System.Management.Automation.PsCredential($username,$password)
Connect-AzAccount -Credential $credential
# Input Variables
$dataFactoryName="dna-production-gen2"
$resourceGroupName="DataLake-Gen2"
# get dataFactory triggers
$triggers=Get-AzDataFactoryV2Trigger -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName
$datas=@()
foreach ($trigger in $triggers) {
# get the trigger run history
$today = Get-Date
$yesterday = $today.AddDays(-1)
$splat = @{
ResourceGroupName = $trigger.ResourceGroupName
DataFactoryName = $trigger.DataFactoryName
TriggerName = $trigger.Name
TriggerRunStartedAfter = $yesterday
TriggerRunStartedBefore = $today
}
$historys =Get-AzDataFactoryV2TriggerRun @splat
if($historys -ne $null){
# create date
foreach($history in $historys){
$obj =[PsCustomObject]@{
'TriggerRunTimestamp ' = $history.TriggerRunTimestamp
'ResourceGroupName ' =$history.ResourceGroupName
'DataFactoryName' =$history.DataFactoryName
'TriggerName ' = $history.TriggerName
'TriggerRunId'= $history.TriggerRunId
'TriggerType'=$history.TriggerType
'Status' =$history.Status
}
# add data to an array
$datas += $obj
}
}
}
# convert data to csv string
$contents =(($datas | ConvertTo-Csv -NoTypeInformation) -join [Environment]::NewLine)
# upload to Azure Data Lake Store Gen2
#1. Create a sas token
$accountName="dna2020gen2"
# $path = New-Item -ItemType Directory -Path ".\$((Get-Date).ToString('yyyy-MM-dd'))"
$YY = (Get-Date).year
$MM = (Get-Date).month
$DD = get-date –f dd
$fileSystemName="dev"
$filePath="triggers/YYYY=$YY/MM=$MM/DD=$DD/data.csv"
$account = Get-AzStorageAccount -ResourceGroupName 'DataLake-Gen2' -Name $accountName
$sas= New-AzStorageAccountSASToken -Service Blob -ResourceType Service,Container,Object `
-Permission "racwdlup" -StartTime (Get-Date).AddMinutes(-10) `
-ExpiryTime (Get-Date).AddHours(2) -Context $account.Context
$baseUrl ="https://{0}.dfs.core.windows.net/{1}/{2}{3}" -f $accountName , $fileSystemName, $filePath, $sas
#2. Create file
$endpoint =$baseUrl +"&resource=file"
Invoke-RestMethod -Method Put -Uri $endpoint -Headers @{"Content-Length" = 0} -UseBasicParsing
#3 append data
$endpoint =$baseUrl +"&action=append&position=0"
Invoke-RestMethod -Method Patch -Uri $endpoint -Headers @{"Content-Length" = $contents.Length} -Body $contents -UseBasicParsing
#4 flush data
$endpoint =$baseUrl + ("&action=flush&position={0}" -f $contents.Length)
Invoke-RestMethod -Method Patch -Uri $endpoint -UseBasicParsing
#Check the result (get data)
Invoke-RestMethod -Method Get -Uri $baseUrl -UseBasicParsing
看起来您的脚本中有错误。密码读取不正确,身份验证失败。你应该在问题中加入你的脚本。尝试将其减少到仍然会产生此错误的程度。@marsze:使用我使用的脚本、密码文件和保存在脚本中提供的路径中的AES密钥更新了问题。我得到了上面提到的这个错误。在PowerShell提示符下运行脚本时,我的密码文件和密钥也可以正常工作。