简体   繁体   English

PowerShell 脚本如何保存 state 然后在计算机重新启动后继续?

[英]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...我从谈论我当前的 Powershell 脚本开始 - 但这将成为一个更多的 C# 问题......

I have 3 Powershell scripts that all work as expected.我有 3 个 Powershell 脚本,它们都按预期工作。 The first one includes a GUI that allows the user to select a variety of functions.第一个包括一个GUI,它允许用户使用select 的各种功能。 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.例如重命名 PC 和加入域。

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. “重新启动”部分并不太麻烦,因为 - 在适当的时候 - 脚本会设置一个 RunOnce 注册表项,该注册表项会在重新启动完成后调用第二个脚本。 If necessary (depending on the original options selected), it sets another RunOnce key to call the 3rd script after another reboot.如有必要(取决于所选的原始选项),它会设置另一个 RunOnce 键以在再次重新启动后调用第三个脚本。

My problem thus far is how to save variables/states for Post-reboot processes.到目前为止,我的问题是如何保存重启后进程的变量/状态。

Say the user selects 5 options...假设用户选择了 5 个选项...

-Rename the machine -Join Domain -Disable Hibernation -Configure WSUS -Reset Indexing -重命名机器 -加入域 -禁用休眠 -配置 WSUS -重置索引

My 3rd script contains functions for those last 3 items + others.我的第三个脚本包含最后 3 个项目 + 其他项目的功能。 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.现在我开始用 PS 写这个,因为我的大部分任务都是系统配置,我觉得 PS 是更合适的语言。 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.我在 C# 方面的经验“有限” - 足以完成我想要的所有功能 - 或者至少使用 C# 作为调用 PS 脚本的包装器。 But im unsure of how i can save the state of the selected items that can get called on any subsequent reboots.但我不确定如何保存可以在任何后续重新启动时调用的所选项目的 state。

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.我对 RunOnce 键一无所知,但也许您可以在脚本调用中添加一个参数,以便为其提供恢复文件。

Something like this:像这样的东西:

This script takes in 3 parameters, then writes them to a file in JSON format.该脚本接受 3 个参数,然后将它们写入 JSON 格式的文件。

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:该脚本有 1 个参数,一个指向您刚刚使用之前的脚本创建的 json 文件的文件路径,它填充变量然后将它们吐出:

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.或者想出一些方法来存储某些信息,让您恢复 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 安装完成后提示重新启动计算机 - prompt to reboot computer after installation completed 如何使用powerShell保存c#应用程序的状态? - how to Save the State of an c# App with powerShell? 从调用后停用的脚本中调用协程后如何继续 - How can I continue Coroutine after calling it from a script that I deactivated after that call 如何使用 C# 在其中运行带有重命名计算机的 Powershell 脚本 - How to run a Powershell script with Rename-Computer in it using C# 调用InitiateSystemShutdownW之后,如何确定将来是否计划重新启动? - How can I detect if a reboot is scheduled in the future after calling InitiateSystemShutdownW? 重新启动计算机后c#加载程序设置 - c# load program settings after reboot computer 启动Powershell过程后继续执行代码 - Continue code execution after starting powershell proces 在其他计算机上以管理员权限运行PowerShell脚本 - Run a PowerShell script with Administrator privileges on other computer 如何在执行期间暂停、保存状态并稍后从同一点继续? - How do I pause during execution, save state, and continue from same point later on? 关闭 Outlook 插件后如何保存复选框的状态? - How can I save the state of a checkbox after closing my outlook AddIn?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM