none
Powershell scripts not writing it to output file RRS feed

  • Question

  • Hi,

    Below is my script which loops through all sites and web in a site collection and if the particular web part is added in a page, it will give the page URL and it should be written to a output file.

    function enumerateWebParts($Url)
     {
     $webApp = Get-SPWebApplication $Url  #Get WebApplication URL
     
    foreach($web in $webApp | Get-SPSite -Limit All | Get-SPWeb -Limit All) #foreach loop to iterate through all sites and webs of WebApplication
     {
     if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) #Check if site is a publishing site
     {
     $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
     $pages = $pWeb.PagesList
     foreach ($item in $pages.Items)
     {
     $fileUrl = $webUrl + "/" + $item.File.Url
    
     $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
      
     $wps = $manager.webparts
     foreach ($type in $wps) #loop through each type found $wps
     {
    #Write-Host " type searching";
    Write-Host $type.GetType().Name;
     if ($type.GetType().Name -eq "RightMenu") #if webpart $type -like KWizCom select webpart object
     {
    Write-Host "found";
     $wps | select-object @{Expression={$pWeb.Url};Label="Web URL"},@{Expression={$fileUrl};Label="Page URL"},DisplayTitle, IsVisible, @{Expression={$type};Label="Type"}
    Write-Host $fileUrl;
    $page=$pweb.Url+$fileUrl;
    Write-Output $page;
     }
     }
     }
     }
    
     else #if web is not a publishing site
     {
     $pages = $null
     $pages = $web.Lists["Site Pages"]
     if ($pages)
     {
     foreach ($item in $pages.Items)
     {
     $fileUrl = $webUrl + "/" + $item.File.Url
    
     $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
     $wps = $manager.webparts
     foreach ($type in $wps)
     {
     if ($type.GetType().Name -eq "RightMenu")
     {
     $wps | select-object @{Expression={$Web.Url};Label="Web URL"},@{Expression={$fileUrl};Label="Page URL"},DisplayTitle, IsVisible, @{Expression={$type};Label="Type"}
    Write-Host $fileUrl;
    $page=$pweb.Url+$fileUrl;
    Write-Output $page;
     }
     }
     }
     }
     else
     {
     }
     }
     # Write-Host “… completed processing” $web
     }
     }
    
    $row = enumerateWebParts(‘http://sptest.danfoss.net’) #call the enumerateWebParts function pass it the URL for the web application
    
    
    
     
     
    

    I run the command through D:\RightMenu.ps1 | Out-File D:\RightMenu.txt

    Aruna

    Wednesday, June 18, 2014 11:29 AM

Answers

  • Try 

    http://stackoverflow.com/questions/10340007/exporting-powershell-output-to-text-file

    You can always redirect the output an exe to a file like so (even from cmd.exe):

    powershell c:\test.ps1 > c:\test.log
    

    Within PowerShell, you can also redirect individual commands to file but in those cases you probably want to append to the log file rather than overwrite it e.g.:

    $logFile = 'c:\temp\test.log'
    "Executing script $($MyInvocation.MyCommand.Path)" > $logFile
    foreach ($proc in Get-Process) {
        $proc.Name >> $logFile
    }
    "Another log message here" >> $logFile
    


    If this helped you resolve your issue, please mark it Answered

    • Marked as answer by JasonGuo Sunday, June 29, 2014 8:42 AM
    Wednesday, June 18, 2014 12:21 PM
    Moderator