Feeds:
Posts
Comments

Posts Tagged ‘PowerShell’

To have http redirects in a sharepoint we can use something like this in the web.config file

<location path=”oldpage1.htm”>
<system.webServer>
<httpRedirect enabled=”true” destination=”newpage1.html” exactDestination=”true” httpResponseStatus=”Permanent” />
</system.webServer>
</location>

One simple way of doing it using PowerShell is to use the SPWebConfigModication object.

The following script can achieve that

 

$Owner = “redirectMod”
$webApp = Get-SPWebApplication $Url

 

$modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$modification.Path = “configuration”;
$modification.Name = “redirect-1” ;
$modification.Sequence = 0;
$modification.Owner =$Owner;
$modification.Type = 0 # EnsureChildNode;
$modification.Value = “<location path='”+$from+”‘><system.webServer><httpRedirect enabled=’true’ destination='”+$to+”‘ exactDestination=’true’ httpResponseStatus=’Permanent’ /></system.webServer></location>”;
$webApp.WebConfigModifications.Add($modification);

$webApp.Update()
$webApp.Parent.ApplyWebConfigModifications()
do{
Start-Sleep -Seconds 2;
Write-Host “.” -NoNewline
$configJobRunning = IsWebConfigModificationJobPendingOrRunning $webApp;
}while ($configJobRunning);

 

Read Full Post »

If we need to add some URL rewrite to a SharePoint site, we can do that using some web.config settings like :

<rewrite>
<rewriteMaps>
<rewriteMap name=”Redirects”>
<add key=”/Page1.html” value=”/Page1″ />
<add key=”/Page2.html” value=”/Page2″ />
</rewriteMap>
</rewriteMaps>
</rewrite>

One way to do that Is to use SharePoint Powershell commands.

The following script achieves that:
[Microsoft.SharePoint.Administration.SPWebConfigModification] $modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$modification.Path = “configuration/system.webServer”;
$modification.Name = “rewrite”;
$modification.Sequence = 0;
$modification.Owner =$Owner;
$modification.Type = 0 # EnsureChildNode;
$modification.Value = “”;
$webApp.WebConfigModifications.Add($modification);

$modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$modification.Path = “configuration/system.webServer/rewrite”;
$modification.Name = “rewriteMaps”;
$modification.Sequence = 0;
$modification.Owner =$Owner;
$modification.Type = 0 # EnsureChildNode;
$modification.Value = “”;
$webApp.WebConfigModifications.Add($modification);

$modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$modification.Path = “configuration/system.webServer/rewrite/rewriteMaps”;
$modification.Name = “rewriteMap”;
$modification.Sequence = 0;
$modification.Owner =$Owner;
$modification.Type = 0 # EnsureChildNode;
$modification.Value = “”;
$webApp.WebConfigModifications.Add($modification);

$modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$modification.Path = “configuration/system.webServer/rewrite/rewriteMaps/rewriteMap”;
$modification.Name = “redirect-1”;
$modification.Sequence = 0;
$modification.Owner =$Owner;
$modification.Type = 0 # EnsureChildNode;
$modification.Value = “”;
$webApp.WebConfigModifications.Add($modification);

$modification = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$modification.Path = “configuration/system.webServer/rewrite/rewriteMaps/rewriteMap”;
$modification.Name = “redirect-2”;
$modification.Sequence = 0;
$modification.Owner =$Owner;
$modification.Type = 0 # EnsureChildNode;
$modification.Value = “”;
$webApp.WebConfigModifications.Add($modification);

$webApp.Update()
$webApp.Parent.ApplyWebConfigModifications()

do{
Start-Sleep -Seconds 2;
Write-Host “.” -NoNewline
$configJobRunning = IsWebConfigModificationJobPendingOrRunning $webApp;
}while ($configJobRunning);

References:

https://blog.kenaro.com/2010/09/02/add-web-config-modification-with-powershell-spwebconfigmodification/ 

 https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module 

Read Full Post »

When running Enable-SPFeature, you may get the weird error:

“Enable-SPFeature : The file is currently checked out or locked for editing by another user.”

 

One possible cause of this issue could be that some file(s) the are deployed by the feature could be checked out by a user.

Checking the ULS logs you could find something like

 

