Alexander’s Blog

April 1, 2012

PowerShell Script to Find Site Template ID

by @ 1:21 pm. Filed under Scripting, SharePoint, Tips & Tricks

I often create a new site collection based on Microsoft Office SharePoint Server (MOSS) 2007 Collaboration Portal template for my clients’ intranet site. Just so you know, the Collaboration Portal template is hidden in SharePoint Server 2010. I use a PowerShell script to create the top-level site collection using the site template ID (SPSPORTAL#0) for the Collaboration Portal, as documented in my post Installing Collaboration Portal in SharePoint 2010.

Sometimes you may want to create a site based on your own custom site template that you have previously saved. Of course, if the site is a subsite then you can use the GUI to create the site, unless it’s a hidden template. However, if you want to use a script, and there are several situations where you may want to do that, the following script will come handy to create a new site based on a custom site template. Thanks to SharePoint MVP Alex Pierce for this script.

Step 1

Get the site template ID using the following method.

  1. Enter the following text in Notepad.
    $url = “http://www.contoso.com/”
    $site= new-Object Microsoft.SharePoint.SPSite($url )
    $loc= [System.Int32]::Parse(1033)
    $templates= $site.GetWebTemplates($loc)
    foreach ($child in $templates){ write-host $child.Name “  ” $child.Title}

    Replace “http://www.contoso.com/” with your site URL.

  2. Save the file with .ps1 extension, e.g. GetSiteID.ps1.
  3. Run the file in PowerShell.
  4. You will see all the sites and their IDs displayed.
  5. Make a note of your custom site’s ID.

Step 2

Use the method documented in my post Installing Collaboration Portal in SharePoint 2010 to create a new site based on your custom site ID.

February 15, 2012

PowerShell ISE Keyboard Shortcuts

by @ 12:41 pm. Filed under Scripting, Tips & Tricks

I prefer to use PowerShell ISE over the standard Windows PowerShell console. Here are some keyboard shortcuts that you may find helpful. Just make sure that you use the 64-bit version of PowerShell ISE and not the 32-bit. If you use Windows PowerShell ISE in Windows Server 2008 R2 or Windows 7 then you are using the 64-bit version. If you use Windows PowerShell ISE (x86) then you are using the 32-bit version.

PowerShell Command Keyboard Shortcut
New Script (pane) Ctrl + N
Open Script (or other file) Ctrl + O
Save Ctrl + S
Save As Alt +F + A
Run F5
Run Selection F8
New PowerShell Tab Ctrl + T
Close PowerShell Tab Ctrl + W
Close Script Ctrl + F4
New Remote PowerShell Tab Ctrl + Shift + R

For more information on this topic check out this Microsoft TechNet article.

October 15, 2011

SharePoint 2010 Lockdown Mode (ViewFormPagesLockdown)

by @ 9:03 am. Filed under Articles, Scripting, Security/Firewalls, SharePoint, Tips & Tricks

Microsoft Office SharePoint Server (MOSS) 2007 has a feature called ViewFormPagesLockdown, or some people simply refer to it as the SharePoint lockdown feature. Fortunately, the feature also works with SharePoint Server 2010.

The lockdown feature is useful if you have a site collection that is configured for Anonymous access on a Publishing site and you want to lock it down so Anonymous users don’t have access to the Forms page (e.g. http://ServerName/Pages/Forms/AllItems.aspx). You might also be able to take advantage of this feature in another way. For example, if you ever run into an issue on a Publishing Portal configured for Anonymous access where users are unable to post comments (which are stored in a List) on a blog site then the lockdown feature can be disabled, which will result in allowing Anonymous users to post comments. Normally, people won’t have problem posting comments on a blog site unless it is a Publishing site, in which case they will get a prompt to enter user credentials. In such a scenario you can disable the lockdown feature.

NOTE: By default, all publishing sites have the ViewFormPagesLockdown feature enabled.

You can either use stsadm.exe or PowerShell to enable this feature. I prefer to use PowerShell. If you want more detailed information on how to use stsadm.exe, Microsoft’s Tyler Butler has documented it here for MOSS 2007.

With PowerShell, you can easily enable to disable this feature. Here are the instructions.

  1. If you are unsure whether the lockdown is enabled, use the following PowerShell command to find out the answer.
    get-spfeature -site SiteCollectionURL
    e.g. get-spfeature -site http://www.winnetusergroup.com

  2. Look at all the features listed and see if ViewFormPagesLockdown is enabled. If you see it listed then it is enabled, otherwise ViewFormPagesLockdown is disabled.
  3. The lockdown feature can be enabled or disabled. To enable it first run the following command.
    $lockdown = get-spfeature viewformpageslockdown
  4. Now execute the following command to enable it.
    enable-spfeature $lockdown -url SiteCollectionURL
    e.g. enable-spfeature $lockdown -url http://www.winnetusergroup.com



    NOTE
    : To disable the lockdown feature replace the word enable with disable. For example:
    disable-spfeature $lockdown -url SiteCollectionURL

  5. At this point you can verify that the feature is enabled by running the following command. Look for the ViewFormPagesLockdown entry in the list. If it exists, the lockdown feature is enabled.



  6. According to Microsoft, if Anonymous Access is configured for the site then you need to first disable it and then re-enable it. To enable/disable Anonymous Access in SharePoint Server 2010 go to Site Actions, Site Permissions and click Anonymous Access icon on the ribbon.

Copyright ©2011 Zubair Alexander. All rights reserved.

June 13, 2011

Search Server is Not Necessary to Crawl PDF files in SharePoint Foundation 2010

by @ 8:08 am. Filed under Scripting, SharePoint, Tips & Tricks

A lot of blogs and articles on the Internet indicate that in order to crawl PDF documents in SharePoint Foundation 2010 you must install Microsoft Search Server. I want to clear this myth by stating that according to Microsoft, Search Server is not required to crawl PDF files in SharePoint Foundation 2010.

The main problem that people run into is the fact that, unlike WSS 3.0, SharePoint Foundation 2010 does not have an interface to add file extensions for additional file types and iFilters. So how can you crawl additional file types, such as PDFs, in SharePoint Foundation 2010? One easy solution is to use the following VB script. The VB script is available in the KB article 2518465. Here’s the step-by-step procedure.

  1. Copy the following content to notepad and save the file with a .vbs extension. For example, AddExtension.vbs.Sub UsageSub Usage

    WScript.Echo “Usage:    AddExtension.vbs extension”
    WScript.Echo

    end Sub

    Sub Main

    if WScript.Arguments.Count < 1 then
    Usage
    wscript.Quit(1)
    end if

    dim extension
    extension = wscript.arguments(0)

    Set gadmin = WScript.CreateObject(“SPSearch4.GatherMgr.1″, “”)

    For Each application in gadmin.GatherApplications
    For Each project in application.GatherProjects
    project.Gather.Extensions.Add(extension)
    Next
    Next

    End Sub

    call Main

  2. Copy the script to SharePoint Foundation Server and run it at the command prompt. This will add the PDF extension.
    > WScript AddExtension.vbs pdf
  3. Register the PDF iFilter by going to the following registry key.
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0\Search\Setup\ContentIndexCommon\Filters\Extension\.
  4. Right-click the Extensions folder and select New, key.
  5. Enter .pdf for the key name.
  6. In the right-hand pane dobule-click the Default value and enter the following for the Value data:
    {E8978DA6-047F-4E3D-9C78-CDBE46041603}.
  7. Restart SPSearch4 by typing the following at the command prompt:
    net stop spsearch4
    net start spsearch4
  8. Run crawl by typing the following at the command prompt:
    >stsadm –o spsearch –action fullcrawlstart
    The stsadm.exe utility is located in the “14 Hive” folder at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN.
  9. You should now be able to crawl PDF files in SharePoint Foundation 2010.

Note that this method adds the PDF extension. You can use the same technique to add additional filters as necessary.

May 23, 2011

How to Start a Stopped Windows Service Automatically Using a Script

by @ 8:29 pm. Filed under Articles, Scripting, SharePoint, Tips & Tricks, Windows 2008

If you have worked with SharePoint Server 2010, you may have noticed that once in a while, especially after a reboot, the Forefront Identity Manager (FIMService) doesn’t start. Sometimes both the FIMService and the Forefront Identity Manager Synchronization Service (FIMSynchronizationService) don’t start. Without FIM services for all practical purposes your SharePoint is out of commission.

To address this issue, I figured it would be great to write a batch file that will look for these services to see if they are running, if not, it will start these services and log the activity in a log file. If the service is running on multiple computers. this script will even take care of all the remote servers and start the service, assuming you have the proper administrative rights. I want to thank Jerry, a Microsoft MVP – Windows Expert – Consumer, for writing this script on forums.techguy.org. I modified his original script to fit my need.

NOTE: To benefit from the script in this article you must have at least a fundamental knowledge of batch files and scripting. You should also understand what environmental variables are and how to modify the system path.

What Does the Script Do?

I wrote this batch file that will check to see if the two FIM services have started. If not, it will start them automatically. Here’s exactly what the script does. It sets a log file, called log.txt. If the file exists, it will delete it so you only have the information from the last time you ran the script. Instead of using the “net start” command, which will work on the local computer, it uses psservice.exe which is part of Sysinternals Suite and can be downloaded from Microsoft’s Web site here. I wrote the batch file with SharePoint in mind but you can use this script for any service on any Windows computer. In a typical SharePoint environment, SharePoint is installed on more than one server. If you want to make sure that the service is running on multiple computers, simply add the names of the computers in the computers.txt file.

Best Practice: Add the folder where you have copied the Sysinternals tools, such as psservice.exe, to the system’s path. That way you can run the psservice.exe from any folder.

I like to keep the batch file (FIM.bat), the log file (log.txt) and the server name file (computers.txt) in a folder called BATCH. I also like to add the BATCH folder to the path.

You should save the script as a .bat file. Make sure that it does not have a .txt extension. If you want, you can schedule this batch file to run every so often. That way you can ensure that your services are always running. If a service has already started it will only take a second for it to check and log the information. There is no overhead that you need to worry about. You can check out my blog post on how to accomplish that. The link is posted at the end of this article.

Determine the Name of the Service

In the script you should replace the name of the service with your service name. To find out what service name you should use go to Services Console (services.msc) and double-click the service. On the General name look for Service name. For example, the name of the Forefront Identity Manager Service is FIMService, as shown below.

The script will check to see if the service is running, stopped, or paused and will document the result accordingly in the log file. I have set the script to check the status 10 times. You can adjust this setting if necessary.

Running Same Script for Multiple Services

To make things simple, I start both the services in the same script. There are other ways to handle this situation but I wanted my script to be pretty flexible and decided to implement what I wanted to accomplish using this method.

In the first section, I check for the FIMService and in the second section I essentially run the same script for FIMSynchronizationService with slight modification. For example, I don’t want the log file to be deleted after the FIMService is started because I also want to see the results for the second service so I commented out the line that deletes the log file in FIMSynchronizationService section.

Dealing With Multiple Computers

You can use this script to remotely start a service on remote computers. Simply add the name of the computers in the computers.txt file on separate files. If you have only one computer, just add its name to the computers.txt file. For example, if you have 3 servers named server1, server2, and server3, your computers.txt file will look like this.

Once the batch file is complete you can verify the status of the services in the Services Console and also check the log file for the activity that took place.

The Log File

The script also utilizes a log.txt file. Each time the batch file is executed it first deletes the old log file and then creates a new log.txt file and enters the necessary information in the file for you. If both the FIM services were stopped, your log file will display the following information.

Sample Script

I named my script FIM.bat because I am using it to start two FIM services. Obviously, you can name it whatever you want but keep the name short.

Best Practice: Use the 8.3 file naming convention for your batch file. In other words, do not use more than 8 characters (excluding the extension) for the file name. For example, instead of naming it something like startsrvcs.bat, name it strtsrvc.bat to follow the 8.3 file naming convention.

You can copy the following text to the clipboard and then paste it into notepad. Modify the script as mentioned above and then save it as a batch file using 8.3 naming convention. Make sure you test the script in a test environment first to make sure it does what you expect it to do.

@echo off

cls

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: Check the status for FIMService

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Set LogFile=log.txt

If Exist %LogFile% Del %LogFile%

Set Errors=

For /f “tokens=1″ %%i In (computers.txt) Do (

Call :process %%i

If %ErrFlag%==1 Set Errors=1

)

If Defined Errors Echo FIMService on some machines could not be started. Please check the log in %LogFile%.

Goto :STOP

Set LogFile=log.txt

If Exist %LogFile% Del %LogFile%

Set Errors=

For /f “tokens=1″ %%i In (computers.txt) Do (

Call :process %%i

If %ErrFlag%==1 Set Errors=1

)

If Defined Errors Echo FIMService on some machines could not be started. Please check the log in %LogFile%.

Goto :STOP

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: Subroutines

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:process

:: %1 is the Remote Machine Name

Set ErrFlag=0

Set SERVICE1=FIMService

:FIMService

Call :CheckState %1

If “%STATE%”==”RUNNING” (

Echo FIMService on %1 has already started.>> %LogFile%

Goto :EOF

)

If “%STATE%”==”STOPPED” (

Echo FIMService on %1 is in Stop mode. Attempting to start the service….>> %LogFile%

psservice \\%1 Start %SERVICE1% 2>Nul

Call :Wait %1

Goto :EOF

)

If “%STATE%”==”PAUSED” (

Echo FIMService on %1 is in Paused mode. Attempting to start the service…>> %LogFile%

psservice \\%1 cont %SERVICE1% 2>Nul

Call :Wait %1 Re-

Goto :EOF

)

Echo FIMService on %1 is in an Unknown state. Please check the %1 system.>> %LogFile%

Set Errflag=1

Goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:CheckState

Set STATE=

For /F “Tokens=3 Delims=: ” %%a In (‘psservice 2^>Nul \\%1 query %SERVICE1%^|Find /I “STATE”‘) Do Set STATE=%%a

Goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:Wait

:: %1 is the Remote Machine Name

:: %2 is null if attempting to start, is Re- if restarting

Set loop=0

:Check

:: Will check state 10 times, adjust as needed.

If %loop%==10 Goto NoStart

:: Wait 1 second for service to (Re-)start (can also use sleep utility)

:: adjust -w as needed units are milliseconds

Ping 1.0.0.0 -n 1 -w 1000 >Nul

Call :CheckState %1

If NOT “%STATE%”==”RUNNING” (set /a loop+=1) & Goto Check

Echo FIMService on %1 has %2started successfully.>> %LogFile%

GOTO :EOF

:NoStart

Echo Unable to %2start FIMService on %1>> %LogFile%

Set ErrFlag=1

Goto :EOF

)

:STOP

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: Check the status for FIMSynchronizationService

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

@echo off

Set LogFile=log.txt

:: If Exist %LogFile% Del %LogFile% (Log file should not be deleted after the FIMService is started)

Set Errors=

For /f “tokens=1″ %%i In (computers.txt) Do (

Call :process %%i

If %ErrFlag%==1 Set Errors=1

)

If Defined Errors Echo FIMSynchronizationService on some machines could not be started. Please check the log in %LogFile%.

Goto :STOP

Set LogFile=log.txt

If Exist %LogFile% Del %LogFile%

Set Errors=

For /f “tokens=1″ %%i In (computers.txt) Do (

Call :process %%i

If %ErrFlag%==1 Set Errors=1

)

If Defined Errors Echo FIMSynchronizationService on some machines could not be started. Please check the log in %LogFile%.

Goto :STOP

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: Subroutines

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:process

:: %1 is the Remote Machine Name

Set ErrFlag=0

Set SERVICE1=FIMSynchronizationService

:FIMSynchronizationService

Call :CheckState %1

If “%STATE%”==”RUNNING” (

Echo FIMSynchronizationService on %1 has already started.>> %LogFile%

Goto :EOF

)

If “%STATE%”==”STOPPED” (

Echo FIMSynchronizationService on %1 is in Stop mode. Attempting to start the service….>> %LogFile%

psservice \\%1 Start %SERVICE1% 2>Nul

Call :Wait %1

Goto :EOF

)

If “%STATE%”==”PAUSED” (

Echo FIMSynchronizationService on %1 is in Paused mode. Attempting to start the service…>> %LogFile%

psservice \\%1 cont %SERVICE1% 2>Nul

Call :Wait %1 Re-

Goto :EOF

)

Echo FIMSynchronizationService on %1 is in an Unknown state. Please check the %1 system.>> %LogFile%

Set Errflag=1

Goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:CheckState

Set STATE=

For /F “Tokens=3 Delims=: ” %%a In (‘psservice 2^>Nul \\%1 query %SERVICE1%^|Find /I “STATE”‘) Do Set STATE=%%a

Goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:Wait

:: %1 is the Remote Machine Name

:: %2 is null if attempting to start, is Re- if restarting

Set loop=0

:Check

:: Will check state 10 times, adjust as needed.

If %loop%==10 Goto NoStart

:: Wait 1 second for service to (Re-)start (can also use sleep utility)

:: adjust -w as needed units are milliseconds

Ping 1.0.0.0 -n 1 -w 1000 >Nul

Call :CheckState %1

If NOT “%STATE%”==”RUNNING” (set /a loop+=1) & Goto Check

Echo FIMSynchronizationService on %1 has %2started successfully.>> %LogFile%

GOTO :EOF

:NoStart

Echo Unable to %2start FIMSynchronizationService on %1>> %LogFile%

Set ErrFlag=1

Goto :EOF

)

:STOP

By the way, I have tested this batch file numerous times and it worked perfectly every single time. If you decide to schedule this batch file, check out my article How to run a batch file as a task in Windows Server 2008.


Copyright ©2011 Zubair Alexander. All rights reserved.

Contact E-mail | Terms of Use | Privacy Policy

Copyright ©2002-2013 Zubair Alexander. All rights reserved.

Internal Links

Search Blog

Categories

Archives

April 2014
M T W T F S S
« Mar    
 123456
78910111213
14151617181920
21222324252627
282930  

RSS Feeds

TechGalaxy Visitors

30 queries. 0.611 seconds