运行一次未在启动时运行



我的启动脚本在重新启动时未运行时遇到问题。

我正在尝试让我的系统执行一堆脚本,但在每个脚本运行后重新启动,等等。此脚本将设置注册表项,但在自动登录后,Powershell 根本不运行。

#Sets Autologin for scripts
$RegPath = "HKLM:SOFTWAREMicrosoftWindows NTCurrentVersion Winlogon"
$DefaultUsername = Read-Host -Prompt "User's O365 Cred"
$DefaultPassword = Read-Host -Prompt "User's O365 Pass"
Set-ItemProperty $RegPath "AutoAdminLogon" -Value "1" -type String
Set-ItemProperty $RegPath "DefaultUsername" -Value "$DefaultUsername" -type String
Set-ItemProperty $RegPath "DefaultPassword" -Value "$DefaultPassword" -type String

#Reboots and starts next script
$RunOnceKey = "HKLM:SoftwareMicrosoftWindowsCurrentVersionRunOnce"
set-itemproperty $RunOnceKey "NextRun" ('C:WindowsSystem32WindowsPowerShellv1.0Powershell.exe  -File ' + "~Downloadssystemrename.ps1")
Start-Sleep 10
Restart-Computer

提前感谢!!

通过计划任务的替代解决方案:

## Create the action
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-Command "c:tempsystemrename.ps1"'
## Set to run as local system, No need to store Credentials!!!
$principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITYSYSTEM" -LogonType ServiceAccount -RunLevel Highest
## set to run at startup could also do -AtLogOn for the trigger
$trigger = New-ScheduledTaskTrigger -AtStartup
## register it (save it) and it will show up in default folder of task scheduler.
Register-ScheduledTask -Action $action -TaskName 'mytask' -TaskPath '' -Principal $principal -Trigger $trigger

请注意,所有这些命令都支持通过 cimsession 进行远程处理,如下所示:

## Create remote cimsession
$cimSession = New-CimSession -ComputerName 'computername'
## Create the action
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-Command "c:tempsystemrename.ps1"' -CimSession $cimSession
## Set to run as local system, No need to store Credentials!!!
$principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITYSYSTEM" -LogonType ServiceAccount -RunLevel Highest -CimSession $cimSession
## set to run at startup could also do -AtLogOn for the trigger
$trigger = New-ScheduledTaskTrigger -AtStartup -CimSession $cimSession
## register it (save it) and it will show up in default folder of task scheduler.
Register-ScheduledTask -Action $action -TaskName 'mytask' -TaskPath '' -Principal $principal -Trigger $trigger -CimSession $cimSession
## clean up cimsession
Remove-CimSession -CimSession $cimSession

您是否检查过 GPO 中的以下设置是否不适用?

计算机配置\管理模板\系统\禁用运行一次列表 (见 https://msdn.microsoft.com/en-gb/library/ms815142.aspx(

当我通过 GPO 应用此设置并尝试使用 runonce reg 键时,我输入的值在重新启动后仍然存在,表明 Windows 甚至没有尝试运行它。

从组策略管理(或您最喜欢的执行 RSoP [策略结果集] 的方法(执行组策略结果将显示应用于计算机/用户的所有 GPO,以允许您缩小应用设置的 GPO 的范围。

尝试将Set-ItemProperty行更改为:

Set-ItemProperty -Path $RunOnceKey -Name 'NextRun' -Value 'C:WindowsSystem32WindowsPowerShellv1.0Powershell.exe -executionpolicy Bypass -File "~Downloadssystemrename.ps1" '

可能是执行策略阻止了文件执行。

根据MS的说法,runonce用户登录时触发,而不是启动。根据 MS 文档:

运行和 RunOnce 注册表项会导致程序在用户每次登录时运行。键的数据值是不超过 260 个字符的命令行。通过添加 description-string=commandline 形式的条目来注册要运行的程序。您可以在一个键下写入多个条目。如果在任何特定键下注册了多个程序,则这些程序的运行顺序是不确定的。

来源:https://learn.microsoft.com/en-us/windows/win32/setupapi/run-and-runonce-registry-keys

执行此操作的唯一方法是创建上述计划任务或服务。

上面文档中曾经有一个RunServicesOnce注册表项,但显然它与Windows ME一起死亡。