有没有办法获取特定路径中文件的本例 xml 值?我想获取每个虚拟机的信息,我必须知道我的软件正在运行哪个版本。
我想通过使用 Azure 门户获取此信息
Azure VM 获取文件值
注意:确保在订阅级别分配 azure 虚拟机管理员角色
PowerShell 代码
Connect-AzAccount -Identity
Set-AzContext -Subscription "Subscription_ID"
# Path of the XML file on each VM
$xmlFilePath = "C:\file.xml"
# Define the PowerShell script that will run on each VM
$script = @"
param (
[string]`$xmlFilePath
)
[xml]`$xmlContent = Get-Content -Path `"$xmlFilePath`"
`$appName = `$xmlContent.SoftwareInfo.Application.Name
`$appVersion = `$xmlContent.SoftwareInfo.Application.Version
Write-Output "Application Name: `$appName"
Write-Output "Application Version: `$appVersion"
"@
$vms = Get-AzVM
# Loop through each VM and execute the script
foreach ($vm in $vms) {
try {
# Run the command on the VM
$result = Invoke-AzVMRunCommand -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -CommandId 'RunPowerShellScript' -ScriptString $script -Parameter @{"xmlFilePath" = $xmlFilePath}
# Display the VM name along with application information
Write-Output "VM Name: $($vm.Name)"
Write-Output "Output from VM:"
$result.Value | ForEach-Object {
Write-Output $_.Message
}
Write-Output "-----------------------------------"
}
catch {
Write-Output "Failed to run command on VM $($vm.Name): $_"
}
}
这是azure VM中的xml文件。
对于单个虚拟机,您可以通过导航到
VM > Run command.
从 azure 门户使用运行命令
对于多个虚拟机,您可以使用带有 PowerShell 脚本的自动化帐户。
输出: