存储为对象时,使用Powershell输出进行遍历

时间:2017-10-30 20:01:47

标签: html powershell email output html-email

我有这个命令从我的服务器获取CLuster信息:

$DBServers = "DC2TSCDB01"
$DBObject = `
    Invoke-Command -ComputerName $DBServers {
        Import-Module FailoverClusters
        Get-ClusterGroup `
        | Where-Object {$_.Name -Like 'SQL Server*'} `
        | Sort-Object Name `
        | Format-Table `
        | out-string
}
$DBObject

Name                     OwnerNode  State
----                     ---------  -----
SQL Server (FS)          DC2TSCdb02 Online
SQL Server (GG)          DC2TSCdb02 Online
SQL Server (RQ)          DC2TSCdb02 Online
SQL Server (MSSQLSERVER) DC2TSCdb01 Online

如何在打印时遍历每个值,以便将其格式化为html输出?

1 个答案:

答案 0 :(得分:1)

$DBServers = "DC2TSCDB01"
$DBObject = `
    Invoke-Command -ComputerName $DBServers {
        Import-Module FailoverClusters
        Get-ClusterGroup `
        | Where-Object {$_.Name -Like 'SQL Server*'} `
        | Sort-Object Name
}

# Convert to table for console
$DBObject | Format-Table -AutoSize

# Convert to HTML table with all elements
$DBOject | ConvertTo-Html

# Convert to HTML table with just <table> elements
$DBObject | ConvertTo-Html -Fragment