简体   繁体   English

从 Windows 服务设置注册表不起作用

[英]Setting Registry from windows service does not work

I am setting or reading reg key from a windows services which runs as local system.我正在从作为本地系统运行的 Windows 服务中设置或读取 reg 密钥。 But when I read or set the values in the Registry Editor they are not the same as when i read and set them from the windows services.但是当我在注册表编辑器中读取或设置值时,它们与我从 Windows 服务中读取和设置它们时的值不同。

If I execute the following command in powershell from the windows services or when i am logged in as a user the results differ as well.如果我从 Windows 服务中的 powershell 中执行以下命令,或者当我以用户身份登录时,结果也会有所不同。 Why?为什么? Is there a different LocalMachine for a LocalSystem account? LocalSystem 帐户是否有不同的 LocalMachine?

$DefaultUserName = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name "DefaultUserName").DefaultUserName
Write-Host $DefaultUserName 

I am using the following c# lines to execute the powershell script from the windows service:我正在使用以下 c# 行从 Windows 服务执行 powershell 脚本:

var process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe";
process.StartInfo.Arguments = "\"&'" + cacheFile + "'\"";
process.StartInfo.Verb = "runas";
process.Start();

You need to be careful of bitness.你需要小心比特。 On 64-bit Windows, 32-bit applications run using the 'Windows on Windows' subsystem, which by default uses different filesystem paths and registry paths.在 64 位 Windows 上,32 位应用程序使用“Windows on Windows”子系统运行,该子系统默认使用不同的文件系统路径和注册表路径。 You are executing powershell from the SysWOW64 folder, which means you are executing the 32-bit version, which will use the 32-bit registry hive.您正在从SysWOW64文件夹执行 powershell,这意味着您正在执行 32 位版本,它将使用 32 位注册表配置单元。

If you open up regedit, the 32-bit hive for your registry key is: Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon .如果您打开 regedit,您的注册表项的 32 位配置单元是: Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon I suspect you are getting/setting this value.我怀疑您正在获取/设置此值。

Please refer to How to access a 64Bit Registry key using 32Bit Powershell without Redirection to WOW6432Node for strategies you can take, including using the Sysnative rather than SysWOW64 version of powershell.请参阅如何在不重定向到 WOW6432Node 的情况下使用 32 位 Powershell 访问 64 位注册表项以了解您可以采取的策略,包括使用Sysnative而不是SysWOW64版本的 powershell。

If you need to support 32-bit and 64-bit operating systems, you will need to make sure you are using the appropriate technique.如果您需要支持 32 位和 64 位操作系统,则需要确保使用了适当的技术。

This answer has beautiful explanation of Windows built in accounts : The difference between the 'Local System' account and the 'Network Service' account?这个答案对 Windows 内置帐户有很好的解释: “本地系统”帐户和“网络服务”帐户之间的区别?

A limited service account that is very similar to Network Service and meant to run standard least-privileged services.与网络服务非常相似的受限服务帐户,旨在运行标准的最低特权服务。 However, unlike Network Service it accesses the network as an Anonymous user但是,与网络服务不同的是,它以匿名用户的身份访问网络

Completely trusted account, more so than the administrator account.完全受信任的帐户,比管理员帐户更受信任。 There is nothing on a single box that this account cannot do, and it has the right to access the network as the machine (this requires Active Directory and granting the machine account permissions to something)单机上没有这个账号做不到的事情,它有作为机器访问网络的权限(这需要Active Directory并授予机器帐户权限)

Also can you try running the service as SYSTEM?您也可以尝试以 SYSTEM 身份运行该服务吗?

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

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