【问题标题】:Running Azure commands over a PSSession通过 PSSession 运行 Azure 命令
【发布时间】:2016-01-23 01:17:05
【问题描述】:

我正在尝试自动将 Blob 从本地计算机上传到 Azure 存储,然后在我设置的 Azure VM 上下载 Blob。我已经配置了所有内容,并且能够在本地运行脚本来上传 blob。然后我可以 RDP 进入 Azure VM 并运行其他脚本来下载 blob。完美的!

我的问题是尝试使用 PSSession 或 Invoke-Command 在我的本地计算机上执行所有这些操作。我可以成功启动到 Azure VM 的 PSSession

Enter-PSSession -ComputerName <myMachine>.cloudapp.net -Port 8888 -Credential username/password -UseSSL

但是,当我尝试运行 Azure 命令时,例如通过 PSSession

Get-AzureStorageBlob -Container $containerName -Context $context

我得到了错误。

The term 'Get-AzureStorageBlobContent' is not recognized as the name of a 
cmdlet, function, script file, or operable program.

虽然我可以 RDP 到 Azure VM 并正常运行这些命令,但一切正常。 (我在两台机器上都安装了 Azure Powershell SDK)我也尝试过创建一个 New-PSSession 然后使用

Invoke-Command -Session $myNewSession -ScriptBlock {my script}

同样的错误。对这个问题有任何帮助,我会很高兴,谢谢。

【问题讨论】:

  • 在 PS Session 中运行命令之前,您是否尝试过 Import-Module Azure
  • 是的,不高兴 - 我已经添加了答案

标签: powershell azure


【解决方案1】:

所以,经过更多的研究。看来答案很简单。

Enter-PSSession 仅用于交互模式,不适用于脚本。解决此问题的一种方法是创建一个 New-PSSession,然后将其与 Invoke-Command 一起使用

此外,Azure 为您的 blob 文件提供了下载 Uri,因此您可以直接调用和 Web 请求,而无需运行 Azure 命令

例子

$session  = New-PSSession -ComputerName <myMachine>.cloudapp.net -Port 8888 -Credential user/pass -UseSSL

Invoke-Command -Session $session -ScriptBlock { Invoke-WebRequest -Uri https://<myBlobFile>.blob.core.windows.net/container/file.zip -OutFile C:\path\on\remote\machine }

我希望这可以帮助其他人解决这个问题

编辑:这仅在您的 blob 设置为私有时才有效 - 如果您需要限制对文件的访问,那就不好了:(

【讨论】:

    猜你喜欢
    • 2022-01-27
    • 2019-02-08
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    相关资源
    最近更新 更多