Changing your Network Location with PowerShell

You’re having one of those days. Remote Desktop isn’t working. The Windows Firewall is constantly in your way. You hop in to the Network and Sharing Center and there it is, your network adapter staring you down with a Public network category. As Windows makes this a complete pain in the bum to change, off to PowerShell we go.

  1. Open a PowerShell prompt as an Administrator.
  2. Run Get-NetConnectionProfile to retrieve a list of all of the current network connections. Take note of the InterfaceIndex for the connection you’d like to update.
  3. With your InterfaceIndex in hand, run Set-NetConnectionProfile and pass in the InterfaceIndex and the desired NetworkCategory. [e.g. Set-NetConnectionProfile -InterfaceIndex 11 -NetworkCategory Private]
  4. Rejoice in your network connection that is now in the right category.

Quick PowerShell Snippet - BITS Transfer Status with Progress

BITS Transfers are fun. They can be asynchronous, sometimes intelligent, and far more resilient than a traditional WebClient download.

As asynchronous transfers are occurring in the background, it can be helpful to monitor their progress. The following snippet allows us to do just that, outputting the progress on any current BITS transfers with a status (or JobState) of Transferring.

Determining Core Counts in Azure PowerShell the Easy Way™

In the past, to retrieve the number of cores in use in a given Azure subscription (or to retrieve the maximum number of cores available), we had to jump through some hoops to get the information we needed.

