Saturday, 4 July 2015

Powershell Get SharePoint Site Collections Size

Please run the below mentioned script but before that create input file of the environment:


File Name:
QAAllSites.csv


Content:
Site   URL
ABC http:\\Webapp1\sites\SC1
DEF  http:\\WebApp1\sites\SC2


Script:




#First steps to pass parameter values


param(
[Parameter(Mandatory = $true, valueFromPipelineByPropertyName = $true )][system.string] $env,
[Parameter(Mandatory = $true, valueFromPipelineByPropertyName = $true)][system.string] $outputFilePath
)


#Check SharePoint snapin is added or not?


$snapin = Get-PSSnapin | Where-Object {$_.Name = ""Microsoft.SharePoint.PowerShell"}
if($snapin -eq $null)
{
     Write-Host "Adding 'Microsoft.SharePoint.PowerShell' snapin..."
      Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}


#create variable to get current date time


$startDate = Get-Date
Write-Host "Script start running.." $startDate -ForegroundColor Yellow




#Create table for binding result


$table = New-Object System.Data.DataTable ["Site Collection Size Table"]
$Title = New-Object System.Data.DataColumn Title,[(system.string)]
$URL = New-Object System.Data.DataColumn URL,[(system.string)]
$Size_In_MB = New-Object System.Data.DataColumn Size_In_MB,[(system.string)]


#Check the environment for which script is going to run(eg: QA, Pre-QA)


if($env -eq "QA")
{
    $FileName = "QAAllSites.csv"
}
elseif($env -eq "Pre-QA")
{
   $FileName = "Pre-QAAllSites.csv"
}
else
{
    Write-Host "Choose the correct environment name!"
    return
}


$getFileContent = Import-csv -path $FileName
$getSites  = $getFileContent.URL


Foreach($getSite in $getSites)
{
           Write-Host "Fetching size details of site..." $getSite -Foreground Green
  try{
           $Site = Get-SPSite -Identity $getSite
            $row = $table.NewRow()
            $row.Title = $Site.RootWeb.Title
            $row.URL = $Site.URL
            $row.Size_In_MB = $Site.usage.storage/1MB
            $table.Rows.Add($row)
            $table.AcceptChanges()
            $Site.Dispose()
}
Catch{
             $_.Exception.Message -ForegroundColor Red
}
}


#Export result


$table | Export-csv -path $outputPath -NoTypeInformation


#Get the end date & time of the script


$endDate = Get-Date
Write-Host "Script Completed successfully, Please verify the output file"






##To run the above script pass the command:


.\GetSiteCollectionSizes -env "QA" -outputPath "QAReport.CSV"








###Incase of query, please write to me on Arpit_Mohan@outlook.com

Thursday, 27 November 2014

File cannot be loaded because the execution of scripts is disabled on this system

There two to solve this error :

  • Set policy
  • By pass the policy
  1. Set policy
Set-ExecutionPolicy Unrestricted
or
Set-ExecutionPolicy RemoteSigned

     2. By pass the policy

powershell.exe -ExecutionPolicy ByPass -File script.ps1

Get Bulk Server IPS

  • List all server in sheet and save it by name "InputServerIPs.csv". And name column by name "ServerIPs".
example:
ServerIPs
server1
server2
.
.
.
server


  • Copy below code and paste in notepad and save by name GetServerIPs.ps1
  • Call the script according to environment. ex:
  • .\GetServerIPs.ps1 -env env1,
  • .\GetServerIPs.ps1 -env env2,
  • .\GetServerIPs.ps1 -env production
  • Get output in "OutputServerIPs.csv" in location where your "InputServerIPs.csv" is located.




param
(
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)] [System.String] $env
)
$data = Get-Content "C:\Users\sp_farm\InputServerIPs.csv"
$startDate =Get-Date
Write-host $startDate -ForegroundColor Red
$env1Domain = ".env1.net"
$env2Domain = ".env2.net"
$productionDomain = ".production.net"
if($env -eq "env1")
{
foreach($item in $data)
{
if($item -ne "ServerIPs")
{
try
{
$item
$server = $item + $env1Domain
$serverIP = ping $server
$getServerIP =$serverIP.GetValue(2)
$SpiltbyColon =$getServerIP.Split(":")
$splitbyfrom = $SpiltbyColon[0].Split("m")
$result = $item + ":" + $splitbyfrom[1]
}
catch
{
 $result = $item + ":" + "Not Found"
}
Write-Host $result -ForegroundColor Blue
write-output $result >> "C:\Users\sp_farm\OutputServerIPs.csv"
}
}
}
elseif($env -eq "env2")
{
foreach($item in $data)
{
if($item -ne "ServerIPs")
{
try
{
$item
$server = $item + $env2Domain
$serverIP = ping $server
$getServerIP =$serverIP.GetValue(2)
$SpiltbyColon =$getServerIP.Split(":")
$splitbyfrom = $SpiltbyColon[0].Split("m")
$result = $item + ":" + $splitbyfrom[1]
}
catch
{
 $result = $item + ":" + "Not Found"
}
Write-Host $result -ForegroundColor Blue
write-output $result >> "C:\Users\sp_farm\OutputServerIPs.csv"
}
}
}
else
{
foreach($item in $data)
{
if($item -ne "ServerIPs")
{
try
{
$item
$server = $item + $productionDomain
$serverIP = ping $server
$getServerIP =$serverIP.GetValue(2)
$SpiltbyColon =$getServerIP.Split(":")
$splitbyfrom = $SpiltbyColon[0].Split("m")
$result = $item + ":" + $splitbyfrom[1]
}
catch
{
 $result = $item + ":" + "Not Found"
}
Write-Host $result -ForegroundColor Blue
write-output $result >> "C:\Users\sp_farm\OutputServerIPs.csv"
}
}
}
$endDate = Get-Date
Write-host $endDate -ForegroundColor Green

