0

Is there another way to get the last restore point's name in C# other than using powershell?

Here is the code I'm currently using:

private void Rp_MouseEnter(object sender, MouseEventArgs e)
    {
        try
        {
            using (PowerShell PowerShellInstance = PowerShell.Create())
            {
                PowerShellInstance.AddScript("((Get-ComputerRestorePoint)[-1]).Description");
                foreach (var o in PowerShellInstance.Invoke())
                {
                    rp.ToolTip = o.ToString();
                }
            }
        }
        catch (Exception)
        {
            rp.ToolTip = "No previous Restore Points found";
        }
    }

The

((Get-ComputerRestorePoint)[-1]).Description

doesn't really work most of the times . I have tried it in powershell too and it only works when the Restore Point is freshly created, so I'm getting the exception's message most of the time since nothing is retrieved.

LE: Everyhing is run as admin. That's not the issue. The problem is that the command works after a restore point is freshly created, but after some time it fails to produce any output, even though you can find the created restore points using Get-ComputerRestorePoint

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
erma86
  • 75
  • 1
  • 10
  • Get-ComputerRestorePoint works, ((Get-ComputerRestorePoint)[-1]).Description doesn't – erma86 Jan 15 '19 at 19:19
  • Your command predictably fails when `Get-ComputerRestorePoint` produces no output; use `Get-ComputerRestorePoint | Select-Object -Last 1 | Select-Object -ExpandProperty Description` instead. That is, assuming the output objects do have a `.Description` property. – mklement0 Jan 15 '19 at 19:20
  • Will try, but as I said the Get-ComputerRestorePoint never fails, only the filtering command I already posted. – erma86 Jan 15 '19 at 19:24
  • 1
    What do you see for a description in powershell when you run `$(Get-ComputerRestorePoint)[-1]`. It's possible there isn't a description for the last item. – Rufus L Jan 15 '19 at 19:31
  • 1
    Are you running it as an administrator? If not, that is most likely the issue here (I see no results unless I run it as admin). – Rufus L Jan 15 '19 at 19:41
  • To add to @RufusL's comments: please update your question (directly, not in a comment) with what, specifically, doesn't work - expected vs. actual result. While my previous comment is correct in principle, use of `.AddScript()` would actually "eat" the `Cannot index into a null array` error that PowerShell reports, so the case of empty `Get-ComputerRestorePoint` output shouldn't be a problem. – mklement0 Jan 15 '19 at 19:42
  • done. as mentioned before Get-ComputerRestorePoint is not the issue, it displays everthing as it should – erma86 Jan 15 '19 at 20:06
  • [Microsoft's Documentation](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-computerrestorepoint?view=powershell-5.1) says "This cmdlet uses the WMI SystemRestore class." so I would suggest [querying WMI](https://stackoverflow.com/a/6264529/2557128) [SystemRestore](https://learn.microsoft.com/en-us/windows/desktop/sr/systemrestore). – NetMage Jan 15 '19 at 20:09

0 Answers0