简体   繁体   中英

Running Command on Azure vm via powershell

I am trying to invoke powershell script via another powershell script , and i can see output on console but while extracting in csv values are null and also how can i print individual value like StandardName (for reference check output file )etc .

$report =$null
$report= @()
$vms = get-azvm  #vmname detail 
foreach( $vm in $vms){
$data =get-azvm -ResourceGroupName $vm.resourcegroupname -Name $vm.name -Status
if ( ($data.OsName -contains "Windows Server 2016 Datacenter") -and ($data.Statuses.DisplayStatus -contains "VM running") )
{
    Write-Host server name is : $data.Name
    $invoke = Invoke-AzVMRunCommand -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -CommandId RunPowerShellScript -ScriptPath ./timezone.ps1 
    $output=$invoke.Value[0,1]
    $output
    $report_temp = new-object psobject 
    $report_temp | Add-Member -MemberType NoteProperty -Name "VM Name" -Value $data.Name
    $report_temp | Add-Member -MemberType NoteProperty -Name "Detail" -Value $output.Message
    #assiging value to variable which we had created eariler 
    #$report += $report_temp
  }
}
$report |export-csv "c:\date.csv"

Below are Output for script:

Code          : ComponentStatus/StdOut/succeeded
Level         : Info
DisplayStatus : Provisioning succeeded
Message       : Id                         : UTC
                DisplayName                : (UTC) Coordinated Universal Time
                StandardName               : Coordinated Universal Time
                DaylightName               : Coordinated Universal Time
                BaseUtcOffset              : 00:00:00
                SupportsDaylightSavingTime : False
                DisplayHint : DateTime
                Date        : 3/31/2020 12:00:00 AM
                Day         : 31
                DayOfWeek   : Tuesday
                DayOfYear   : 91
                Hour        : 13
                Kind        : Local
                Millisecond : 841
                Minute      : 55
                Month       : 3
                Second      : 11
                Ticks       : 637212597118417503
                TimeOfDay   : 13:55:11.8417503
                Year        : 2020
                DateTime    : Tuesday, March 31, 2020 1:55:11 PM `

but while extracting in csv values are null

In this line of code $report_temp | Add-Member -MemberType NoteProperty -Name "Detail" -Value $output.Message $report_temp | Add-Member -MemberType NoteProperty -Name "Detail" -Value $output.Message , you should use $output.Message[0] instead of $output.Message (because the type of $output.Message is array, you need to use index to fetch the value; otherwise, it will not add the details to the report.)

how can i print individual value like StandardName

you'd better convert the output to json, then convert it to a custom PSCustomObject object. And you can use the properties(like StandardName) of the object.

Here is the completed code:

$report =$null
$report= @()
$vms = get-azvm   #vmname detail 
foreach( $vm in $vms){
$data =get-azvm -ResourceGroupName $vm.resourcegroupname -Name $vm.name -Status
if ( ($data.OsName -contains "Windows Server 2016 Datacenter") -and ($data.Statuses.DisplayStatus -contains "VM running") )
{
    Write-Host server name is : $data.Name
    $invoke = Invoke-AzVMRunCommand -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -CommandId RunPowerShellScript -ScriptPath ./timezone.ps1
    $output=$invoke.Value[0,1]
    $output
    $report_temp = new-object psobject 
    $report_temp | Add-Member -MemberType NoteProperty -Name "VM Name" -Value $data.Name
    $report_temp | Add-Member -MemberType NoteProperty -Name "Detail" -Value $output.Message[0]

    #here convert the $output to json
    $temp =$output.message[0]
    $temp = $temp | ConvertTo-Json
    $temp = $temp.Replace("\n\n","\n").replace("\n\n","\n").replace("\n",";") 
    $temp = $temp.Remove($temp.LastIndexOf(";"),1) 
    $temp = $temp -replace '\s',''
    $temp = $temp -replace '\"',''
    $items = [ordered]@{}
    $temp.Split(";") | ForEach-Object {
        $key, $value = $_.Split(":")
        $items[$key] = $value
        }

    $t2 = $items | ConvertTo-Json

    #here convert the json to custom PSCustomObject object
    $b1 = $t2 | ConvertFrom-Json

    #here, add the StandardName to the report
    $report_temp | Add-Member -MemberType NoteProperty -Name "StandardName" -Value $b1.StandardName

    #assiging value to variable which we had created eariler 
    $report += $report_temp
  }
}
$report |export-csv "c:\date.csv"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM