[英]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 .我正在尝试通过另一个 powershell 脚本调用 powershell 脚本,我可以在控制台上看到输出,但是在提取 csv 值时为空,以及如何打印单个值,如 StandardName(用于参考检查输出文件)等。
$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但是在 csv 值中提取时为空
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
$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.) $report_temp | Add-Member -MemberType NoteProperty -Name "Detail" -Value $output.Message
,你应该使用$output.Message[0]
而不是$output.Message
(因为类型$output.Message
是数组,你需要使用index 来获取值;否则,它不会将详细信息添加到报告中。)
how can i print individual value like StandardName我如何打印像 StandardName 这样的单个值
you'd better convert the output to json, then convert it to a custom PSCustomObject object.您最好将输出转换为 json,然后将其转换为自定义的 PSCustomObject 对象。 And you can use the properties(like StandardName) of the object.您可以使用对象的属性(如 StandardName)。
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.