Instantiating module “NAMEOFMODULE”: File could not be added at URL “FILENAME.jpg: Error 689225296 99e77593-d0d4-49df-adcc-c8f8251fbb82
04/06/2017 20:53:42.02 PowerShell.exe (0x107C) 0x1334 SharePoint Foundation Monitoring b4ly High Leaving Monitored Scope (SPSqlClient). Execution Time=11589.5652 99e77593-d0d4-49df-adcc-c8f8251fbb82
04/06/2017 20:53:42.02 PowerShell.exe (0x107C) 0x1334 SharePoint Foundation General 8kh7 High The file is currently checked out or locked for editing by another user.<nativehr>0x80070021</nativehr><nativestack></nativestack> 99e77593-d0d4-49df-adcc-c8f8251fbb82
04/06/2017 20:53:42.03 PowerShell.exe (0x107C) 0x1334 SharePoint Foundation General aix9j High SPRequest.EnableModuleFromXml: UserPrincipalName=i:0).w|s-1-5-21-995927836-1113283497-4239367599-17896, AppPrincipalName= ,bstrSetupDirectory=C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\Template ,bstrFeatureDirectory=Features\ABCDEP ,bstrUrl=,bstrXML=<Module Name=”images” Path=”Style Library\images” Url=”Style Library/images” RootWebOnly=”TRUE” xmlns=”http://schemas.microsoft.com/sharepoint/”><File ReplaceContent=”True” Type=”GhostableInLibrary” Path=”favicon.ico” Url=”favicon.ico” /><File ReplaceContent=”True” Type=”GhostableInLibrary” Path= ,fForceUnghost=False ,pModuleContext=<null> 99e77593-d0d4-49df-adcc-c8f8251fbb82
04/06/2017 20:53:42.03 PowerShell.exe (0x107C) 0x1334 SharePoint Foundation General ai1wu Medium System.Runtime.InteropServices.COMException: The file is currently checked out or locked for editing by another user.<nativehr>0x80070021</nativehr>

Read Full Post »

Here is a simple script to create the site structure for SharePoint Online based on a csv file.

The script is using the Client-side SharePoint PowerShell project from Codeplex (https://sharepointpowershell.codeplex.com/ ).

 

In order to create the site structure just fill the csv file of the following format

Url,Name,Template,Description,Language
Blog,Blog,BLOG#0,Blog,1033
Subsite1, SubSite1, STS#0, Name1, 1033
SubSite1/Subsite11, Name11, STS#0, Description, 1033
SubSite1/Subsite11/Subsite1111, Name3, STS#0, Description, 1033

 

And then use the following script

 

Import-Module "$PSScriptRoot\spps.psm1"
Import-Module "$PSScriptRoot\spps.subsites.psm1"
Import-Module "$PSScriptRoot\ConfigCredentials.ps1"

Initialize-SPPS -siteURL $O365SiteUrl -isOnline $true -onlineusername $username -onlinepassword $password

Import-Csv $PSScriptRoot\SiteStructure.csv  | ForEach-Object{

    $name = $_.Name
    $template = $_.Template
    $description = $_.Description
    $lang = $_.Language
   
    Write-Host "Processing " $name " "  $_.Url
    Open-Rootsite
   
    $relUrl = ""   
    foreach($subsite in $_.Url -split "/")
    {
        $relUrl += "/"+ $subsite
        try
        {
            Open-Subsite -relativeUrl $relUrl
            Write-Host "Site" [$relUrl] "Already exists"
        }
        catch
        {
            Write-Host "Trying to create" $subsite
            Add-Subsite -title $name -url $subsite -webTemplate $template -language $lang -description $description
        }
    }
    Write-Host " ———————– "
}

 

Note, the Script ConfigCredentials.ps1 is used to store the Site Url and the credentials as follows

$O365SiteUrl = “https://url_here
$username = username@domain.com
$password = "password"

 

Note: to make sure the script behaves as expected, make sure that the hierarchy in the csv file is in order. i.e.

Subsite1
Subsite1/Subsite2

in other words do no list the sub site before its parent site.

Read Full Post »

$webAppName = “Contoso Internet Site XYZ”
$webAppUrl = “http://www.contoso.com&#8221;
$webAppPort = 80
$webAppHostHeader = “sharepoint.contoso.com”
$membershipProviderName = “OAMMembershipProvider”
$roleProviderName = “OAMRoleProvider”
$appPoolName = “ContosoAppPool”
$contentDBName = “Contoso”
$appPoolUser =  “DE\sp_AppPool”

Write-Host “Deleting Web application $webApp if it exists…”
Remove-SPWebApplication -Identity $webAppUrl -Confirm:$false -Verbose -DeleteIISSite:$true -RemoveContentDatabases:$true -ErrorAction SilentlyContinue

$formsAuthProvider = New-SPAuthenticationProvider -ASPNETMembershipProvider $membershipProviderName -ASPNETRoleProviderName $roleProviderName
$windowsAuthProvider = New-SPAuthenticationProvider
$AuthProvidersArray = $formsAuthProvider, $windowsAuthProvider

New-SPWebApplication -Name $webAppName -Port $webAppPort -HostHeader $webAppHostHeader -URL $webAppUrl -ApplicationPool $appPoolName -ApplicationPoolAccount (Get-SPManagedAccount $appPoolUser) -AllowAnonymousAccess -AuthenticationProvider $AuthProvidersArray -AuthenticationMethod NTLM -Verbose -DatabaseName $contentDBName  -ErrorVariable $ev
if($ev -ne $null)
{
    Write-Host “Web Application $webApp created successfully”
}

$webApp = get-SPWebApplication $webAppUrl
$webApp.UseClaimsAuthentication = $true
$webApp.Update()
Write-Host “Updated Web application to use claims based authentication for $webApp..”

Read Full Post »