alex
alex

Reputation: 976

Writing xml with powershell

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 !

Upvotes: 2

Views: 15345

Answers (3)

user8128167
user8128167

Reputation: 7696

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

Upvotes: 0

iHunger
iHunger

Reputation: 83

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 

Upvotes: 4

Thierry Franzetti
Thierry Franzetti

Reputation: 1863

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.

Upvotes: 1

Related Questions