se autoindent
se shiftwidth=4
se softtabstop=4
se expandtab
se nowrapscan
se guifont=Lucida_Console:h9:cDEFAULT
se columns=160
se lines=45
se cursorline
se showcmd
se showmatch
syntax on
set backup
set backupdir=C:\WINDOWS\Temp
set backupskip=C:\WINDOWS\Temp\*
set directory=C:\WINDOWS\Temp
set writebackup
#=============================================================================
# Tests whether relevant statements in the specified
# SQL adhere to four-part naming conventions.
#=============================================================================
param (
[string]$filename = $(throw "-filename is required."),
[ValidateSet("MINT", "QA1", "QA2", "PROD")]
[string]$targetEnvironment = $(throw "-targetEnvironment is required.")
)
Set-StrictMode -Verbose -Version 1.0
function load-mintTransforms
{
$transforms.Add("\[server\\blah\]\.\[SIDE\]\.\[DBO\]\.\[SIDE_", "[server\\blah2].[SIMT].[DBO].[SIMT_")
$transforms.Add("\[server\\blah\]\.\[SIDE\]\.\[DBO\]\.", "[server\\blah2].[SIMT].[DBO].")
$transforms.Add("\[server\\blah\]\.\[SIDE_IMT\]\.\[STEMS_INTERFACE\]\.", "[server\\blah2].[SIMT_IMT].[STEMS_INTERFACE].")
}
function load-qa1Transforms
{
$transforms.Add("\[server\\blah\]\.\[SIDE\]\.\[DBO\]\.\[SIDE_", "[SITSQA1LISTHA\\blah2,4901].[SIQA].[DBO].[SIQA_")
$transforms.Add("\[server\\blah\]\.\[SIDE\]\.\[DBO\]\.", "[SITSQA1LISTHA\\blah2,4901].[SIQA].[DBO].")
$transforms.Add("\[server\\blah\]\.\[SIDE_IMT\]\.\[STEMS_INTERFACE\]\.", "[server\\blah2].[SIQA_IMT].[STEMS_INTERFACE].")
}
function load-qa2Transforms
{
$transforms.Add("\[server\\blah\]\.\[SIDE\]\.\[DBO\]\.\[SIDE_", "[server\\blah2].[SIQA].[DBO].[SIQA_")
$transforms.Add("\[server\\blah\]\.\[SIDE\]\.\[DBO\]\.", "[server\\blah2].[SIQA].[DBO].")
$transforms.Add("\[server\\blah\]\.\[SIDE_IMT\]\.\[STEMS_INTERFACE\]\.", "[server\\blah2].[SIQA_IMT].[STEMS_INTERFACE].")
}
function load-prodTransforms
{
$transforms.Add("\[server\\blah\]\.\[SIDE\]\.\[DBO\]\.\[SIDE_", "[SITSPDLISTHA\\blah2,4901].[SIPR].[DBO].[SIPR_")
$transforms.Add("\[server\\blah\]\.\[SIDE\]\.\[DBO\]\.", "[SITSPDLISTHA\\blah2,4901].[SIPR].[DBO].")
$transforms.Add("\[server\\blah\]\.\[SIDE_IMT\]\.\[STEMS_INTERFACE\]\.", "[SITSPDLISTHA\\blah2].[SIPR_IMT].[STEMS_INTERFACE].")
}
function load-transforms
{
switch ($targetEnvironment)
{
"MINT" { load-mintTransforms }
"QA1" { load-qa1Transforms }
"QA2" { load-qa2Transforms }
"PROD" { load-prodTransforms }
}
}
$transforms = [ordered]@{}
load-transforms
foreach ($line in (get-content $filename))
{
foreach ($transform in $transforms.GetEnumerator())
{
$search = $($transform.Name)
$replace = $($transform.Value)
$line = $line -replace $search,$replace
}
$line
}
#=============================================================================
# Tests whether relevant statements in the specified
# SQL adhere to four-part naming conventions.
#=============================================================================
param (
[string]$filename = $(throw "-filename is required."),
[switch]$inlineFilename = $false
)
Set-StrictMode -Verbose -Version 1.0
function line-usesFourPartNaming($statement)
{
# Optional square brackets on all but the first element of four-part names
$line -match $statement + "\s*\[.*\]\.\[*.*\]*\.\[*.*\]*\.\[*.*\]*.*$"
}
function process-from
{
# Ignore anything relating to temp tables
if ($line -match "\b(from|join)\b\s+(#|@)(tmp|usr)") { return "" }
# Ignore anything relating to sub-selects
if ($line -match "\b(from|join)\b\s+\(") { return "" }
# Ignore anything selecting from openxml
if ($line -match "\b(from|join)\b\s+openxml\s*\(") { return "" }
# Ignore DELETE FROM (these are picked up separately)
if ($line -match "\bdelete\b\s+\bfrom\b") { return "" }
# Issue a warning for openquery statements
if ($line -match "\bfrom\b\s+openquery\s*\(")
{
return "Warning"
}
if (line-usesFourPartNaming $searchPatterns.Get_Item("from")) {
return ""
} else {
return "Error"
}
}
function process-update
{
# Ignore anything relating to temp tables
if ($line -match "\bupdate\b\s+(#|@)(tmp|usr)") { return "" }
if (line-usesFourPartNaming $searchPatterns.Get_Item("update")) {
return ""
}
if ($line -match "\bupdate\b\s*$") {
return "Warning"
} else {
return "Error"
}
}
function process-delete
{
# Ignore anything relating to temp tables
if ($line -match "\bdelete\b\s+\bfrom\b\s+(#|@)(tmp|usr)") { return "" }
if (line-usesFourPartNaming $searchPatterns.Get_Item("delete")) {
return ""
}
if ($line -match "\bdelete\b\s*$") {
return "Warning"
} else {
return "Error"
}
}
function process-ddl
{
# Ignore anything relating to temp tables
if ($line -match "\b(alter|create|drop)\b\s+\btable\b\s+(#|@)(tmp|usr)") { return "" }
if (line-usesFourPartNaming $searchPatterns.Get_Item("ddl")) {
return ""
} else {
return "Error"
}
}
function process-grant
{
# Automatically issue a warning for all GRANT statements
"Warning"
}
$searchPatterns = @{}
$searchPatterns.Add("from", "\b(from|join)\b")
$searchPatterns.Add("update", "\bupdate\b")
$searchPatterns.Add("delete", "\bdelete\b\s+\bfrom\b")
$searchPatterns.Add("ddl", "\b(alter|create|drop)\b\s+\b(constraint|index|procedure|table|view)\b")
$searchPatterns.Add("grant", "\bgrant\b\s+(\ball\b\s)*(\bprivileges\b\s)*(\b[A-z]+\b\s)*(\(\b[^\)]+\b\)\s)*\bon\b.*$")
$lineNumber = 0
$inlineText = [String]::Empty
$issueFound = ""
if ($inlineFilename) { $inlineText = "{0}`: " -f $filename }
else { "Filename: '$filename'" }
foreach ($line in (get-content $filename))
{
$lineNumber++
# Ignore commented-out lines
if ($line -match "^\s*(--|#|\/\*)") { continue }
# Ignore 'THROW' statements
if ($line -match "^\s*throw\b") { continue }
if ($line -match $searchPatterns.Get_Item("from")) { $issueFound = process-from }
if ($line -match $searchPatterns.Get_Item("update")) { $issueFound = process-update }
if ($line -match $searchPatterns.Get_Item("delete")) { $issueFound = process-delete }
if ($line -match $searchPatterns.Get_Item("ddl")) { $issueFound = process-ddl }
if ($line -match $searchPatterns.Get_Item("grant")) { $issueFound = process-grant }
if ($issueFound)
{
"{0}{1} at line {2}`: {3}" -f $inlineText, $issueFound, $lineNumber, ($line -replace '^\s*', '')
$issueFound = ""
}
}
$sc = Get-SPWeb "http://host/type/year/mod"
$ann = $sc.Navigation.QuickLaunch[0]
$ann.Title = "Staff Announcements"
$ann.Update()
$ann = $sc.Navigation.QuickLaunch[0]
$server=$env:COMPUTERNAME|%{$_ -replace ".*-",""}
$dumpdir="\\unc\path"
gci 'c:\Program Files (x86)\*'|foreach-object {$_.FullName}|out-file -encoding ASCII $dumpdir\${server}_c_progfilesx86.txt
gci 'c:\Program Files\*'|foreach-object {$_.FullName}|out-file -encoding ASCII $dumpdir\${server}_c_progfiles.txt
gci 'd:\Program Files\*'|foreach-object {$_.FullName}|out-file -encoding ASCII $dumpdir\${server}_d_progfiles.txt
Get-ChildItem -filter *.ps1 -Recurse|ForEach-Object {write-host $_.FullName }
<data>
<config>
<server>YourServerName</server>
</config>
<pageModification>
<pageName>Pages/Home3.aspx</pageName>
<webTemplate>VLE3LEARNINGSPACE</webTemplate>
<webPartType>YourWebPartType</webPartType>
<webPartTitle>YourWebPartName</webPartTitle>
<webPartZoneName>RightColumn</webPartZoneName>
<webPartZoneIndex>1</webPartZoneIndex>
<checkOutComment>{0} added to page by an automated script</checkOutComment>
<learningSpaces>
<learningSpace>spweb.Url<learningSpace>
</learningSpaces>
<siteCollections>
<siteCollection>SPSite.Url</siteCollection>
<siteCollection>SPSite.Url</siteCollection>
</siteCollections>
</pageModification>
</data>
Logging from Simple PowerShell Script Logging
Adding WebPart to page from Add WebPart to a web with PowerShell
Checking out pages from Using PowerShell script update the page name
[void][reflection.assembly]::LoadWithPartialName()
[xml] $xdoc = Get-Content .\Add-WebPartToPage.xml
$logfile = ".\Add-WebPartToPage_$(get-date -format `"yyyyMMdd_HHmmss`").txt"
Function ProcessLearningSpace($learningSpace)
{
$web = Get-SPWeb $learningspace
$page = $web.GetFile($homepage)
if ($web.WebTemplate -ne $webTemplate)
{
log "$wpType NOT added to $learningSpace, This is not a $webTemplate site" red
return
}
if (!(IsPublishingPage($web)))
{
log "adding $wpType NOT added to $learningSpace, This is not a publishing page" red
return
}
if (IsWebPartOnPage($web))
{
log "$wpType NOT added to $learningSpace, It is already present" red
return
}
if ($page.CheckOutType -eq "None" -And $page.LockType -eq "None")
{
log "$wpType added to $learningSpace," white
AddWebPartToPage
}
else
{
log "Not adding $wpType to $learningSpace, Page is checked out or locked" red
return
}
}
Function Log($string, $colour)
{
if ($colour -eq $null)
{
$colour = "white"
}
write-host $string -ForeGroundColor $colour
$string | Out-File -Filepath $logfile -append
}
Function IsPublishingPage($web)
{
return [Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)
}
Function IsWebPartOnPage($web)
{
$retValue = $False
$pweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pages = $pweb.PagesList
foreach ($item in $pages.Items)
{
$fileUrl = $tutorSpace + '/' + $item.File.Url
$webpartmanager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared)
$wps = $webpartmanager.webparts
foreach ($webpart in $wps)
{
if ($webpart.GetType().ToString() -eq $wpType)
{
$retValue = $True
break
}
}
$webpartmanager.Dispose()
}
return $retValue
}
Function AddWebPartToPage()
{
$page.Checkout()
$webpart = New-Object $wpType
$webpart.Title = $wpTitle
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::TitleOnly
$webpartmanager = $web.GetLimitedWebPartManager(
$homepage,
[System.web.ui.webcontrols.webparts.personalizationscope]::Shared
)
$webpartmanager.AddWebPart($webpart, $wpZoneName, $wpZoneIndex)
$webpartmanager.Dispose()
$page.CheckIn($comments)
}
Function Main()
{
$server = $xdoc.data.config.server
$pageModification = $xdoc.data.pageModification
$homepage = $pageModification.pageName
$webTemplate = $pageModification.webTemplate
$wpTitle = $pageModification.webPartTitle
$wpType = $pageModification.webPartType
$wpZoneName = $pageModification.webPartZoneName
$wpZoneIndex = $pageModification.webPartZoneIndex
$comments = [string]::Format($pageModification.checkOutComment, $wpType)
foreach ($siteCollection in $pageModification.siteCollections.siteCollection)
{
$spSite = Get-SPSite $server$siteCollection
foreach($web in $spSite.AllWebs)
{
ProcessLearningSpace($web.Url)
}
}
foreach ($learningSpace in $pageModification.learningSpaces.learningSpace)
{
$spWeb = Get-SPWeb $server$learningSpace
ProcessLearningSpace($spWeb.Url)
}
}
Main
history|Format-Table CommandLine