【发布时间】:2023-03-20 05:03:01
【问题描述】:
我正在尝试从批处理池中运行的任务执行 powershell 脚本(我正在使用适用于 .NET 的 Azure 存储客户端库将其上传到存储帐户)。
我知道我正确上传了 powershell 脚本(我可以通过登录 Azure 并验证存储帐户中是否存在 .ps1 脚本来验证这一点),并且池、作业和任务已正确创建,但是我怀疑我发送给任务的实际命令不正确,这就是我的任务出错的原因。 (这是下面代码示例中名为 powershellTask 的字符串)
List<CloudTask> tasks = new List<CloudTask>();
string inputFileName = inputFiles[0].FilePath;
string powershellTask = $"powershell {inputFileName}";
CloudTask task = new CloudTask("Test-Powershell-Execution-Task",
powershellTask);
task.ResourceFiles = new List<ResourceFile> {inputFiles[0]};
tasks.Add(task);
batchClient.JobOperations.AddTask(JobId, tasks);
powershell 脚本之前在代码前面已上传到 azure 存储(我可以验证这是否已正确上传):
foreach (string filePath in inputFilePaths)
{
inputFiles.Add(UploadFileToContainer(blobClient, inputContainerName, filePath));
}
private static ResourceFile UploadFileToContainer(CloudBlobClient blobClient, string containerName, string filePath)
{
Console.WriteLine("Uploading file {0} to container [{1}]...", filePath, containerName);
string blobName = Path.GetFileName(filePath);
filePath = Path.Combine(Environment.CurrentDirectory, filePath);
CloudBlobContainer container =
blobClient.GetContainerReference(containerName);
CloudBlockBlob blobData = container.GetBlockBlobReference(blobName);
blobData.UploadFromFileAsync(filePath).Wait();
SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy
{
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(2),
Permissions = SharedAccessBlobPermissions.Read
};
// Construct the SAS URL for blob
string sasBlobToken = blobData.GetSharedAccessSignature(sasConstraints);
string blobSasUri = String.Format("{0}{1}", blobData.Uri, sasBlobToken);
return ResourceFile.FromUrl(blobSasUri, filePath);
}
池、作业和任务均已正确创建。 但是,当我单击 Azure 上的任务时,我会收到消息。 “处理您的请求时遇到错误,请重试” Azure 似乎没有向我提供有关该请求有什么问题的更多信息。 当我终止任务时,我无法获得任何进一步的信息,因为终止任务会引发异常。 “尝试从已完成任务中确定信息时抛出异常 消息:操作返回了无效的状态代码“冲突””
如果有人有任何想法或可以向我指出有关在 azure 批处理中上传和执行 powershell 脚本的进一步文档的方向,那将不胜感激。谢谢!
编辑: 感谢@Hydraxy 在下面的回复,让我在这方面更进一步(真的很有帮助,特别是告诉我不要在代码末尾删除任务)。
正如正确推测的那样,该任务正在运行。问题是我的代码只是试图解析 stdout.txt,而不是 stderr.txt。
当我在 Azure 门户上检查 stderr.txt 时,我看到了我的错误:
Invoke-Sqlcmd : The term 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At D:\Dev\codebase\BatchesInAzure\BatchesInAzure\bin\Debug\netcoreapp2.1\BatchesInAzure.ps1:1
char:1
+ Invoke-Sqlcmd -ServerInstance "dev-database" -Database "Dayinsure.Ap ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Invoke-Sqlcmd:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
看起来是这样,因为我怀疑我的 powershell 脚本不正确。我已经在本地的 powershell 中执行了这个脚本,它总是返回结果。 Azure 不支持 Invoke-SqlCmd 吗?
认为分享我正在上传并尝试执行的 BatchesInAzure.ps1 文件的内容可能会有所帮助(出于显而易见的原因已删除服务器名称、用户名和密码)。
Invoke-Sqlcmd -ServerInstance "my-database-server" -Database "my-lovely-database" -Username "database-user" -Password "super-secure-password" -Query "SELECT GetDate() as TimeOfQuery"
【问题讨论】:
标签: c# azure powershell azure-batch