2015-10-13 129 views
4

我已經創建了一個「將文件從Azure存儲複製到AzureVM」的操作手冊。在測試時,我得到了下面的異常,指出「無法找到'Connect-Azure'命令」。任何一個從另一端,請看看這個並幫助我。找不到'Connect-Azure'命令

下面是屏幕截圖:

enter image description here

代碼:

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 } 
+0

我不熟悉'Connect-Azure'通訊你從哪裏得到這些信息?如果您使用自動化帳戶中定義的連接,則應該執行'$ AzureConn = Get-AutomationConnection -Name $ AzureConnectionName'這樣的操作。 – BenV

+0

我們有一個從圖庫中定義的名爲「Connect-Azure」的Runbook。我試圖導入那個。但是,當我試圖測試它的說法是「runbook已被depricated」。 –

+0

,它會拋出一個異常,說「例外​​ 無法檢索'具有MSDN'的Visual Studio Enterprise'連接資產。請檢查您是否在自動化服務中首先創建了這個例外。」 –

回答

3

這裏是 「複製文件從Azure存儲到AzureVM」 自定義運行手冊

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") 
} }