Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Thursday 23 June 2011

SP2010 install using AutoSPInstaller from PowerGUI

Problem: I want to install SharePoint 2010 using Brian Lala's AutoSPInstaller codeplex project however, I want to be able to see values and debug using PowerShell

Resolution:
Ensure AutoSPInstaller is setup correctly and PowerGUI is installed.  Correct setup is shown below.  Ensure that the xml file is named correctly.

Tip: Windows has a built in graphical debuggger for PowerShell called ISE Editor
http://blogs.msdn.com/b/powershell/archive/2009/01/19/debugging-powershell-script-using-the-ise-editor.aspx
PS> c:\SP2010\AutoSPInstaller\AutoSPInstallerMain.ps1 c:\SP2010\AutoSPInstaller\AutoSPInstallerInput-WIN-2C5PI8TSRPA.xml

Press F5 or the arrow to debug.  I have all the .ps1 files in the PowerGUI editor and add break points. 

Note.  If you hit a break point and press F5 once you are debugging, PowerGUI will not stop at the next break point.  F10 will take you to your next breakpoint or where the next error is trapped.

Wednesday 8 June 2011

Powershell Setup for SharePoint Developers

Overview:  PowerGUI (from Quest Software) provides a PowerShell(PS) GUI to perform actions with minimal customisation you get Powershell with Intellesense and colour coding that is SharePoint aware.  Additionally you can plug in a Visual Studio (VS) plug-in (VSIX) that allows you to have PowerGUI functionality directly within you VS IDE.

1.> Install PowerGUI
1.1.> Download PowerGUI at http://www.powergui.org/ I installed version 2.4.0
1.2.> Install the PowerGUI msi on your development server

2.> Configure PowerGUI
2.1.> Open the PowerGUI application
2.2.> Add the Microsoft.SharePoint.PowerShell module as shown below:
3.> Write a PS script
3.1.> Open the "PowerGUI Script Editor" by clicking Tools > PowerGUI Script Editor
3.2.> Ensure your intellesense is working by typing "Get-SP", you will see the intellisense pick up the GET-SPSite cmdlet.
3.3> Add the following command and debug with a break point to see PowerGUI in action.  This will get all the Content Types associated to the RootWeb of a specific Site Collection.  Note the variables windows on the right hand side of the PowerGUI interfaces allows you to examine each of the variable objects declared in the script.
It's not full intellesense but it is a huge help to get you most of the way to writing custom PS.

