Date and Month display

Get Calculated columns details with HTML tags across Web Application Level

<#
******-----------------------------------------------------------------------******
Author          -> Shiv Mangal Singh
Date            -> 25 th July - 2017
Description     -> This Script will generate the calculated columns details with HTML tags across List for all SharePoint 2013 site collection at WebApplication Level

Path of csv file -->$FileUrl = "D:\contosoMW_Team\Shiv\PS\UsersPermission\CalculatedColumns_Report_WebApplication_24jul17_ABC.CSV"
                    $currentLogPath ="D:\contosoMW_Team\Shiv\PS\UsersPermission\logerror_24jul17_ABC.txt"
Tenant Site URL  --> $WebAppURL = "https://ABC.MS.contoso.net/"

******-----------------------------------------------------------------------******
 #>
 
Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
function GetSPFieldDetailsForAllLists($WebAppURL)
{
############################*************************** Creating Header TAB for Report ##########**********************************************

#Logs and prints error messages
function LogError([String] $ErrorMessage, [String]$ErrorDetails, [String]$ErrorPosition)
{
    Write-Host $ErrorMessage -foregroundcolor red
    $fullErrorMessage = $ErrorMessage + $ErrorDetails + ". " + $ErrorPosition
    Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") ERROR: $fullErrorMessage" | Out-File -FilePath $currentLogPath -Append -Force
}
#Logs and prints messages
function LogMessage([String] $Msg)
{
    Write-Host $Msg -ForegroundColor Green
    Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") Message: $Msg" | Out-File -FilePath $currentLogPath -Append -Force
}
$FileUrl = "D:\contosoMW_Team\Shiv\PS\UsersPermission\CalculatedColumns_Report_WebApplication_24jul17_ABC.CSV"
$currentLogPath ="D:\contosoMW_Team\Shiv\PS\UsersPermission\logerror_24jul17_ABC.txt"
"Site Url `t Site Owner `t Owner Email `t List URL `t List `t Field Name `t Field Type `t HTML Tag " | Out-File $FileUrl
    #$site = new-object Microsoft.SharePoint.SPSite($SiteCollectionURL) #Change site URL#
    $SiteCollections  = Get-SPSite -Webapplication $WebAppURL -Limit All
    #Loop throuh all Site collection
    foreach($Site in $SiteCollections)
     {
      try
      {
    # Looping through Each sites
            foreach($web in $Site.AllWebs)
             {
           
              try
                {
                #
                $SiteOwner =@()
                $OwnerEmail =@()
                foreach($group in $web.Groups)
                    {
                        if($group.Name -like "*Owners*")
                        {
                            foreach($user in $group.Users)
                            {
                            $SiteOwner += $user.Name + ";"
                            $OwnerEmail+=$user.Email + ";"
                            }
                        }
                    }
                #
                #Write-Host $web.Url `t $web.SiteAdministrators -ForegroundColor Cyan
             $requiredlists=$web.Lists | where{$_.Hidden -eq $false -or $web.Lists -ne $null}

               foreach ($list in $requiredlists) #Get all list in web
                {
                $requiredFields = $list.Fields | where {$_.Hidden -eq $false -or $list.Fields -ne $null}
                foreach ($field in $requiredFields) #Get all fields in lists
                    {
                        if(($field.Type -eq "Calculated") -and ($field.hidden -eq $false) -and ($field.formula -like "*<*>*</*>*") -or ($field.formula -like "*<*>*"))
                            {
                         $fieldFormula=($field.formula).replace("`n", "")
                            Write-Host "List Name: " $list.Title  ##Print List title
                            Write-Host "------------------------------------------------------"
                            Write-Host "Field Name | Field Title " -ForegroundColor DarkGreen
                            Write-Host "------------------------------------------------------"
                            Write-Host $field.Type `t $field.Title `t $fieldFormula -ForegroundColor Green                        
        #Send the Data to CSV file
        "$($web.Url) `t $($SiteOwner) `t $($OwnerEmail) `t $($list.ParentWeb.Url)/$($list.RootFolder.Url) `t $($list.Title) `t $($field.Title) `t $($field.Type) `t $($fieldFormula)" | Out-File $FileUrl -Append
                            }
                    } #$field foreach loop closing
         
                } #$list foreach closing loop
                $web.Dispose()
        } #$web foreach closing loop
       # try loop
        catch
        {
        write-host "for web catch" $web.Url -ForegroundColor Red
         # Exception handling in log file
                if($_.Exception.Message -like '*(401) Unauthorized*' -or $_.Exception.Message -like '*Access denied*')
                {
                    LogMessage("You need permission to access this site: "+ $web.Url)
                }
               elseif($_.Exception.Message -like '*List does not exist' -or $_.Exception.Message -like '*Invalid file name')
                {
                    LogMessage("You need permission to access this site or list does not exist or Invalid file name: "+$requiredlists +$requiredFields)
                }
                else
                {
                LogError $_.Exception.Message $_.Exception.GetType().FullName $_.InvocationInfo.PositionMessage
                }
        }
        finally
         {
                 $web.Dispose()
          }
        }
     
    }
    catch
    {
      if($_.Exception.Message -like '*(401) Unauthorized*' -or $_.Exception.Message -like '*Access denied*')
                {
                    LogMessage("You need permission to access this site: "+ $Site.Url)
                }
                else
                {
                    LogError $_.Exception.Message $_.Exception.GetType().FullName $_.InvocationInfo.PositionMessage
                }
    }
    finally
    {
      $Site.Dispose()
    }
    }#$Site collection foreach closing loop
 }
 # calling function and provided Web Application URL as a parameter
GetSPFieldDetailsForAllLists "https://ABC.MS.contoso.net/"

No comments:

Post a Comment