One method was to loop through ALL of the Services and then ALL of the Virtual Machines provisioned and maintain a running count of the consumed cores (Ex. Windows Azure compute cores and you. Not only is this tedious, it’s no longer necessary!

A new parameter has been introduced to the Get-AzureSubscription cmdlet called ExtendedDetails. By executing the cmdlet with the ExtendedDetails parameter, you’ll see not only the CurrentCoreCount and MaximumCoreCount, but also some metrics around the number of services you’re consuming (Cloud Services, Storage Accounts, etc.).

Get-AzureSubscription -ExtendedDetails output

If you’ve been executing scripts to provision environments that consistently fail due to lack of cores, a quick check of the output of the Get-AzureSubscription cmdlet could save you some time.

Inconvenient Settings Loss With Dismount-SPContentDatabase

Are you someone that likes to go through your web applications and dismount your databases prior to patching? Or maybe you’re just moving a content database from one web application to another. Or maybe, just maybe, you want to dismount a content database for the fun of it!

Well, if that’s you, you might have noticed (or maybe not), that when the content database is remounted, a few properties get reset to their default values such as MaximumSiteCount and WarningSiteCount (2,000 and 5,000 respectively). This can be pretty inconvenient (to say the least!) if you’ve modified these settings in any way and even more inconvenient if you didn’t document them before running Dismount-SPContentDatabase.

Once again, it’s our friend PowerShell to the rescue. The following functions allow you to dismount all of the content databases in a given web application (Dismount-SPContentDatabases) while retaining the pertinent settings in CSV file. The function helpfully titled Mount-SPContentDatabases will remount the content databases (using Mount-SPContentDatabase) to the supplied web application using the previous settings (including the original DatabaseServer), thus saving you at least 5 minutes to enjoy an additional cup of coffee.

Example Use

Imagine you have a web application with the name ContosoIntranet.

Importing Terms from CSV in PowerShell

As the Term Store Management Tool in SharePoint 2010 does not allow for delta imports, the following PowerShell script will consume a .csv file and import the terms into the term store for the supplied term set if they do not yet exist.

File Format

Term
Sample Term 1
Sample Term 2
Sample Term 3

The Script

Export a Term Set to CSV with Identifiers in PowerShell

I recently had a need to export all of the terms from a given term set in a SharePoint 2010 Managed Metadata Service application to CSV for consumption in another process. The following PowerShell script will create a .csv file with all of the term names and identifiers for the supplied Term Store/Term Store Group/Term Set.

Updated: There was an error in the previous version of this script. It did not account for the fact that the ampersand character (“&”) is stored as a fullwidth ampersand (“\uFF06”) when a term is created. The script has been adjusted to replace any fullwidth ampersands with a basic Latin ampersand in the output.

The Script

SharePoint 2010 Products scripted deployment

It seems I missed it when Microsoft published the SPModule PowerShell module to aid in a scripted installation of a SharePoint 2010 farm.

If anything, it can serve as a handy guide for how to script out individual actions pertaining to installation and farm maintenance.

Reference

SharePoint 2010: Provisioning a new Farm with Powershell

SharePoint 2007 always required some level of scripting in it’s installation to allow for greater control of the installation process (i.e. creating database names without GUIDs). SharePoint 2010 is not any different in that scripting is still required to gain the finer control over installation. What has changed is that now PowerShell can be used in place of psconfig/stsadm. With that in mind, let’s begin.

  1. Install the SharePoint binaries on each server in your farm. Select “Complete” as the installation type. This will allow you to create a farm as opposed to a single server installation.After the install has completed, you will asked if you would like to complete the “SharePoint Products Configuration Wizard”. Do not run the wizard at this time.
  2. On the server which you wish to provision Central Administration:
    1. Open the  ”SharePoint 2010 Management Shell” (right-click and select “Run as administrator”). The shell will load with a message that the local farm is not accessible. This is expected as we have only installed the binaries.
    2. Run the following command to create the initial configuration/content database for the farm.
      
      New-SPConfigurationDatabase –DatabaseName “SharePoint2010_Config” –DatabaseServer “SharePoint2010SQL” –AdministrationContentDatabaseName “SharePoint2010_Content_Admin” –Passphrase (ConvertTo-SecureString “Pass@word1” –AsPlaintext –Force) –FarmCredentials (Get-Credential)
      

    3. After the initial creation of the farm, close and reload the “SharePoint 2010 Management Shell”. You should no longer receive any error messages.
    4. Install the help files.
      
      Install-SPHelpCollection -All
      

    5. Secure the resources used by the server (files and registry).
      
      Initialize-SPResourceSecurity
      

    6. Install and provision the farm services.
      
      Install-SPService
      

    7. Install the features on the server.
      
      Install-SPFeature –AllExistingFeatures
      

    8. Provision Central Administration.
      
      New-SPCentralAdministration -Port 1234  -WindowsAuthProvider "NTLM"
      

    9. Install the application content.
      
      Install-SPApplicationContent
      

    10. Optional: Disable the loopback check. If this is a development install, outright disabling the check should be fine. For production environments, the loopback check should be left in place and BackConnectionHostNames should be used in its place. See KB 896861.
      
      New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -Name "DisableLoopbackCheck"  -value "1" -PropertyType dword
      

  3. On each additional server that you wish to converge in the farm:
    1. Open the  ”SharePoint 2010 Management Shell” (right-click and select “Run as administrator”). The shell will load with a message that the local farm is not accessible. This is expected as we have only installed the binaries.
    2. Connect the server to the farm with the following command:
      
      Connect-SPConfigurationDatabase -DatabaseName "SharePoint2010_Config" -DatabaseServer "SharePoint2010SQL" -Passphrase (ConvertTo-SecureString "Pass@word1" -AsPlaintext -Force)
      

    3. Install the help files.
      
      Install-SPHelpCollection -All
      

    4. Secure the resources used by the server (files and registry).
      
      Initialize-SPResourceSecurity
      

    5. Install and provision the farm services.
      
      Install-SPService
      

    6. Install the features on the server.
      
      Install-SPFeature –AllExistingFeatures
      

    7. Install the application content.
      
      Install-SPApplicationContent
      

    8. Optional: Disable the loopback check. If this is a development install, outright disabling the check should be fine. For production environments, the loopback check should be left in place and BackConnectionHostNames should be used in its place. See KB 896861.
      
      New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -Name "DisableLoopbackCheck"  -value "1" -PropertyType dword
      

Reference

Getting Started With Windows PowerShell and SharePoint

Today was my first experience with PowerShell, and I have to say I’m impressed.  PowerShell truly does appear to be a command prompt for the .NET generation.  While it can be agravating at times, I can see PowerShell quickly becoming a “can’t do without” tool in my arsenal.

Resources: