【问题标题】:Unable to execute a powershell script using Azure Batch Account task无法使用 Azure Batch Account 任务执行 powershell 脚本
【发布时间】: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


    【解决方案1】:

    我不确定你的任务是否真的失败了。导航到 UI 中的任务页面时,您可能会遇到一些间歇性错误。

    您在终止任务时遇到的错误:“消息:操作返回了无效的状态代码‘冲突’”表明当您尝试终止任务时该任务可能已经完成。您应该能够检查 task.ExecutionInformation 以查看任务的退出代码,或使用 Azure 门户之类的 UI 来查看结果。

    您能否分享您看到错误的门户页面的屏幕截图?

    此外,如果您重现问题并留下任务(即不要删除作业和任务),如果问题仍然存在,我可以帮助进一步调试。请注意,您无需为离开工作/任务而付费,因此无需支付任何费用。一旦我们诊断出问题所在,您就可以将其删除。

    【讨论】:

    • 您好,谢谢您的回复。该任务现在正在运行(当我键入时),我将在它完成时发布更多详细信息,同时这是显示失败任务的 Azure 门户页面的屏幕截图。 drive.google.com/file/d/1o7sMjg0VLUA6NpdOe0wznmQ-FYeWyJQi/view
    • 您好,您是对的。任务实际上并没有失败。当我让任务有时间执行时,它最终又回来了。我的问题是我只在代码中解析 stdout.txt,如果我检查了 stderr.txt,我会看到我的任务出错:code 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.
    • 基于this,如果您运行的机器没有安装 SQL Server(或至少 SQL Server 客户端工具),听起来您会收到该错误。我的猜测是您使用的操作系统映像没有安装 SQL 服务器,这就是您不能使用该 cmdlet 的原因。您可能需要使用 Batch StartTask 安装一些东西,例如 SQL 服务器客户端工具,以便您的任务使用它
    猜你喜欢
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多