2

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 !

3 Answers 3

4

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 
Sign up to request clarification or add additional context in comments.

1 Comment

out-file does seems to be writing the xml object signature, not the actual xml object
1

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.

1 Comment

Really good code if one wanted to proof that xml is - contrary to what they say - not human readable/writable.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.