使用powershell编写xml

发布于 2024-12-15 14:13:20 字数 1603 浏览 0 评论 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 并创建一个网站供经理使用,

我该怎么做?

感谢您的帮助!

i have a script that get all the info i need about my SharePoint farm :

[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 "" 
    } 
    }

    }

    }

    } 
}

i want to make the output to an XML file and then connect the xml file to HTML and make a site of it for manager use

how can i do it ?

thanks for the help !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空心空情空意 2024-12-22 14:13:20

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

最简单的方法是,不使用写输出,而是构建一个字符串作为你的 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 

I would say you have 2 options that should be fairly easy:

The simplest way would be, instead of using write-output, build up a string as your 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

Or - you could build up your output as a psobject and then try one of the PowerShell cmdlets to output it as XML. Probably the more robust way to do it, since you can then use other PowerShell cmdlets via the pipeline. That would look something like this:

$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 
一杆小烟枪 2024-12-22 14:13:20

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

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

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

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

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

If you want to output the objects to XML, you can try the Export-Clixml command. For example :

dir | Export-Clixml c:\temp\output.xml

If you want directly an HTML page, you can use the commande ConvertTo-Html :

dir | ConvertTo-Html > c:\temp\output.html

You can customize the later command with head, title, body and CSS.

暮凉 2024-12-22 14:13:20

以下是 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 文档

Here is an update to iHunger's answer:

$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)

See Reading, updating and writing XML document with PowerShell

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文