Friday, September 27, 2013

Setting Environment Variable with Powershell

In this article I will give you a quick little script that will help you set environment variables that you can then call from any other script.
There are two ways to set Environment Variables and a couple ways to call them, we will start with the standard way of doing it.
setx
Lets say that you want to set your Home Environment, for instance you have a Development or QA server farm.

setx /m Home_Env "My_Company_QA"; This particular Command would set a system variable named Home_Env and the value to "My_Company_QA". This value can be seen in
"My Computer-Properties-Advanced System Settings-Environment Variables it is also written to the registry"

The second way, and less used is to write the value directly to the registry, which essentially achieves the same goal. $EnvRegKey01 = "HKLM:\System\ControlSet001\Control\Session Manager\Environment"
cd $EnvRegKey01
New-ItemProperty -Path $EnvRegKey01\ -Name "Home_Env" -Value "My_Company_QA"
This would accomplish the same as the above; however it directly sets it to the registry. Now if the ItemProperty already exists you can use a number of different options to the -ItemProperty such as:
remove-, clear-, and rename-. Just so as to not deal with writing in checks to my script I typically just run the remove first then the new. If your key does not exist it will throw an error though.

Now to Call the value in your other scripts you need to know that the standard call is Get-ChildItem or GCI, but this will not work if it is run in the same session that you set the value, so you will have to close and reopen powershell. However there is a way around that. But first the standard way to grab your value.
Get-ChildItem env:Home_Env
#to actually use it as a variable you will need to store it, I will use the shortened version
$HomeEnv = (GCI env:Home_Env).value
#Now $HomeEnv is -eq to "My_Company_Qa"

Now lets say you need the value in the same session you are in, to do this you will need to directly call the Registry value and write it to your variable
$HomeEnv = (Get-ItemProperty -Path $EnvRegKey01\ -Name "Home_Env").home_env

Friday, April 5, 2013

Perminantly add Snap-Ins to your Powershell

In this article I will give a quick little script that will add your powershell snap-ins to you profile so that everytime you launch ISE they will auto load!

if (!(test-path $profile.AllUsersAllHosts)) {new-item -type file -path $profile.AllUsersAllHosts -force} powershell_ise $profile.AllUsersAllHosts #Add the following code to the script file and save your changes: $ver = $host | select version if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { Add-PSSnapin "Microsoft.SharePoint.PowerShell" } This particular script will load the SharePoint snap-ins, but you can switch those last two lines for whatever snap-ins you wish.

For instance: ((Get-PSSnapin "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin" -ErrorAction SilentlyContinue) -eq $null) { Add-PSSnapin "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin" } This would add the Exchange snapin instead of the SharePoint.

Micosoft has a snap-in for just about every solution they produce, and 3rd parties are beginning to put out snap-ins as well. The easiest way to find yours would be to simply google "Add-PSSnapin Name"

Wednesday, March 20, 2013

SharePoint Site Collection Backups Out of the Box

In this article I am going to show you a very simple way that you can configure your SharePoint Site Collection Backups using Powershell. These Powershell scripts were written for 2013, but should work for 2010 as well.

   There are a couple of different types of backups that you can accomplish by using Powershell; Farm Backups - Full, Farm Backup - Differential, Site Collection Backups, and Configuration Database Backups.
   You can technically accomplish a granular backup of all content, but this isn't really a backup as much as a site copy, and I won't be going into that at this time.

Although I personally run all of these backups, my experience is that the most utilized backup is the Site Collection Backup. So lets go ahead and get started.

The first step of any Powershell script is ensuring that the proper scriptlets are loaded, SharePoint is no exception.

1: To load the SharePoint Scriptlet into your SharePoint Script add the following lines

#Add the SharePoint Snap-in, in case PowerShell wasn't started with the Management Shell
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue


2: The code in this piece of the script will enumerate all site collections within a web-app and then return them to the variable $sites

#Enumerate Sites
$sites=Get-SPWebApplication "Web App URL or Name" |
    Get-SPSite -limit ALL


3: Now that you have your variable of Site Collections, you want to loop through them and back them up. It will also name all of the backup files based on the site collection Title.


#Back-Up Sites
foreach($site in $sites)
{
Backup-SPSite -Identity $site.Url -Path "$backuppath$((Get-SPWeb -Identity $site.Url).Title).bak" -Force -Verbose
Write-Host "Successfully backed up $($site.Url) to $backuppath$((Get-SPWeb -Identity $site.Url).Title).bak"
}

4: The final step of this process it to schedule the backups. This must be done from one of your Web Front Ends (WFE), simpley just launch your task schedule and add your .ps1 file, ensure that the account you are using has full privileges to the site collections.


Here is the script in one piece.
#Add the SharePoint Snap-in, in case PowerShell wasn't started with the Management Shell
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

#Enumerate Sites
$sites=Get-SPWebApplication "Web App URL or Name" |
    Get-SPSite -limit ALL

#Back-Up Sites
foreach($site in $sites)
{
Backup-SPSite -Identity $site.Url -Path "$backuppath$((Get-SPWeb -Identity $site.Url).Title).bak" -Force -Verbose
Write-Host "Successfully backed up $($site.Url) to $backuppath$((Get-SPWeb -Identity $site.Url).Title).bak"
}

Tuesday, March 19, 2013

New Blogger

So, I have stayed out of the blogging arena for years, but at some point I realized that I had valuable information with no real way to get it out there. Thus the blog! Most likely this blog will be focussed on my SharePoint endeavors, both 2010 and 2013, much of which will probably be powershell related. So if this is something you are interested in, stay tuned, I have some good stuff to share!