4.> Add PowerGUI Visual Studio 2010
4.1.> Add the "PowerGUI VSX" through extension manager (VSIX) (Tools > Extension Manager > Online Gallery > Search for .. PowerGUI > Download.
4.2.> VS2010 will reboot, ensure VS2010 is open, click "View" > "PowerGUI Console".  You can use the console to write PS.
4.3.>  The VSIX also provides an SPI for adding PS Scripts as shown below:



Error msg:
PS E:\Windows\system32> Add-PSSnapin -Name Microsoft.SharePoint.PowerShell
Add-PSSnapin : The Windows PowerShell snap-in 'Microsoft.SharePoint.PowerShell' is not installed on this machine.

References:
PowerGUI and SharePoint 2010

Update: 19/07/2012 - I downloaded the latest version of PowerGui 3.2.0.2237 and tried to call SharePoint scripts arfer adding the SP add-in and got the following error:
Get-SPFarm : Microsoft SharePoint is not supported with version 4.0.30319.237 of the Microsoft .Net RuntimeFix navigate to: C:\Program Files (x86)\PowerGUI\ScriptEditor.exe.config and edit the xml.  I comented out the line telling PowerGui to use .NET 4.0.

Friday 4 March 2011

Reset your AD Pswd history

Problem:  As a developer I setup a development machine on my own network and the default security policy forces me to change my password every 60 days.

Hypothesis:  Use Powershell to change how often the password needs to be reset in AD.  I don't know this so if anyone has this script please post it.

Resolution: Use the PS to remove you history.  This at lease allows me to reuse pswds repeatedly so I don't ned up with a lot of versions.  I have multiple VM so it's pretty useful to know my passwords are consistant.  Thanks to Brad Turner for posting this script.

# Pass the number of days to retain on the cmdline
param ([string]$NumDaysToKeepPwdHistory = 14)
# Calculate the date to clear password history against
[string]$ClearPwdHistoryDate= [DateTime]::Now.AddDays(-$NumDaysToKeepPwdHistory).ToUniversalTime()
# Get the WMI Object for your sever (use your server name)
$myserver = @(get-wmiobject-class "Win2008R2-machine6" -namespace "root\MicrosoftIdentityIntegrationServer" -computer ".")
# Clear the Password History
Write-Host "Clearing the Password History prior to (UTC)" $ClearPwdHistoryDate
Write-Host "Result: " $myserver[0].ClearPasswordHistory($ClearPwdHistoryDate).ReturnValue
# New line
trap{
Write-Host "'nError: $($_.Exception.Message)'n" -foregroundcolorwhite -backgroundcolordarkred
Exit
}

Tip:  This should not be done in production, only use on development environements.

Read More:
Brad Turner on removing Pswd history

Update: 2 April 2011 - Edit password setting using Group Policy 

Thursday 3 March 2011

AD account password out of sync with the managed service account within SharePoint

Problem: I am trying to start services on a server, when I start the Search Foundation service I get the following error: "The password for the account ...\..., as currently stored in SharePoint, is not the same as the current password for the account within Active Directory.  To fix this with Powershell, run Set-SPManagedAccount -UseExistingPassword."
Initial Hypothesis: The password for the account I am using to run the service using has been changed in AD, this does not match the password stored in the SharePoint.

Resolution: Reset the AD Password



Ensure SharePoint is using the correct pswd i.e. chnage the store managed account password as shown below using the Set-SPManagedAccount cmd.




Friday 19 November 2010

Deploying a Sandbox solution

Problem: I created several wsp packages, unfortunately 1 of the solution projects was marked as a Sandbox solution when the project was created in Visual Studio 2010.  The lookup lists are not deploying correctly in the sandbox solution because I deployed the solution at farm level.

Initial Hypothesis: I notice my lists had strange behaviour.  I check my deployment and 1 of the solutions had not been deployed.  When I deployed the wsp using Powershell (PS> Install-SPSolution ...) it did not error so I had presumed it had installed correctly.  Using PowerShell (PS) I could see the solution holding the list wasn't installed. PS> Get-Solution
Resolution: Retract the solution and remove it from the farm.  Deploy the solution to the Site Collection's solutions and enable the feature/s.  Powershell Commands:
PS> Add-SPUserSolution -LiteralPath D:\packages\.Lists.wsp -Site http://demo.dev

PS> Install-SPUserSolution –Identity Lists.wsp -Site http://demo.dev
PS> Enable-SPFeature –Identity Feature_GeneralLists –url http://demo.dev/

Tip: Update 10 Dec 2010 - If you remove a solution (this also applies to sandbox solutions) (including deactivating the associated features), if the feature deploys content types or site columns, you will need to do an IISreset if you want the content types and site columns removed from the Site Collection (alernatively restart the Web Application so the whole farm is not reset).

Post on deploying fasm wsp's.

Wednesday 17 November 2010

Deploying packages into production using PowerShell

Problem: Developers build solutions and features for SharePoint 2010 using Visual Studio 2010 (VS), the farm wsp's need to be deployed in UAT, pre-production, production, CI potentially etc.  You don't have VS so you can use stsadm or Powershell.

Initial Hypothesis: This is a walk-thru on deploying your solutions and features in SharePoint 2010.  The main areas are:
  1. creating solution files (wsp's);
  2. moving them to the target environment;
  3. adding the various solutions and features at the appropriate level in your farm (features have 4 steps to get into production namely:
    • Add Solution,
    • Install Solution,
    • Install Feature and
    • Activate Feature.
Resolution:

1.> Create wsp's
1.1> Using VS2010, put the project into "Release" mode
1.2.> Next create the package using VS
2.> Copy the wsp located in the bin\Release directory of your VS solution to the farm where CA is hosted.
3.> Add solutions and activate features.
PS> Add-SPSolution D:\Packages\Demo.wsp
PS> Install-SPSolution –Identity Demo.wsp -GACDeployment
PS> Install-SPFeature demo_feature1 -force
PS> Enable-SPFeature –Identity demo_feature1 –url http://demo.dev/sites/demo
Tip: The feature scope requires different parameters.  For example if the feature above ( ) was scoped at Site (Site collection) level, the cmd would be: PS> Install-SPSolution –Identity Demo.wsp -GACDeployment –WebApplication http://demo.dev/
Scoped as web application level
PS>Install-SPSolution –Identity Demo-WepAppScope.wsp -GACDeployment –WebApplication http://demo.dev/sites/demo
Remove a web or site scoped solution
PS> Disable-SPFeature –Identity demo_feature1 –url http://demo.dev/sites/demo
PS> UnInstall-SPFeature demo_feature1 -force
PS> UnInstall-SPSolution –Identity Demo.wsp -GACDeployment
PS> Remove-SPSolution Demo.wsp
Cmd to add the SharePoint snapin, allowing the administration to manage SharePoint
PS> Add-PSSnapin Microsoft.SharePoint.PowerShell

Post on deploying Sandbox solutions.

Update: 27 Feb 2013.  The Install-SPFeature is only used for manual install i.e. not with wsp deployments but when you manually copy the feature files to each WFE.  You can keep the step as it has no negetive impact but it is not required.

Update: 20 May 2013.  This just bit me again.  I have an SP Timer Job scoped to WebApplication, by default when the solution/wsp is deployed, the feature receiver is activate automatically on ALL web applications (including MySites).  Change the Feature xml to include the attribute ActivateOnDefault="FALSE".

Read More:
http://sppowershell.com/2010/06/25/provisioning-sharepoint-2010-with-powershell/
http://msdn.microsoft.com/en-us/library/ms442691.aspx
http://stsadm.blogspot.com/2010/06/deploying-sharepoint-2010-solution.html
http://patrickboom.wordpress.com/2010/05/31/using-powershell-to-deploy-sharepoint-solutions-wsp-2/
http://dotnet.sys-con.com/node/1208275

Powershell - Running as a farm administrator using the SharePoint plug-in on Powershell

Problem: I need to run SharePoint Powershell commands in Production/UAT, I get remote access to the servers however the account is not the SharePoint farm account as the Famr account runns windows services and should not have remote access to the servers. 

Initial Hypothesis: Remote desktop into the WFE server, use the command prompt to launch Powershell using the farm account.  Lastly, add the SharePoint snapin for SharePoint 2010.
Update 6 Dec 2010 - Alternatively, hold down the shift key and right click the program you wish to run, you are given the "run as .." menu option.

Resolution:
1.> Remote desktop into the machine using a remote desktop access account i.e. demo\
2.> Open the cmd prompt and run the following cmd:
cmd > runas /user:demo\farm_admin c:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe
3.> In the new Powershell windows add the PowerShell snapin
ps c:\windews\system32> Add-PSSnapin Microsoft.SharePoint.PowerShell
Read More:
http://social.technet.microsoft.com/Forums/en-US/sharepoint2010setup/thread/fcb77654-0f13-42e0-b181-6e52242fe9d6  Adding the SharePoint snapin to PowerShell.

Saturday 2 October 2010

Powershell to Move a Site Collection to a new Content Database

Problem: I have several site collections inside a single content database.  The content database is growing rapidly.  Backup and restore is fairly slow and SQL can start slowing down if the db is large enough.

Hypothesis: Create at least additional content db, move specific site collections to the new Content database.  I was surprised that moving site collections to content databases is not in the Central Admin (CA) UI.

Resolution: Create an additional content database either using PowerShell (PS) or the UI namely:
1.> CA > Application Management > Manage Content Databases > Add a Content Database (I called my content database "WSS_Content_Empty").
2.> Move the Site collection to the new content database using PS
3.> Check the database has been moved in CA

More Info:
http://technet.microsoft.com/en-us/library/cc825328.aspx

Set New Content Database using CA Only:
  1. UI only option to achieve the same result would be to create the new content database using CA as shown above. 
  2. Next using CA go to "Manage Content Databases", select the old/existing content database and set it to offline (This means that you can't add new content databases, existing site collections are still available).
  3. Adding a new Site Collection will now use the new/next content database that is ready/online.   
End of Post

Backup and Restore Powershell commands:
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
cls
Backup-SPSite -Identity 'http://my.demo.dev/sites/kbtest' -Path C:\Projects\Backup\test.bak -Force -NoSiteLock -UseSqlSnapshot -Verbose
Restore-SPSite -Identity 'http://my.demo.dev/sites/newkbtest' -Path C:\Projects\Backup\test.bak -Force -Verbose 

Friday 3 September 2010

SharePoint 2010 PowerShell Useful Scripts

Get Content Types in a Site collection
$site = new-object Microsoft.SharePoint.SPSite(http://ww3.sp.demo.dev/)

$web = $site.rootweb.ContentTypes
ForEach ($id in $web)
{
'"' + $id.Name + $id.Id + '"' Out-File "d:\logfiles\contenttypes.txt" -append
}

Delete a List Instance
$site = new-object Microsoft.SharePoint.SPSite(“http://demo.dev/”)
$web = $site.rootweb
$list = $web.Lists["Orders"]
$list.Delete()
 
Run all time jobs 
$timers=Get-SPTimerJob|{$_.isDisabled -eq $false}
foreach ($timer in $timers) {$timer.RunNow()}
View ULS data for a CorrelationId
PS C:\Users\PaulB> get-splogevent ?{$_.Correlation -eq "fe45a7cd-e91b-4ad3-80dc-50cd2a238c6a"} select Area, Category, Level, EventID, Message Format-List 
Run the SharePoint Health Analyzer
$jobs = Get-SPTimerJob | Where {$_.DisplayName -match "Health Analysis Job"}
foreach($job in $jobs) {
  Write-Host "Running" . $job.DisplayName
  $job.RunNow()
}

Read an XML file:

Not all jobs are fired automatically so you can run all the SP Health Analyzer jobs using this PS cmd.  Ensure server can deal with extra load (run out of hours if possible).
Ari Bakker

Turn On-Off Publishing Workflow
John Livingston
PowerShell Basics
Zach Rosenfield's - Farm settings, retrieve solutions, basic SharePoint operations
Gary Lapointe
Blog

Brian Lalancette
AutoSPInstaller - Installation of SP2010
Use Windows PowerShell to Display Service Dependencies

Adam Preston
Blog
Disable Loopback in IE - New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -Name "DisableLoopbackCheck" -Value "1" -PropertyType dword

Todd Klindt
Blog

PowerGui
PowerGui SharePoint reference card
Download PowerGui

Add the SP cmdlets to PS window
Add-PSSnapin Microsoft.SharePoint.PowerShell

Get Help about PS Cmds
PS> Get-Help Get- SpWebapplication
http://thuansoldier.net/?p=1559

Updated: 23 Oct 2012
Add a local user account

Add an account to AD and add the account to a group.

Apologies if I have ripped script directly.  I don't have the source for a lot of the work these help scripts are based.  On pls let me know if I need to credit a source.
 

Monday 16 August 2010

Using PowerShell to retrieve SharePoint site column guids

Problem: I am creating a Content Type that uses 1 of the built in site columns (field column), I need the guid of a OOTB site column.  I am looking to use the OOTB "Append-Only Comments" site column.
Resolution: Open PowerShell and execute the cmd:
$web = Get-SPweb http://demo1
write-host $web.Fields["Append-Only Comments"].Id

Tip: Don't add xml comments into declaritive content type creation, it stops the activation without any warning. 

Tuesday 15 June 2010

Finding Your Default File Locations in SMO

Alan White's article helped me figure out where my default databases would be created.
To open SQL Powershell type "sqlps" in the run prompt.

==============================================

Update: 2012/12/20
Problem: After creating a udl file to check my SQL connection to the database which failed I realiased the SQL 2012 install done by the DBA was not on the default port 1433.  And it was set to dynamic.

Resolution: Change the TCP/IP network configuration as shown below