AHK (PowerShell) - turn on System Protection

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

AHK (PowerShell) - turn on System Protection

23 Apr 2017, 09:50

There are times when PowerShell can do 'stuff' that AHK can't (or not easily) and I've successfully wrapped PowerShell code in AHK many times before. However, I've run into a problem which, on the face of it, should be very easy but I just can't understand why it's not working.

Background: Windows 10, for some unfathomable reason, has System Protection (i.e. System Restore) turned OFF by default. It's easy to turn back on again manually... but every automatic 'upgrade' turns it OFF again. I use an AHK 'Win 10 configurator' script so I don't have to spend ages on new installs remembering where all the settings are stored. I want to add a method to turn System Protection ON and configure the maximum amount of storage space to set aside for system restore points.

This is simplicity itself using an elevated PowerShell console:

Code: Select all

Enable-ComputerRestore -drive "C:\"
vssadmin resize shadowstorage /on=c: /for=c: /maxsize=10%
Using AHK I can wrap the PowerShell code in the same way I've used loads of times before. I know I have to run it as elevated so I've tried the following:

Code: Select all

If Not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
   ExitApp
}

psScript =
(
Enable-ComputerRestore -drive "C:\"
vssadmin resize shadowstorage /on=c: /for=c: /maxsize=10%
)

; Use this call if you want to see PowerShell output
; -NonInteractive supresses the 'Cannot load PSReadline module. Console is running without PSReadline.' module error
; -NoExit prevents the PowerShell console from closing automatically so is useful for testing code
Run powershell.exe -NonInteractive -NoExit -Command %psScript%
This fails completely. Enable-ComputerRestore -drive "C:\" just doesn't work and there's an issue with vssadmin resize shadowstorage /on=c: /for=c: /maxsize=10%. The IDE (I'm using SciTE4AHK) shows an issue with the % sign and, if I run the code anyway, AHK shows an error of 'This parameter contains a variable name missing its ending percent sign'.

So... does anyone know why the first PowerShell command-line doesn't work when wrapped in AHK and, secondly, how do I re-write the second command-line so the last parameter is not interpreted as a variable?

All suggestions/explanations, including working alternative methods, gratefully received ('cos I've spent hours so far trying to figure this out).
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: AHK (PowerShell) - turn on System Protection

23 Apr 2017, 11:01

Escape the % with a backtick:

Code: Select all

psScript =
(
Enable-ComputerRestore -drive "C:\"
vssadmin resize shadowstorage /on=c: /for=c: /maxsize=10`%
)
HTH
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: AHK (PowerShell) - turn on System Protection

23 Apr 2017, 11:20

Xtra wrote:Escape the % with a backtick:

Code: Select all

psScript =
(
Enable-ComputerRestore -drive "C:\"
vssadmin resize shadowstorage /on=c: /for=c: /maxsize=10`%
)
HTH
My apologies, I should have mentioned that I had already tried escaping the % sign using a backtick, only to get a different error of "This parameter contains a variable name missing its ending percent sign."

I understand that I need to pass the whole command-line with its parameters as 'literal' but the normal method of escaping characters doesn't work when wrapping a PowerShell command-line in AHK... so I'm a bit flummoxed. :(
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: AHK (PowerShell) - turn on System Protection

27 Apr 2017, 19:12

I don't know PowerShell in the slightest so cannot tell you why your code is failing on that end (assuming PS is the problem) and I'm not well versed on the intricacies of AutoHotkey syntax so I have no idea what's going on there. The good news, however, is that you don't need PowerShell to achieve what you want.

Enable-ComputerRestore hits up WMI to enable System Protection for the given drive. You can do the same in AutoHotkey with just a single line and avoid the PowerShell overhead: ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:SystemRestore").Enable("C:\")

vssadmin is a bog-standard program - just Run it as normal.
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: AHK (PowerShell) - turn on System Protection

29 Apr 2017, 11:54

qwerty12 wrote:Enable-ComputerRestore hits up WMI to enable System Protection for the given drive. You can do the same in AutoHotkey with just a single line and avoid the PowerShell overhead: ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:SystemRestore").Enable("C:\")
Thank you... that works perfectly!
qwerty12 wrote:vssadmin is a bog-standard program - just Run it as normal.
Hmm... I've had no success so far. If I drop vssadmin.exe resize shadowstorage /on=c: /for=c: /maxsize=10% into the Start > Run dialog it doesn't work but if I drop it into an elevated command prompt then it works as expected, instantly. This suggests I should run it in AHK using RunAs and %comspec%. After a couple of errors which looked like the script couldn't find vssadmin.exe I decided to expand the full filepath to vssadmin but with no luck. So I decided to change the maxsize parameter from 10% to 10GB as I was having no joy escaping the % sign.

Code: Select all

; Prompt to 'Run as Admin', i.e. show UAC dialog
If Not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
   ExitApp
}

ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:SystemRestore").Enable("C:\") ; works perfectly
ToRun=%windir%\System32\vssadmin.exe resize shadowstorage /on=c: /for=c: /maxsize=10GB
;MsgBox, %comspec% %ToRun% ; used for testing output is correct
Run, %comspec% %ToRun% ; doesn't work
;Run, %ToRun% ; doesn't work
If I run the script from my main script repository then System Protection is turned on but I get a command-line error (in the CMD console running as Administrator) that CMD was started with the above path as the current directory. UNC paths are not supported. Defaulting to Windows directory. OK, that's fair enough.

