简体   繁体   中英

How can a PowerShell script save state then continue after a computer reboot?

Im starting by talking about my current Powershell script(s) - but this will turn out to be a more a C# issue...

I have 3 Powershell scripts that all work as expected. The first one includes a GUI that allows the user to select a variety of functions. Once the functions are selected, it runs through each one sequentially. However there are a couple that require a restart of the local machine. Such as Renaming the PC & Joining a Domain.

The 'restarting' portion is not too troublesome, as - when appropriate - the script sets a RunOnce registry key, which in turn calls the 2nd script after the reboot is complete. If necessary (depending on the original options selected), it sets another RunOnce key to call the 3rd script after another reboot.

My problem thus far is how to save variables/states for Post-reboot processes.

Say the user selects 5 options...

-Rename the machine -Join Domain -Disable Hibernation -Configure WSUS -Reset Indexing

My 3rd script contains functions for those last 3 items + others. I want that 3rd script to "know" which options/functions were originally selected and to only run those - not the others that have NOT been selected.

Now i started writing this in PS, because most of my tasks are System Configuration and i felt like PS was the more appropriate language for this. However, i cant find a way to complete this the way i want. I have 'limited' experience in C# - enough to do all of the functions i want - Or to at least use C# as a wrapper that calls PS scripts. But im unsure of how i can save the state of the selected items that can get called on any subsequent reboots.

Appreciate any responses.

Thanks! Mike

If all you are trying to do is save a few variables and their values so that you can re-load them later, you could likely accomplish this by writing them to file, and then re-loading that file when the script runs. It would need to know to check for that file.

I don't know anything about RunOnce keys, but perhaps you could add a parameter to the script call so that a recovery file is supplied to it.

Something like this:

This script takes in 3 parameters, then writes them to a file in JSON format.

param (
    [Parameter()][string]$foo,
    [Parameter()][string]$bar,
    [Parameter()][int]$baz
)

Write-Host "Do some stuff"

Write-Host "Save state before rebooting"

$state = @{
    'foo' = $foo;
    'bar' = $bar;
    'baz' = $baz;
};

$state | ConvertTo-Json | Set-Content -Path resume.json

This script takes 1 parameter, a file path pointing to the json file you just created with the previous script, it populates the variables and then spits them out:

param (
    [Parameter()][string]$StateFile
)

if ($StateFile) {
    $state = Get-Content -Path $StateFile | ConvertFrom-Json
    $foo = $state.foo;
    $bar = $state.bar;
    $baz = $state.baz;
}

Write-Host "foo: '$foo' | bar: '$bar' | baz: '$baz'";

Note: This script will work well for saving/loading simple value types, like strings, numbers, dates, etc. But if you are hoping to store entire objects, then you may need to look into other solutions. Or come up with ways to store certain pieces of information that would allow you to recover that object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM