Deleting old SP backup files

by bdenton at 2012-09-11 10:55:09

I am getting an “Unexpected Token” error in the “Save” line of this script. It is failing on the “(”. Everything I read says this is the correct way to do what I need to do. Any suggestions would be helpful.

$spbrtocXML = “d:\SharePointBackups\spbrtoc.xml”
$days = 7
[XML]$sharepointlist = gc $spbrtocXML
$oldbadckups = $sharepointlist.SPBackupRestoreHistory.SPHistoryObject|
? {$.SPStarTime -lt ((get-date).AddDays(-$days))}
$oldbackups | % {$sharepointlist.SPBackupRestoreHistory.RemoveChild($
$oldbackups | % {Remove-Item $.SPBackupDirectory -Recurse}
$sharepointlist.Save ($spbrtocXML)

by DonJ at 2012-09-11 14:23:44
Remove the space before the (.
by bdenton at 2012-09-12 08:39:18
When I do that it breaks the two lines above it. Error message is this:

Exception calling “RemoveChild” with “1” argument(s): "Object reference not set
to an instance of an object."
At D:\sharepointbackups\scripts\bak\cleanspbackups.ps1:15 char:68
+ $oldbackups | % {$sharepointlist.SPBackupRestoreHistory.RemoveChild <<<< ($
+ CategoryInfo : NotSpecified: (:slight_smile: [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

Remove-Item : Cannot bind argument to parameter ‘Path’ because it is null.
At D:\sharepointbackups\scripts\bak\cleanspbackups.ps1:18 char:29
+ $oldbackups | % {Remove-Item <<<< $.SPBackupDirectory -Recurse}
+ CategoryInfo : InvalidData: (:slight_smile: [Remove-Item], ParameterBinding
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,
by DonJ at 2012-09-12 08:46:15
So, doing that didn’t break those lines. The error with the space before the ( is one PowerShell catches when parsing the script, before it’s run. Once you fix that, PowerShell runs the script - and then finds the other errors. Those other errors are syntactical, like the first one was - they’re logic errors, so it can’t “see” them until it runs them.

The first indicates that $
doesn’t have anything in it at that point. That suggests $oldbackups doesn’t have anything in it.

The second also indicates that $_ doesn’t contain anything, again suggesting that $oldbackups is empty.

Because $oldbackups is being populated with the output of a Where-Object command, it’s possible you’re not getting any output. Modify your script to just output $oldbackups and see what’s in there.
by poshoholic at 2012-09-12 10:16:47
There is something else you should consider doing as well. When you assign a value to $oldBackups, your current script doesn’t set it up well in the event that the Where-Object clause filters everything out. As a result, if nothing comes back, $oldBackups gets assigned $null. If you pipe $null to ForEach-Object, then ForEach-Object still runs, which is why you see the errors you are seeing. This is an issue in PowerShell 2 that was fixed for PowerShell 3.

Instead you should explicitly indicate that you want $oldBackups to be a collection. By doing this, if nothing comes back from the Where-Object clause then you end up with an empty collection. When an empty collection is unwrapped at the start of a pipeline, PowerShell realizes that there is nothing in it and it doesn’t run the rest of the pipeline, so you won’t have issues if your collection happens to be empty.

That probably sounds really confusing, so I’ll give you a simple example. Consider this script, which was written assuming that I don’t have any Windows services that start with the letter “x”:

$services = Get-Service x*
$services | ForEach-Object {if ($_ -eq $null) {‘I received $null’} else {‘I received a service’}}

When I run this in PowerShell 2.0, I see the output “I received $null”, because the $services variable was assigned to $null.

Now look at this modified example, where I am explicitly indicating I want a collection:
$services = @(Get-Service x*)
$services | ForEach-Object {if ($_ -eq $null) {‘I received $null’} else {‘I received a service’}}

When I run this in PowerShell 2.0, I don’t see any output, because PowerShell properly recognizes that I am not sending anything down the pipeline to ForEach-Object.
by ToddKlindt at 2012-09-15 07:42:48
Good tip, Kirk.

by ToddKlindt at 2012-11-20 12:03:00
[quote=“princegrips”]I’m new to Windows PowerShell. Currently, I am using Windows XP. I have had no luck with downloading Windows PowerShell for Windows XP yet. Could you please give me a specific link so that I can download it? Thanks[/quote]

I don’t have any Windows XP boxes laying around, so I can’t really test any XP specific downloads. If I were doing this I’d just Google “PowerShell for Windows XP” and go from there.