locked
Error when reading CSV from blob storage RRS feed

  • Question

  • My bjective is to be able to update a CSV with a list of Server Names and import them into Powershell where a command can be written to shutdown the VMs from the imported CSV.  The CSV is located on the blob storage account.  However I have  received the following error:

    "Microsoft.PowerShell.Utility\Write-Error : Parameter set cannot be resolved using the specified named parameters.
    At ListedVMsToShutDown:82 char:82"

    Equally my variables do not output the individual machines within the CSV as the output shows only the text bit of the write-output and no variable.

    My code is:


    #>

    workflow ShutdownVMFromCSV
    {  
        param (
            [parameter(Mandatory=$false)]
            [String]  $AzureADCredentialAssetName = 'xxxxxxxx',
           
            [parameter(Mandatory=$false)]
            [String]
            $AzureSubscriptionName = 'xxxxxxxxxxxx',

            [parameter(Mandatory=$false)]
            [String]
            $url
        )
       
        # Returns VMs that were started
        [OutputType([PersistentVMRoleContext])]

     $Cred = Get-AutomationPSCredential -Name $AzureADCredentialAssetName
        #   Write-Output "Credential Asset Name = $AzureADCredentialAssetName"
        if ($Cred -eq $null)
        {
               Write-Output "Could not retrieve $AzureADCredentialAssetName credential asset. Check that you created this first in the Automation service."
        }

     # Connect to Azure and select the subscription to work against
     Add-AzureAccount -Credential $Cred -ErrorAction Stop | Write-Verbose

        # Select the subscription if a subscription name is provided
        if($AzureSubscriptionName -and ($AzureSubscriptionName.Length > 0) -and ($AzureSubscriptionName -ne "default")) {
            Select-AzureSubscription -Name $AzureSubscriptionName | Write-Verbose
               
        }
      ################
       #Read from Azure blob container
       $VMs = InlineScript { (New-Object System.Net.WebClient).DownloadString("https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.csv"); }
         Write-Output $VMs 
       Write-Output "Starting Forloop"
     
          foreach -parallel ($VM in $VMs)
          {  
            
           #Get-AzureVM | Where-Object {$_.Name -in $VMs} | select name, servicename, instancename
             Write-Output 'VM Name:' $($VM.Name)   
                      Write-Output 'VM InstanceName:' $($VM.instancename)    
               Stop-AzureVM -Name $VM.Name -ServiceName $VM.ServiceName
          }
    }

    Can anyone instruct me on how this 'should' be written or where i am going wrong?

    Monday, June 22, 2015 5:55 PM

Answers

  • You are downloading the CSV as a string, but never converting that CSV string to a PowerShell object. So $VMs contains just a single long string, therefore looping through $VMs it is not looping through the VMs in the CSV, but through each character in the CSV string.

    You need to use ConvertFrom-Csv to convert the CSV string to a PowerShell object, and loop through that.



    Joe Levy
    Twitter: @Jodoglevy
    Blog: http://jodoglevy.com

    If this post was helpful to you, please upvote it and/or mark it as an answer so others can more easily find it in the future.


    • Proposed as answer by Joe Levy_ Monday, June 22, 2015 9:42 PM
    • Marked as answer by Joe Levy_ Tuesday, June 23, 2015 5:51 PM
    • Edited by Joe Levy_ Monday, July 6, 2015 8:10 AM
    Monday, June 22, 2015 9:29 PM

All replies

  • You are downloading the CSV as a string, but never converting that CSV string to a PowerShell object. So $VMs contains just a single long string, therefore looping through $VMs it is not looping through the VMs in the CSV, but through each character in the CSV string.

    You need to use ConvertFrom-Csv to convert the CSV string to a PowerShell object, and loop through that.



    Joe Levy
    Twitter: @Jodoglevy
    Blog: http://jodoglevy.com

    If this post was helpful to you, please upvote it and/or mark it as an answer so others can more easily find it in the future.


    • Proposed as answer by Joe Levy_ Monday, June 22, 2015 9:42 PM
    • Marked as answer by Joe Levy_ Tuesday, June 23, 2015 5:51 PM
    • Edited by Joe Levy_ Monday, July 6, 2015 8:10 AM
    Monday, June 22, 2015 9:29 PM
  • How would that be structured? like so:

    #Read from Azure blob container
       $VMs = InlineScript { (New-Object System.Net.WebClient).DownloadString("https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.csv"); }
    $VMs = convertFrom-CSV    

    Write-Output $VMs 
       Write-Output "Starting Forloop"
     
          foreach -parallel ($VM in $VMs)
          {  
            
           #Get-AzureVM | Where-Object {$_.Name -in $VMs} | select name, servicename, instancename
             Write-Output 'VM Name:' $($VM.Name)   
                      Write-Output 'VM InstanceName:' $($VM.instancename)    
               Stop-AzureVM -Name $VM.Name -ServiceName $VM.ServiceName
          }

    Tuesday, June 23, 2015 8:49 AM
  • Something like the following:

    $VMsCsvString = InlineScript { (New-Object System.Net.WebClient).DownloadString("https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.csv"); }
    $VMs = ConvertFrom-Csv -InputObject $VMsCsvString



    Joe Levy
    Twitter: @Jodoglevy
    Blog: http://jodoglevy.com

    If this post was helpful to you, please upvote it and/or mark it as an answer so others can more easily find it in the future.


    • Edited by Joe Levy_ Monday, July 6, 2015 8:10 AM
    Tuesday, June 23, 2015 5:55 PM