PowerShell script exchange
Post Reply
WesJackson1
Novice
Posts: 4
Liked: never
Joined: Sep 04, 2012 10:11 am
Full Name: Wes Jackson
Contact:

Issues when filtering Restore Points

Post by WesJackson1 »

Hello,

I haven't logged this with support as yet as I think it's going to be an easy one for those with a bit more powershell knowledge than myself :)

But I am currently in the process of creating a small script to allow users to remotely start a replica failover from a specific restore point.

I've got to the point in the script where I want to create a variable that contains the date/time of the restore point and select this for failover. This variable comes from a batch file which is read after Powershell is called.

Below is the code relating to this certain point in the script file.

Code: Select all

$point = Get-Content C:\Text.txt | Select -Last 1
$restore = Get-VBRReplica -Name DR | Get-VBRRestorePoint | Select CreationTime | where {$_.CreationTime -eq $point}
Start-VBRReplicaFailover -RestorePoint $restore -Reason "Test"
But when I run the middle line of code it is not returning any restore points. If I change this to where {$_.CreationTime -ne $point} and output to file it returns all restore points including the one in which I am trying to select.

Just to confirm C:\Test.txt contains dates in the same format which is usually returned: 04/09/2012 11:11:11 for example.

Any ideas as to why this isn't working?

Thanks,
Wes
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Issues when filtering Restore Points

Post by tsightler » 2 people like this post

I'm not looking at the objects right at this moment, but I'm guessing that "CreationTime" is actually in some type of date format, while your "$point" variable is just a string. You will need to convert one of them so that they are the same type to make a valid comparison, perhaps something like:

Code: Select all

$restore = Get-VBRReplica -Name DR | Get-VBRRestorePoint | where {$_.CreationTime.ToString() -eq $point}
Note that with that code it would need to be an exact match, including any AM/PM indication if used in your locale.
WesJackson1
Novice
Posts: 4
Liked: never
Joined: Sep 04, 2012 10:11 am
Full Name: Wes Jackson
Contact:

Re: Issues when filtering Restore Points

Post by WesJackson1 »

That did it! You sir are amazing thanks for the help! I knew it would be something simple..

:)
WesJackson1
Novice
Posts: 4
Liked: never
Joined: Sep 04, 2012 10:11 am
Full Name: Wes Jackson
Contact:

Re: Issues when filtering Restore Points

Post by WesJackson1 »

OK, I spoke to soon I tried this straight away and it worked.

Now it doesn't?! :?: :?:

Here's the full code exactly how I am writing it:

Code: Select all

$date = Get-Content C:\Failover\Selected_Failover.txt | Select -Last 1
Get-VBRReplica -Name DR | Get-VBRRestorePoint | where {$_.CreationTime.ToString() -eq $date} | Out-File C:\Failover\Confirm_Failover.txt
Surely it's got to be something real small.

And just to confirm here is my test from Selected_Failover.txt

Code: Select all

04/09/2012 13:34:21
And also here is the results to show that there is a restore at that time :

Code: Select all

Get-VBRReplica -Name DR | Get-VBRRestorePoint | where  {$_.Name -eq "Exchange02"}

VM Name                   Creation Time          Type       Failoved
-------                   -------------          ----       --------
Exchange02                04/09/2012 13:34:21    Snapshot   False
Please help this is driving me mad! :|
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Issues when filtering Restore Points

Post by tsightler » 2 people like this post

So I just ran your code in my lab and it worked fine. Not really sure what you might be doing wrong as we are just converting the Date to a string, and then comparing the strings. You could try converting your string to a date and then actually comparing the two dates, maybe something like:

Code: Select all

$date = Get-Date (Get-Content C:\Failover\Selected_Failover.txt | Select -Last 1)
Get-VBRReplica -Name "Replicate View Server" | Get-VBRRestorePoint | where {$_.CreationTime.DateTime -eq $date.DateTime}
I'll admit, I'm not an expert on Powershell dates, but this also seemed to work in my lab. We read the file and convert it's contents to a datetime datatype, then compare that date with the dates of the restore points, rather than performing a string compare. This is probably the more "correct" way to do it anyway as it should work no matter the locale settings.
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Issues when filtering Restore Points

Post by tsightler » 2 people like this post

Actually, even better:

Code: Select all

$date = Get-Date (Get-Content C:\Failover\Selected_Failover.txt | Select -Last 1)
Get-VBRReplica -Name "Replicate View Server" | Get-VBRRestorePoint | where {$_.CreationTime.Date -eq $date.Date}
WesJackson1
Novice
Posts: 4
Liked: never
Joined: Sep 04, 2012 10:11 am
Full Name: Wes Jackson
Contact:

Re: Issues when filtering Restore Points

Post by WesJackson1 »

Yeah I have no idea myself as sometimes I could just re-type exactly the same thing and it would of worked.

But I have tried the first lines of code and this worked a charm. It will have to be the top one aswell as I do need the Date & Time otherwise it just pulls up all jobs for that day.

I've now fully tested it and it's working wonderfully.

Thanks again for your help it's really appreciated! Hopefully I'll start getting my head around how Powershell handles Strings etc in good time :)
Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests