Archive for November, 2013

List files and folders (DIR) with PowerShell

Here’s a quickie. You want to get a list of all the files and folders to use in a script? Here’s how:

$fileEntries = [IO.Directory]::GetFiles(“c:\somefolder”/);
foreach($fileName in $fileEntries)
{
    Write-Host $fileName;

Want to do the equivalent of *.txt? Add this:

$fileEntries = [IO.Directory]::GetFiles(“c:\somefolder”,”*.txt”);

Fun stuff!

Leave a comment

Reading and Writing CSV files with PowerShell

I don’t always use CSV files but when I do, I do them manually. Like a man.

I use to fancy myself a lazy coder. The simpler, the better. With that in mind, here’s an easy one for you.

There are many goofy ways to create and read CSV files in PowerShell. But here is a quick and easy way that won’t make you crazy.

Writing the CSV file

Add-Content d:\backup\backup.txt “Url,Path”;

foreach($site in $spWebApp.Sites)
{
$line = $site.Url + “,” + $fullpath;
Add-Content d:\backup\backup.txt $line;
}

This is as simple as using Add-Content to write to a text file. The first Add-Content command writes the headers which is the only thing you need to really make this a CSV file. The loop just writes two value separated by a comma.

Now comes the fun part: reading the CSV file into a couple of variables.

$csvlines = import-csv d:\backup\backup.txt
foreach($line in $csvlines)
{
$url=$line.Url;
$path=$line.Path;
Write-Host “url=” $url;
Write-Host “path=” $path;
}

Basically, the import-csv command “opens” the file. The loop just enumerates through the file and gets the “records”. Finally, the lines object will contain whatever you put in for the headers. In this case, Url and Path.

Simple and to the point. And always remember …

Stay thirsty my friends

1 Comment

Listing the properties and methods of an object in PowerShell

Here’s a quick one. Have you ever wondered how to list all the properties and methods of an object in PowerShell? It’s pretty simple. Just use the Get-Member cmdlet. Here’s an example to get the properties and methods of an object returned from Get-SPSite:

Get-SPSite | Get-Member

As you can see, you are just piping the output to the Get-Member cmdlet. You get a nice list of everything all organized and alphabetized for you. It looks something like this:

Capture

Try it! You’ll like it!

Leave a comment

Validate Input of PowerShell Commands

In a previous post I presented some code to create site collections in a new content database. My new client requested a similar process but this time I wanted to add some validation to the front. There is a lot of prompts to type and I’ve fat fingered some data on more than one occasion. Of course, once the code starts to execute, it’s too late. Then it’s go to SQL and delete content database, a trip to CA, etc. Not fun.

Now you would think there would be a ton of info on how to manage a simple Y/N prompt but it was a bit harder to find than expected. So I’m putting it here for all to see!

There are several ways to handle this. Some cmdlets have the -confirm parameter. Just use the -? parameter to find out what’s available. However, what I wanted to do was dump out the user input and have them verify everything before running the script.

The basics are pretty simple. We are going to use the
System.Management.Automation.Host.ChoiceDescription function. We just setup a few variables to use and $host.ui.PromptForChoice to make it happen. Below is the sample code:

$title = “Create Site Collection”
$message = “Do you want to create this site collection?”
$yes = New-Object System.Management.Automation.Host.ChoiceDescription “&Yes”, `
    “Creates site collection.”
$no = New-Object System.Management.Automation.Host.ChoiceDescription “&No”, `
    “Do not create site collection.”
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)

As you can see, everything gets returned in the variable results. What you get back is 0 for the first choice (the default), 1 for the second choice, 2 for the third choice, etc. You can add about as many choices are you want here. In our case we have only 2: yes and no. We do have the built in third choice which is help (you get that for free). The help responses are the second parameter in the System.Management.Automation.Host.ChoiceDescription call. For the Yes prompt, for example, it reads “Creates site collection.”. Uber simple stuff.

Now let’s add the code to either execute the script or leave it:

switch ($result)
    {
        0 {break;}
        1 {exit;}
    }

The 0 value returned means we have a Yes so we just leave the switch statement. If we get 1, that’s a no so we just exit out of the PowerShell script. Pretty cool, huh?

Now let’s put it all together. This code gets all the parameters needed to create a site collection in a new content DB, dumps them to the screen, and asks the user if they want to execute the commands or not.

$ErrorActionPreference = “Continue”;

#Get input from user
$server = Read-Host “Enter Content Database Alias”; #This is the alias you setup to your database server.
$webapp = Read-Host “Enter the WebApp Root URL (https://url.webapp.com)”; #This is the root URL to your web app
$name = Read-Host “Enter New Site Collection Name”; #Prompts for Site Title (you can change this later)
$site = Read-Host “Enter New Site Collection URL”; #Prompts for your new site collection URL (https://url.webapp.com/sites/newsitecoll for example)
$dbname = Read-Host “Enter Content Database Name”;
$owner1 = Read-Host “Enter Site Owner 1 (domain\owner1)”;
$owner2 = Read-Host “Enter Site Owner 2 (domain\owner2)”;

#Confirm everything (just dumps out what they entered)
Write-Host “”
Write-Host “Site Collection Parameters”
Write-Host “__________________________”
Write-Host “DB Server: $server”;
Write-Host “Root URL of Web App: $webapp”;
Write-Host “Site Collection Title: $name”;
Write-Host “Site Collection URL: $site”;
Write-Host “Content Database Name: $dbname”;
Write-Host “Site Owner 1: $owner1”;
Write-Host “Site Owner 2: $owner2”;
$title = “Create Site Collection”;
$message = “Do you want to create this site collection?”;
$yes = New-Object System.Management.Automation.Host.ChoiceDescription “&Yes”, `     “Creates site collection.”
 $no = New-Object System.Management.Automation.Host.ChoiceDescription “&No”, `     “Do not create site collection.”
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result)
 {
   0 {break;}
   1 {exit;}
    }
Write-Host “”; Write-Host “Creating site collection: $name”;
Write-Host “______________________________”;

 

 

Leave a comment

Popularity Trends missing in SharePoint 2013

For those of you who have taken the time to wrap your head around the new web analytics for SharePoint 2013, my sympathies are with you. It’s more FAST than SharePoint and the learning curve is steep.

One of the “features” Microsoft left for us was the Site Web Analytics reports link in Site Actions. Click on it. I dare you. Yep, an error. Because it doesn’t exist anymore! So why is it there? Whoever figures that out, please let me know.

The new thing is called Popularity Trends. Sounds more like the it would relate to Paris Hilton and Lady Gaga, doesn’t it? Anyway, it’s no where to be found until you do a little trick. Go to Manage Site Features and turn on the Reporting feature. Then navigate back to Site Settings. You magically have Popularity Trends under Site Administration and Popularity and Search Reports under Site Collection Administration. And, last but not least, the Site Web Analytics link is gone. Two for the price of one!

Now if I can just figure out why my reports are empty …

2 Comments