If, however, I copy the script to the desktop of the PC itself and run it then System Protection is turned on and an elevated console appears (pointing to C:\Users\<mylogin>\Desktop) but nothing happens.

Even if I copy the script to C:\Windows\System32 and run it, it still doesn't work... it just says it can't find the script file.

I'm obviously doing something stupid but I don't know what.
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: AHK (PowerShell) - turn on System Protection

29 Apr 2017, 12:18

RickC wrote:Thank you... that works perfectly!
Good to hear :-)
Hmm... I've had no success so far.
Your script is already elevated AFAICT by virtue of the "If Not A_IsAdmin" block. I was going to say the problem might be using %windir% instead of A_WinDir, but you probably don't have #NoEnv set... I'm the opposite of you in this case: I keep System Restore, Volume Shadow Copies etc. through group policy, so I'm kinda reluctant to properly run this.

This might work - at least my task manager seems to show a valid-looking command line:

Code: Select all

ToRun := A_WinDir . "\System32\vssadmin.exe resize shadowstorage /on=c: /for=c: /maxsize=10%"
Run % ToRun,, UseErrorLevel
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: AHK (PowerShell) - turn on System Protection

29 Apr 2017, 12:29

[quote="qwerty12"This might work - at least my task manager seems to show a valid-looking command line:

Code: Select all

ToRun := A_WinDir . "\System32\vssadmin.exe resize shadowstorage /on=c: /for=c: /maxsize=10%"
Run % ToRun,, UseErrorLevel
[/quote]

Unfortunately, no... it wasn't successful.
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: AHK (PowerShell) - turn on System Protection

29 Apr 2017, 12:47

RickC wrote:Unfortunately, no... it wasn't successful.
Run *runas %comspec% /k "%A_WinDir%\System32\vssadmin.exe" resize shadowstorage /on=c: /for=c: /maxsize=5`% works for me over here - if I run vssadmin list shadowstorage in the same command prompt, I get "Maximum Shadow Copy Storage space: 11.7 GB (5%)". Change the /k to a /c if you want cmd to close when done.
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: AHK (PowerShell) - turn on System Protection

29 Apr 2017, 13:33

qwerty12 wrote:Run *runas %comspec% /k "%A_WinDir%\System32\vssadmin.exe" resize shadowstorage /on=c: /for=c: /maxsize=5`% works for me over here - if I run vssadmin list shadowstorage in the same command prompt, I get "Maximum Shadow Copy Storage space: 11.7 GB (5%)". Change the /k to a /c if you want cmd to close when done.
I tried on 2 different systems (Win 10 Anniversary Update and Win 10 Creators Update). Each time I get Error: A Volume Shadow Copy Service encountered an unexpected error. Check the Application event log for more information.

The Application log on both PC's show the same info, i.e. Error 0x80040154, Class not registered. This doesn't make sense (to me anyway) as the command vssadmin.exe resize shadowstorage /on=c: /for=c: /maxsize=5% works perfectly from an elevated CMD.

I'm using AHK Unicode 32-bit v1.1.24.1 on both PC's.

Edit: Just upgraded both to latest AHK Unicode 32-bit v1.1.25.1... no difference.
Last edited by RickC on 29 Apr 2017, 13:43, edited 1 time in total.
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: AHK (PowerShell) - turn on System Protection

29 Apr 2017, 13:39

RickC wrote:I'm using AHK Unicode 32-bit v1.1.24.1 on both PC's.
Oh. Does Run *runas %comspec% /k "%A_WinDir%\Sysnative\vssadmin.exe" resize shadowstorage /on=c: /for=c: /maxsize=5`% work?
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: AHK (PowerShell) - turn on System Protection

29 Apr 2017, 13:53

qwerty12 wrote:Oh. Does Run *runas %comspec% /k "%A_WinDir%\Sysnative\vssadmin.exe" resize shadowstorage /on=c: /for=c: /maxsize=5`% work?
YES! It works perfectly! Thank you! I didn't understand the significance of adding Sysnative to the filepath for vssadmin.exe at first but a search showed this is used as a file redirector because I'm using a 32-bit version of AHK in a 64-bit environment. I had no idea about this at all so shall have to remember it in future.

Thank you once again. :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, mmflume and 161 guests