1/14/2016

Azure: PowerShell to List All Blobs in Storage

 

I have an Azure account for development and testing purposes that I want to keep as clean as possible. Part of my routine is clearing out unneeded blobs in the Storage Accounts. To do this using the Portal UI would take a long time… so PowerShell to the rescue.

I thought there would be a quick like piped command something like this:
Get-AzureStorageAccount | Get-AzureStorageContainer | Get-AzureStorageBlob

But no such luck. There’s extra steps to access the Storage Key and to create an Azure Storage Context object needed.

# optional!
$PreviousVerbosePreference = $VerbosePreference
$PreviousWarningPreference = $WarningPreference
$VerbosePreference = "SilentlyContinue"
$WarningPreference = "SilentlyContinue"


# if not alreay logged in to your Azure account...
# Add-AzureAccount
# if more than one subscription
# Select-AzureSubscription -SubscriptionName ????

Get-AzureStorageAccount |
  foreach {

    $acct = $_.label; $storageKey = (Get-AzureStorageKey -StorageAccountName $acct ).Primary;

    $ctx = New-AzureStorageContext -StorageAccountName $acct
-StorageAccountKey $storageKey;

    Get-AzureStorageContainer -Container * -Context $ctx } |

  foreach { $container = $_.Name; $_ } |

  Get-AzureStorageBlob |

  Select {$_.context.StorageAccountName}, {$container}, name,blobtype,length |

  Format-Table -autosize

# optional!
$VerbosePreference = $PreviousVerbosePreference

Select @{label="Storage Account";expression={$_.context.StorageAccountName}},
       @{label="Container";expression={$container}},
       name,
       blobtype,
       @{label="Bytes";expression={"{0,20:N0}" -f $_.length}} |

$WarningPreference = $PreviousWarningPreference

 

And if you like pretty columns then replace the Select line with this:

Select @{label="Storage Account";expression={$_.context.StorageAccountName}},
       @{label="Container";expression={$container}},
       name,
       blobtype,
       @{label="Bytes";expression={"{0,20:N0}" -f $_.length}} |

 

There’s got to be a better way to do this. So post any better solution as a comment!

 

Total storage?

Replace the Format-Table line with “Measure-Object -Property length -Sum” and you can get a file count and total bytes. (Don’t combine this with the “pretty columns” change!)

.

1/03/2016

“Unable to display this Web Part” after XSLT Customization

The following applies to SharePoint 2013, 2016 and SharePoint Online.

 

The Error

“Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Microsoft SharePoint Foundation-compatible HTML editor such as Microsoft SharePoint Designer. If the problem persists, contact your Web server administrator.”

image

The above error shows up in SharePoint 2013 and later when doing multi-level grouping with Data Form Web Parts. With the removal of many of the design features from SharePoint Designer 2013, I doubt there are too many people creating new XSLT customization. But if you are upgrading from 2010 or trying to implement a 2010 style customization then you many run into this error.

 

The Cause and the Fix

If you dig into the error logs you will find that this is reported as “Error while executing web part: System.StackOverflowException”. Turns out though that it is a timeout of the XSLT parser and has been around for quite a while. The fix is pretty easy, if you are on premises, just run a tiny PowerShell script from the SharePoint Management Shell.

image

$farm = Get-SPFarm
$farm.XsltTransformTimeOut = 10
$farm.Update()

Some of the articles say to increase it from the default of 1 second to 5 seconds. In my test environment I typically needed to increase it to 10 seconds. If you monitor the CPU on the server you will see that at least on lab-type virtual machines that a page load where you have this issue does put quite a load on the CPU.

SharePoint Online?  No joy for you! It appears that the default is 1 second, and you can’t change it.

 

There’s been a number of articles on the topic, mostly in the SP 2010 world:

http://thechriskent.com/tag/xslttransformtimeout/

http://blogs.msdn.com/b/joerg_sinemus/archive/2012/03/07/xslt-and-timeout-problem-when-transforming-runs-more-than-one-second.aspx

and many more: http://www.bing.com/search?q=xslttransformtimeout+sharepoint

 

.

1/01/2016

Views.Add() Cannot find an overload for "Add" and the argument count: "8".

 

I wrote a PowerShell script to create views. It worked in PowerShell 3 on SharePoint 2013. It failed in PowerShell 2 on SharePoint 2010.

To play it safe, I copied values from an existing view to insure the correct data types…

$web = Get-SPWeb http://sharepoint/sites/training
$list = $web.lists["Announcements22"]
$v = $list.views[0]
$list.Views.add("TestView",$v.ViewFields,$v.query,30,$true,$false,$v.Type,$false)

image

As I can count to 8 (usually) and I made sure the data types were correct, I then wasted a lot of time google/binging the error message.

So I then did the obvious thing for a developer, I fired up Visual Studio. And… found that there’s a datatype weirdness on the ViewFields parameter! The Add method is expecting System.Collections.Specialized.StringCollection while the View object actually returns Microsoft.SharePoint.SPViewFieldCollection.

The type conversion works in PowerShell 3:

image

But does not work in PowerShell 2:

image

 

My workaround? 

Use a handy method built into the Microsoft.SharePoint.SPViewFieldCollection type: ToStringCollection().

image

I could also have created the StringCollection object and copied the strings over using ForEach.

image

 

So why did it work in PowerShell 3 and not PowerShell 2? No idea! I’m guessing the “smarts” behind type conversion has improved. While the two collection objects are different (SPViewFieldCollection includes a .View property), the Items collection of both contain simple Strings.

 

.

12/30/2015

SharePoint – Adding Dropdown Menus without the Publishing Feature

 

The following works in SharePoint 2013, 2016 (beta 2) and SharePoint Online.

 

One of the common reasons to enable the Publishing Feature was to get the Navigation page so you could have dropdowns in the Top Link bar. Turns out that in 2013 and later you can do this without the Publishing Feature.

The Trick!

The trick is drag and drop. Starting with SharePoint 2013 you have an “EDIT LINKS” button on the page. (No Top Link Bar or Navigation page needed!)

image

When you click EDIT LINKS you can add a new item to the menu by clicking the (+) button.

image

Then there’s the trick! Drag the new link just under the parent link. (I’ll drag this one under the “Subsite1” link.)

image

image

Click Save and now you have dropdowns!

image

 

What you don’t get…

The Publishing feature Navigation page lets you select options like Open in New Window and Audience selection.

But if all you need are some dropdowns…

 

.

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.