首页 文章

找不到'Connect-Azure'命令

提问于
浏览
4

我创建了一个“将文件从Azure存储复制到AzureVM”的Runbook . 在测试时,我得到了以下异常,声明“无法找到'Connect-Azure'命令” . 可以从另一端的任何人,请看看这个并帮助我 .

这是屏幕截图:

enter image description here

Code:

workflow Copy-FileFromAzureStorageToAzureVM {
param
(
    [parameter(Mandatory=$true)]
    [String]
    $AzureConnectionName,

    [parameter(Mandatory=$true)]
    [String]
    $CredentialAssetNameWithAccessToVM,

    [parameter(Mandatory=$true)]
    [String]
    $StorageAccountName,

    [parameter(Mandatory=$true)]
    [String]
    $ContainerName,

    [parameter(Mandatory=$true)]
    [String]
    $BlobName,

    [parameter(Mandatory=$true)]
    [String]
    $PathToPlaceFile,

    [parameter(Mandatory=$true)]
    [object]
    $VM
)

$TempFileLocation = "C:\$BlobName"

Connect-Azure -AzureConnectionName $AzureConnectionName

Write-Verbose "Downloading $BlobName from Azure Blob Storage to $TempFileLocation"

InlineScript {
    Select-AzureSubscription -SubscriptionName $Using:AzureConnectionName

    $StorageAccount = (Get-AzureStorageAccount -StorageAccountName $Using:StorageAccountName).Label

    Set-AzureSubscription `
        -SubscriptionName $Using:AzureConnectionName `
        -CurrentStorageAccount $StorageAccount

    $blob = 
        Get-AzureStorageBlobContent `
            -Blob $Using:BlobName `
            -Container $Using:ContainerName `
            -Destination $Using:TempFileLocation `
            -Force
}

Write-Verbose ("Copying $BlobName to $PathToPlaceFile on " + $VM.Name)

Copy-ItemToAzureVM `
    -AzureConnectionName $AzureConnectionName `
    -ServiceName $VM.ServiceName `
    -VMName $VM.Name `
    -VMCredentialName $CredentialAssetNameWithAccessToVM `
    -LocalPath $TempFileLocation `
    -RemotePath $PathToPlaceFile }

1 回答

  • 3

    以下是“将文件从Azure存储复制到AzureVM”的自定义Runbook

    workflow Copy-ItemToAzureVM { 
    param 
    ( 
        [parameter(Mandatory=$true)] 
        [String] 
        $AzureSubscriptionName, 
    
        [parameter(Mandatory=$true)] 
        [PSCredential] 
        $AzureOrgIdCredential, 
    
                [parameter(Mandatory=$True)]
        [String]
        $StorageAccountName,
    
        [parameter(Mandatory=$True)]
        [String]
        $ContainerName,
    
        [parameter(Mandatory=$True)]
        [String]
        $BlobName,
    
        [parameter(Mandatory=$true)] 
        [String] 
        $ServiceName, 
    
        [parameter(Mandatory=$true)] 
        [String] 
        $VMName,   
    
        [parameter(Mandatory=$true)] 
        [String] 
        $VMCredentialName, 
    
        [parameter(Mandatory=$true)] 
        [String] 
        $LocalPath, 
    
        [parameter(Mandatory=$true)] 
        [String] 
        $RemotePath,  
    
        [parameter(Mandatory=$False)]
        [String]
        $PathToPlaceBlob = "C:\"        
    ) 
    $Null = Add-AzureAccount -Credential $AzureOrgIdCredential 
    $Null = Select-AzureSubscription -SubscriptionName $AzureSubscriptionName
    
    Write-Verbose "Downloading $BlobName from Azure Blob Storage to $PathToPlaceBlob"
    
    Set-AzureSubscription `
        -SubscriptionName $AzureSubscriptionName `
        -CurrentStorageAccount $StorageAccountName
    
    $blob = 
        Get-AzureStorageBlobContent `
            -Blob $BlobName `
            -Container $ContainerName `
            -Destination $PathToPlaceBlob `
            -Force
    try {
        Get-Item -Path "$PathToPlaceBlob\$BlobName" -ErrorAction Stop
    }
    catch {
        Get-Item -Path $PathToPlaceBlob
    }
    $Credential = Get-AutomationPSCredential -Name $VMCredentialName     
    if ($Credential -eq $null) 
    { 
        throw "Could not retrieve '$VMCredentialName' credential asset. Check that you created this asset in the Automation service." 
    }      
    $Uri = Connect-AzureVM -AzureSubscriptionName $AzureSubscriptionName -AzureOrgIdCredential $AzureOrgIdCredential –ServiceName $ServiceName –VMName $VMName 
    InlineScript { 
        $ConfigurationName = "HighDataLimits"  
        Invoke-Command -ScriptBlock { 
            $ConfigurationName = $args[0] 
            $Session = Get-PSSessionConfiguration -Name $ConfigurationName 
    
            if(!$Session) { 
                Write-Verbose "Large data sending is not allowed. Creating PSSessionConfiguration $ConfigurationName" 
    
                Register-PSSessionConfiguration -Name $ConfigurationName -MaximumReceivedDataSizePerCommandMB 500 -MaximumReceivedObjectSizeMB 500 -Force | Out-Null 
            } 
        } -ArgumentList $ConfigurationName -ConnectionUri $Using:Uri -Credential $Using:Credential -ErrorAction SilentlyContinue      
        $Content = Get-Content –Path $Using:LocalPath –Encoding Byte 
    
        Write-Verbose ("Retrieved local content from $Using:LocalPath") 
    
        Invoke-Command -ScriptBlock { 
            param($Content, $RemotePath) 
    
            $Content | Set-Content –Path $RemotePath -Encoding Byte 
        } -ArgumentList $Content, $Using:RemotePath -ConnectionUri $Using:Uri -Credential $Using:Credential -ConfigurationName $ConfigurationName 
    
        Write-Verbose ("Wrote content from $Using:LocalPath to $Using:VMName at $Using:RemotePath") 
    } }
    

相关问题