Azure VM 获取文件值

问题描述 投票:0回答:1

有没有办法获取特定路径中文件的本例 xml 值?我想获取每个虚拟机的信息,我必须知道我的软件正在运行哪个版本。

我想通过使用 Azure 门户获取此信息

azure azure-virtual-machine
1个回答
0
投票

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文件。

enter image description here

对于单个虚拟机,您可以通过导航到

VM > Run command.

从 azure 门户使用运行命令

enter image description here

对于多个虚拟机,您可以使用带有 PowerShell 脚本的自动化帐户。

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.