noahlh / aws-automated-backup-powershell

A set of Windows PowerShell scripts to enable automated backups (via snapshots) on AWS EC2 Windows instances
Other
39 stars 27 forks source link

Runtime Exception on CleanupWEeklySnapshots #4

Open MTatersKT738 opened 9 years ago

MTatersKT738 commented 9 years ago

When I run the script the backups will work and I can see them in my console, however I get the following error. I need to be sure that despite the error that the cleanup part of the script will work as well.

11/24/2014 9:29:16 PM xxxx-AWS Production Daily Backup Starting 11/24/2014 9:29:16 PM Instance xxxx-AWS-DC (i-xxxxxxxb) Creating Snapshot 11/24/2014 9:29:16 PM Snapshot xxxx-AWS-DC vol-xxxxxxx Daily 20141124092916 Created for xxx-AWS-DC (i-xxxxxxxx) 11/24/2014 9:29:31 PM Cleaning up daily snapshots 11/24/2014 9:29:31 PM CleanupWeeklySnapshots : System.Management.Automation.RuntimeException: You cannot call a method on a null-valued expression. at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception) at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) 11/24/2014 9:29:31 PM WriteToLog : System.NotSupportedException: Specified method is not supported. at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue) 11/24/2014 9:29:31 PM WriteToLog : System.NotSupportedException: Specified method is not supported. at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue) 11/24/2014 9:29:31 PM WriteToLog : System.NotSupportedException: Specified method is not supported. at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue) 11/24/2014 9:29:31 PM WriteToLog : System.NotSupportedException: Specified method is not supported. at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue) 11/24/2014 9:29:31 PM WriteToLog : System.NotSupportedException: Specified method is not supported. at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue) 11/24/2014 9:29:31 PM WriteToLog : System.NotSupportedException: Specified method is not supported. at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue) 11/24/2014 9:29:31 PM WriteToLog : System.NotSupportedException: Specified method is not supported. at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue) 11/24/2014 9:29:31 PM WriteToLog : System.NotSupportedException: Specified method is not supported. at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue) 11/24/2014 9:29:31 PM WriteToLog : System.NotSupportedException: Specified method is not supported. at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue) 11/24/2014 9:29:31 PM WriteToLog : System.NotSupportedException: Specified method is not supported.

The last error repeats for probably 300 times then stops.

Any assistance would be appreciated.

Thanks, Jason

noahlh commented 9 years ago

Interesting. Off the top of my head, I'm not sure what's causing that, but a few guesses / things to check:

MTatersKT738 commented 9 years ago

I updated to the most recent version that I downloaded today. I am running 2012 R2 and I ran it as administrator and still got the error above.

Any other thoughts? It seems to be trying to delete and run through loop based on an array that is empty?

Thanks, Jason

MTatersKT738 commented 9 years ago

I performed additional troubleshooting and found where the error was. The script checks the description field of all snapshots for the string "Daily" or "Weekly". If the account has snapshots which do not have a description, the script will fail with the Null value exception. I would suggest building into the script a check for the NULL value in the description field and just ignore this snapshot.

noahlh commented 9 years ago

Excellent suggestion Jason - thank you! Want to take a stab at adding & testing this and submitting as a pull request? I'll happily guide you through that process if you'd like.

MTatersKT738 commented 9 years ago

I will try to put this on the schedule to look at eventually but I can not commit to anything near term, due to the large number of projects and ongoing deadlines. I will reach out when I have a chance and need assistance. Talk to you soon.

noahlh commented 9 years ago

No rush or obligation Jason - just if you have the time/interest. Many thanks!

wgknowles commented 9 years ago

Figured it out. Don't know how to submit code though... First off... The catch [exception] for function CleanupDailySnapshots says $function = "CleanupWeeklySnapshots" So that's misleading as heck!

then the fix:

function CleanupDailySnapshots { try { WriteToLog "Cleaning up daily snapshots" $deleteCount = 0 $snapshots = GetAllSnapshots foreach($snapshot in $snapshots) { $description = $snapshot.Description $snapshotID = $snapshot.SnapshotId if ($description -eq $null) { WriteToLog "$snapshot.SnapshotId has a null Description" $description = "null" } elseif($snapshot.Description.Contains("Daily")) { WriteToLog "checking if '$description' ($snapshotID) is expired" $backupDateTime = get-date $snapshot.StartTime $expired = IsDailySnapshotExpired $backupDateTime if($expired) { WriteToLog "$description ($snapshotID) Expired" DeleteSnapshot $snapshot.SnapshotId $deleteCount ++ } } }

WriteToLog "Total of $deleteCount daily snapshots deleted"

}
catch [Exception]
{
    $function = "CleanupDailySnapshots"
    $exception = $_.Exception.ToString()
    WriteToLogAndEmail "$function : $exception" -isException $true
    return false
} 

}

wgknowles commented 9 years ago

Note, you porobably need to fix this in function CleanupWeeklySnapshots as well.