Parsing error messages

I wanted to reduce the amount of verbiage in an error message. I came up with this.

$ScriptName = $MyInvocation.MyCommand.Name # gets name of the script that is running
# Introduce an error for testing
Stop-Process -Name invalidprocess -EA "SilentlyContinue"
If ($Error) {
$E1 = $E2 = $E3 = $E4 = $E5 = " " #Sets variables to null.
$E1 = [string]$Error.Exception #Catches error
$E2 = $E1.Split(":") # divides the string at the :
$E3 = $E2[1] # Selects the part of the string after the :
$E4 = $E3.Split("\.") # divides the result at the period
$E5 = $E4[0] # selects the first part of of the string
# Write-Log -Message "$ScriptName $E5" -Path $LogPath warn # writes the result to the log
# Break # exits the script
Write-Host $E5

For some reason the above script has stopped working. It appears that something has changed in PS ver 2 on Windows 7 at least.
If the above does not work try this:


Function err {
If ($Error) {
$E1 = $E2 = $E3 = $E4 = " " #Sets variables to null.
[string]$E1 = $Error #Catches error
[string]$E2 = $E1.split(":") # divides the string at the ":"
$E3 = $E2.split(".") # divides the string at the "."
$E4 = $E3[0] # Selects the part of the string before the "."
Write-Host $E4


trouble re-installing Microsoft CRM 2013 ( “IIS URL Rewrite Module”)

I am working in an environment with several servers running MSCrm 2013. Three of these servers had there D and E drives wiped in error. Unfortunately, CRM was installed on the D: dive. When I tried to re-install CRM I kept getting an error about the UrlRewriteModule. I tried repairing it and I even reinstalled it, but that did not help. Finely, I uninstalled it and then installed CRM. That time it worked. It seems that CRM wants to install the UrlRewriteModule itself. So, if you are having similar problems with any of the Prerequisites, I would recommend going to the control panel and uninstalling them and letting CRM re-install them. I have found that if I get a 500.19 error when I first try to connect to the CRM web page that repairing the UrlRewriteModule fixes that problem.

Audit Folders/Shares and Export to CSV file

# NAME: AuditPerms2CSV.ps1
# AUTHOR: OldDog
# DATE : 11/12/2014
# COMMENT: Audit Folders/Shares and Export to CSV file
# This saves the results in a directory called C:\powershell\results
# if you don't have that directory it will create one.
# It also deletes any existing copy of Result.csv so if you want to save them rename them.
# ==============================================================================================
$path = $args[0]

if (! $args ) {
$path = Read-Host “Please enter the full path name you wish to check, like this: \\FileServer\share\user”


If (Test-path “C:\Powershell\Results”) {
If (Test-path “C:\Powershell\Results\Result.csv”) {
del “C:\Powershell\Results\Result.csv” }
Else {
New-Item -Path “C:\Powershell\Results” -ItemType directory

Add-Content “C:\Powershell\Results\Result.csv” “Folder Path:, Users/Groups:,Permissions:,Permissions Inherited:”

####Change the path to the folder or share you want NTFS perms on####
$dirToAudit = Get-ChildItem -Path $path -recurse | Where {$_.psIsContainer -eq $true}

foreach ($dir in $dirToAudit)
$colACL = Get-Acl -Path $dir.FullName

foreach ($acl in $colACL)
If ($acl.Access | ? inheritanceflags -eq ‘none’){
$In = $false } Else {$In = $True }

foreach ($accessRight in $acl.Access)
$DirName = $dir.FullName
$ar = $($AccessRight.FileSystemRights)
$id = $($AccessRight.IdentityReference)
Add-Content “C:\Powershell\Results\Result.csv” “$dirName ,$id,””$ar””,$In” #Note the “” to escape the ‘,’ in the permissions
$In = $null


Using PowerCLI to find all the VM’s with ISO’s attached

#Create an instance of the Excel.Application
$xl = New-Object -comobject excel.application
$xl.Visible = $true
$xl.DisplayAlerts = $False
$wb = $xl.Workbooks.Add()
$ws = $wb.Worksheets.Item(1)
$ws1 = $wb.worksheets | where {$ -eq "Sheet1"} #<------- Selects sheet 1

$ws1.Cells.Item(1,1) = “VM”
$ws1.Cells.Item(1,2) = “CD Drive ISOPath”
$row = 2

$vms = get-vm | select Name | sort
foreach ($i in $vms) {
$Item = [string]$i
$g = $Item.Split(“=”)
$Name = $g[1].Trimend(“}”)
$CDConnected = Get-CDDrive $Name | where { ($_.ConnectionState.Connected -eq “true”) -and ($_.ISOPath -like “*.ISO*”)}
If ($CDConnected -ne $null) {
$ws1.Cells.Item($row,1) = $Name
$ws1.Cells.Item($row,2) = $CDConnected.IsoPath


My New Thing: Desired State Configuration

I have been working on a contract for about four months now. Originaly I was tasked with provisioning about 500 servers at a new data center using vmWare. I started doing them one at a time using the vmWare front end. I soon was persauded to investigate powerCli and I was able to script the installs. That is much better and faster. In the mean time, a couple of other guys were working on this thing called DSC or Desired State Configuration. This is a Powershell V4 script that automates your server configuration and if done correctly, keeps the configuration intact. If you want to know about this in more detail check out the *”FREE”* “DSC BOOK” at once there Access all of their free eBooks in their Public OneDrive Folder.

Both of those guys left and I was tasked with finishing the project.

I have had many adventures getting this stuff to work an I plan on detailing some of them in future posts.

Get SharePoint Site Owner and/or Site Admin

One of the things you might have to do as an admin in a large organization is to find out if a particular user is a SharePoint Site Owner and Admin. I had to do this when a long time SharePoint developer left the company.

They are not the same thing, no matter what you may read.
You can find a discusion on the difference here:

This script will sort through all the owners and members of the site admin group on a given server. It will locate your user if they are an Owner or an admin on any SharePoint site on that server.

It uses the SharePoint snapin and at least where I work, I need to run it as the Farm Administrator.
The output is a tab delimited file that opens up nicely in Excel.


Function SiteAdmin($site){
$siteUrl = $site.url
$rootSite = New-Object Microsoft.SharePoint.SPSite($siteUrl)
$spWebApp = $rootSite.WebApplication

foreach($site in $spWebApp.Sites)
    foreach($siteAdmin in $site.RootWeb.SiteAdministrators | where {$_.LoginName -Like $user})
	  Add-Content -Path $strPath "`t`t$($siteAdmin.ParentWeb.Url) `t $($siteAdmin.DisplayName) `t $($siteAdmin.LoginName)"

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$user = Read-Host "Input user name or part thereof to find [Contoso\User*]"
$date = Get-Date
$comp = $env:computername
$strPath = $filename = "E:\Reports\users.txt"
IF(Test-Path $filename) { Remove-Item $strPath }
Add-Content -Path $strPath "$User Owner and\or Admin `t$comp `t$date`n"
Add-Content -Path $strPath "Site.Url `tSite Owner `tParentWeb Url `tSiteAdmin DisplayName `tSiteAdmin LoginName"

$Sites = Get-SPSite -Limit All
ForEach ($site in $Sites) {
	If ($site.owner -Like $user) {
	Add-Content -Path $strPath "$($Site.Url) `t $($Site.Owner)"


My SharePoint 2010 Powershell tutorial for SharePoint 200 developers

Serge Luca's Blog (SharePoint MVP)

Here is an extract of the Powershell module of my  SharePoint 2010 for developers course.


1.1 lab.introduction to Powershell

In this lab, you will work out a number of exercises that have to do with Powershell.

In Lab1 you will discover the main Powershell concepts; in Lab 2, you will apply these concepts.

1.1.1 Lab 1.discovering Powershell

1. Start the Sharepoint 2010 Management Shell.


2. To get help, type:


3. To see all the commands available, type :

4. To see how to use Get-Command, type get-help get-Command; you will notice the Noun option:


5. To get the list of all Sharepoint commands, type:

You will notice familiar cmdlets like Get-SPUser, Get- SPWeb, Get- SPSite,…

Sharepoint commands are organized as verb-noun like Enable-SPFeature.

You can type Get-Command –Noun SPFeature*or Get-Command –verb Enable.

6. In Powershell, there…

View original post 1,291 more words