Wednesday, 26 November 2014

Get time zone + window application

Create two label in form by name ISTTime and CSTTime which show time in IST and CST


{
TimeZone zone = TimeZone.CurrentTimeZone;


 DateTime local = zone.TolocalTime(DateTime.Now);


#get time zone information

TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");


#get offset of the zone information


TimeSpan offset = tzi.GetUtcOffset(DateTime.UtcNow);


#Add offset to universal time
DateTime testDateTime = DateTime.UtcNow.Add(offset);


#display local and universal time in label
ISTTime.Text =local.ToString();


CSTTime.Text=testDateTime.ToString();
}

Tuesday, 25 November 2014

GetFileName + powershell

#get filename in $data variable
$data = get-content "directory:\Filepath\Filename"


#iterate the collection one by one and assign item variable
foreach($item in $data)
{
#get filename in filename variable
   $Filename = [System.IO.Path]::GetFileName($item)


#write output in output file
  write-output >> directory:\Filepath\output.csv
}

Thursday, 20 November 2014

Trust SSL + ServicePointManager.ServerCertificateValidationCallback Property

Trust SSL + ServicePointManager.ServerCertificateValidationCallback Property


Gets or sets the callback to validate a server certificate


An application can set the ServerCertificateValidationCallback property to a method to use for custom validation by the client of the server certificate. When doing custom validation, the sender parameter passed to the RemoteCertificateValidationCallback can be a host string name or an object derived from WebRequest (HttpWebRequest, for example) depending on the CertificatePolicy property.
When custom validation is not used, the certificate name is compared with host name used to create the request. For example, if Create(String) was passed a parameter of "https://www.contoso.com/default.hmtl", the default behavior is for the client to check the certificate against www.contoso.com.




Powershell Script:




$data = get-content InputLogs.csv
foreach($item in $data)
{
try
{
$Filename = [System.IO.Path]::GetFileName($item)


$tofile   = "D:\DownloadFiles\" + $Filename


$webclient = New-Object System.Net.WebClient

[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}


$webclient.UseDefaultCredentials = $true


$webclient.DownloadFile($item, $tofile)
}
catch
{
write-output "$item" >> LogsExceptionCSV.csv

Write-Host Exception at $item and File not found
}
}


reference blogs:
https://social.technet.microsoft.com/Forums/windowsserver/en-US/a260da46-b035-478c-9887-ba5a0a4777a0/powershell-10-trust-any-ssl-cert


http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.servercertificatevalidationcallback(v=vs.110).aspx

Tuesday, 18 November 2014

Datasheet view + multilune text box of type enhance rich text box + SharePoint

Data sheet view with multiline text box column of  type enhanced do not allow you to edit item in datasheet view.


In standard view you are able edit item.


Multiline text box are of three types in SharePoint
  1. Plain
  2. Rich
  3. Enhanced


If you selected multiline text box of first two type then you are able to edit the item in datasheet view.
But if you are going with 3 option ENHANCED then it wont allow you to edit item in datasheet view.


Enhanced multiline text box is read only in Datasheet view.


Its an limitation with SharePoint Out of box features.