xml - 使用Powershell编写xml

标签 xml powershell sharepoint-2010 powershell-2.0

我有一个脚本,可以获取我所需的有关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,并为其创建站点供经理使用

我该怎么做 ?

谢谢您的帮助 !

最佳答案

我要说的是,您有2种选择应该相当容易:

最简单的方法是建立一个字符串作为xml,而不是使用write-output。

$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 

关于xml - 使用Powershell编写xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8134203/

相关文章:

java - Jackson 根据字段值添加包装器

c# - 使用通用标记从 XML 中获取特定数据

php - 从 php 脚本解密在 powershell 中使用 Rijndael 方法创建的文本

JQuery getJSON 在包含撇号的 SharePoint REST 数据上失败

javascript - 如何将JavaScript验证添加到选择字段并检查有效选项?

android - fragment 内容重叠工具栏和底部导航 View

xml - 如何通过命名空间区分 XSD 中的元素

powershell - 动态创建现有对象的名称以设置其属性之一

xml - 通过 PowerShell 写入和读取 XML 配置

sharepoint - 比较 SharePoint 2010 中的两个列表