使用powershell编写xml

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

我有一个脚本,可以获取我需要的有关我的 SharePoint 场的所有信息:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local 
$websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]} 
$webapps = @()

foreach ($websvc in $websvcs) { 
           write-output "Web Applications" 
           write-output "" 
    foreach ($webapp in $websvc.WebApplications) { 
        write-output "Webapp Name -->"$webapp.Name 
           write-output "" 
           write-output "Site Collections" 
           write-output "" 
    foreach ($site in $webapp.Sites) { 
        write-output "Site URL --> -->" $site.URL 
           write-output "" 
           write-output "Websites" 
           write-output "" 
    foreach ($web in $site.AllWebs) { 
        write-output "Web URL --> --> -->" $web.URL 
           write-output "" 
           write-output "Lists" 
           write-output "" 
    foreach ($list in $web.Lists) { 
           write-output "List Title --> --> --> -->" $list.Title 
           write-output "" 
    }

    foreach ($group in $web.Groups) { 
           write-output "Group Name --> --> --> -->" $group.Name 
           write-output ""

    foreach ($user in $group.Users) { 
           write-output "User Name --> --> --> -->" $user.Name 
           write-output "" 
    } 
    }

    }

    }

    } 
}

我想将输出输出到 XML 文件,然后将 xml 文件连接到 HTML 并制作一个网站供经理使用

我该怎么做?

感谢您的帮助!

xml powershell sharepoint-2010 powershell-2.0
3个回答
4
投票

我想说你有两个选择,应该相当简单:

最简单的方法是构建一个字符串作为 xml,而不是使用写入输出。

$procs = get-Process

$xml = "<xml>"
foreach($proc in $procs)
{
    $xml += "<process name='" + $proc.Name + "' id='Proc_"+$proc.Id+"'>"
    $xml += "<modules>"

    foreach($module in $proc.Modules)
    {
        $xml += "<module>"
        $xml += $module.ModuleName
        $xml += "</module>"
    }
    $xml += "</modules>"
    $xml += "</process>"
}

$xml += "</xml>"

$xml | out-File -FilePath c:\temp\proc1.xml

或者 - 您可以将输出构建为 psobject,然后尝试使用 PowerShell cmdlet 之一将其输出为 XML。可能是更可靠的方法,因为您可以通过管道使用其他 PowerShell cmdlet。看起来像这样:

$procs = get-Process

$processObject = @()
foreach($proc in $procs)
{
    $procInfo = new-Object PSObject
    $procInfo | add-Member NoteProperty -Name "Process" $proc.Name
    $procInfo | add-Member NoteProperty -Name "ID" $proc.Id
    $moduleInfo = @()   
    foreach($module in $proc.Modules)
    {           
        $moduleInfo += $module.ModuleName
    }   
    $procInfo | add-Member NoteProperty -Name "Module" $moduleInfo
    $processObject += $procInfo
}

$processObject | export-Clixml c:\temp\procs.xml 

1
投票

如果您想将对象输出为XML,您可以尝试Export-Clixml命令。例如:

目录 |导出-Clixml c: emp\output.xml

如果您想要直接生成 HTML 页面,您可以使用命令 ConvertTo-Html :

目录 |转换为 Html > c: emp\output.html

您可以使用 head、title、body 和 CSS 自定义后面的命令。


0
投票

这是 iHunger 的答案的更新:

$procs = get-Process

$xml = "<xml>"
foreach($proc in $procs)
{
    $xml += "<process name='" + $proc.Name + "' id='Proc_"+$proc.Id+"'>"
    $xml += "<modules>"

    foreach($module in $proc.Modules)
    {
        $xml += "<module>"
        $xml += $module.ModuleName
        $xml += "</module>"
    }
    $xml += "</modules>"
    $xml += "</process>"
}

$xml += "</xml>"

$path = "c:\temp\proc1.xml"
$xml.Save($path)

请参阅使用 PowerShell 读取、更新和写入 XML 文档

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