Powershell Scripts 2
Lets see continuation of my previous PowerShell series. This time will start from the basic commands.
Load Assembly references in PowerShell
Load Assembly references in PowerShell
Declare Global Variables in PowerShell
Pass/Read Command Line arguments
Convert DateTime based on regional settings in PowerShell
Read RESX value in PowerShell
Perform CAML Query in PowerShell
Add SharePoint Snapin
if((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue }
Load default/custom Assembly references
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Net") [void][System.Reflection.Assembly]::LoadWithPartialName("System.IO") [void][System.Reflection.Assembly]::LoadWithPartialName("System.Xml") [void][System.Reflection.Assembly]::LoadWithPartialName("System.Collections.Generic") [void][System.Reflection.Assembly]::LoadWithPartialName("System.Globalization") [void][System.Reflection.Assembly]::LoadWithPartialName("System.Resources.ResXResourceSet") [void][System.Reflection.Assembly]::LoadWithPartialName("MyCompany.CustomDLL")
Declare global variables
We should declare value with $global to assign value from functions
[string]$global:SmtpServer = $null
Pass Command line arguments
# PowerShell File Path $PSPath = "C:\Rathanavel\Workouts\PS\CopyItems.ps1" # Argument values $Url = "https://site.domain.com/sites/sitecoll/web" $Name = "Communications" $Cols = "Title;Description;Editor..." $Id = "143" $argList = [System.String]::Format("{0} -WebUrl {1} -ListName {2} -Columns {3} -ID", $Url, $Name, $Cols, $Id) Start-Process Powershell.exe -Argumentlist ($PSPath + "-file " + $argList)
Read Command line arguments
param ( [string]$WebUrl, [string]$ListName, [string]$Columns, [string]$ID ) function ReadCMDLineArgValues() { Write-Host([string]::Format("Open Web: {0} and List: {1} getbyid: {2} and read these columns: {3}", $WebUrl, $ListName, $ID, $Columns)) }
Get Publishing web
$oWeb = Get-SPWeb "https://site.domain.com/sites/sitecoll/web" if([Microsoft.SharePoint.Publishing.Publishingweb]::IsPublishingWeb($oWeb)) { $oPubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]:: GetPublishingWeb($oWeb) $oList = $oPubweb.PagesList }
Read RESX file
$ApprovedText = [Microsoft.SharePoint.Utilities.SPUtility]::GetLocalizedString("`$Resources:core,402", "core", $oWeb.Language) ## Import this assembly before you begin # [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.ShrePoint.Utilities.SPUtility")
Convert DateTime based on regional settings
$oWeb = Get-SPWeb "https://site.domain.com/sites/sitecoll/web" Write-Host $oWeb.RegionalSettings.TimeZone.UTCToLocalTime((Get-Date).ToUniversalTime())
Use CAMLQuery
function QueryYesterdayItems() { $oWeb = Get-SPWeb "https://site.domain.com/sites/sitecoll/web" if([Microsoft.SharePoint.Publishing.Publishingweb]::IsPublishingWeb($oWeb)) { $oPubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]:: GetPublishingWeb($oWeb) $oList = $oPubweb.PagesList $CreatedStartDate = (Get-Date).AddDays(-1).ToString("yyyy-MM-dd") + "T00:00:00Z" $CreatedEndDate = (Get-Date).AddDays(-1).ToString("yyyy-MM-dd") + "T23:59:59Z" $CamlQuery = New-Object Microsoft.SharePoint.SPQuery if($oList.EnableModeratio -eq $true) { $ApprovedText = [Microsoft.SharePoint.Utilities.SPUtility]::GetLocalizedString("`$Resources:core,402", "core", $oWeb.Language) $CamlQuery.Query = "<Where> <And> <And> <Geq> <FieldRef Name='Created' /> <Value Type='DateTime' IncludeTimeValue='TRUE'>$CreatedStartDate</Value> </Geq> <Leq> <FieldRef Name='Created' /> <Value Type='DateTime' IncludeTimeValue='TRUE'>$CreatedEndDate</Value> </Leq> </And> <Eq> <FieldRef Name='_ModerationStatus' /> <Value Type='ModStat'>$ApprovedTxt</Value> </Eq> </And> </Where> <OrderBy><FieldRef Name='Modified' Ascending='False'/></OrderBy>" } else { $CamlQuery.Query = "<Where> <And> <Geq> <FieldRef Name='Created' /> <Value Type='DateTime' IncludeTimeValue='TRUE'>$CreatedStartDate</Value> </Geq> <Leq> <FieldRef Name='Created' /> <Value Type='DateTime' IncludeTimeValue='TRUE'>$CreatedEndDate</Value> </Leq> </And> </Where> <OrderBy><FieldRef Name='Modified' Ascending='False'/></OrderBy>" } $CamlQuery.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Description' />..." $CamlQuery.ViewFieldsOnly = $true $ListItemCollection = $oList.GetItems($CamlQuery) if($ListItemCollection -ne $null) { foreach($item in $ListItemCollection) { #your logic goes here... } } } }
Will see more commands and scenarios in the upcoming series.
-Ratsub
Related Articles:
-Ratsub
Comments
Post a Comment
Enter your comments..