4

I have created a runbook to "Copy Files From Azure Storage to AzureVM". While testing i got the below exception stating that "Cannot find the 'Connect-Azure' command". Can any one from the other end, please look into this and help me out.

Here is the Screen Shot:

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 }
Michael Curd
  • 617
  • 4
  • 10
praveen gogula
  • 599
  • 3
  • 6
  • 22
  • I'm not familiar with a `Connect-Azure` command, where did you get that from? If you're using a connection defined in your Automation account you should do something like `$AzureConn = Get-AutomationConnection -Name $AzureConnectionName` – BenV Oct 13 '15 at 16:37
  • We have a runbook defined with name "Connect-Azure" from the Gallery. I'm trying to import that one. But when i tried to test that its saying that "the runbook has been depricated". – praveen gogula Oct 13 '15 at 16:40
  • and its throws an exception saying that "exception Could not retrieve 'Visual Studio Enterprise with MSDN' connection asset. Check that you created this first in the Automation service." – praveen gogula Oct 13 '15 at 16:47
  • The Connect-Azure runbook is deprecated. It still works, but its deprecated. In terms of the exception you're getting, it's because you didn't create a connection asset named "Visual Studio Enterprise with MSDN". Please see http://blogs.technet.com/b/orchestrator/archive/2014/04/11/managing-azure-services-with-the-microsoft-azure-automation-preview-service.aspx for how to set up Azure Automation to talk to Azure using certificate-based auth. – Joe Oct 13 '15 at 18:50
  • I have tried "Connect-Azure" runbook, it worked but i'm getting warning saying that this runbook has been deprecated. Even then I have published it, imported in the above code with no issues. – praveen gogula Oct 13 '15 at 19:52
  • But now when i try to run above code it throughs an error saying that "At line:117 char:5 + Copy-ItemToAzureVM ` + ~~~~~~~~~~~~~~~~~~~~ Cannot find the 'Copy-ItemToAzureVM' command. If this command is defined as a workflow, ensure it is defined before the workflow that calls it. If it is a command intended to run directly within Windows PowerShell (or is not available on this system), place it in an InlineScript: 'InlineScript { Copy-ItemToAzureVM }'" @ joe – praveen gogula Oct 13 '15 at 20:00
  • For that I have taken a runbook named "Copy-ItemToAzureVM" from the Gallery and tried to test it, but it raises an error stating that "The converted JSON string is in bad format. (The converted JSON string is in bad format.)" – praveen gogula Oct 13 '15 at 20:02
  • Error: Get-AzureWinRMUri : BadRequest: The hosted service name is invalid. At Connect-AzureVM:63 char:63 + + CategoryInfo : CloseError: (:) [Get-AzureWinRMUri], CloudException + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.GetAzureWinRMUri – praveen gogula Oct 14 '15 at 14:41
  • Error: Invoke-Command : Cannot validate argument on parameter 'ConnectionUri'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the command again. At Copy-ItemToAzureVM:180 char:180 + + CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand – praveen gogula Oct 14 '15 at 14:41
  • @praveengogula Is getting any solution? – Nullpointer Jan 02 '16 at 10:13
  • @Ravi G Yep, Got the Solution – praveen gogula Mar 04 '16 at 15:05
  • @praveengogula plz share the solution.... – Nullpointer Mar 05 '16 at 06:31
  • please do find below aanswer @ravi.G – praveen gogula Mar 16 '16 at 15:36

1 Answers1

3

Here is the custom runbook for "Copy Files From Azure Storage to 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") 
} }
praveen gogula
  • 599
  • 3
  • 6
  • 22