简体   繁体   English

从通过 SCCM 部署的 Powershell 脚本中运行 SFC.EXE

[英]Running SFC.EXE from within Powershell script deployed via SCCM

I'm trying to create a Powershell script that will be deployed to any node that is showing bad update health to automate some of the simple tasks without having to interrupt users during their workday.我正在尝试创建一个 Powershell 脚本,该脚本将部署到任何更新健康状况不佳的节点,以自动执行一些简单的任务,而无需在工作日中断用户。 The Powershell script works perfectly if ran from an elevated PS prompt.如果从提升的 PS 提示符运行,Powershell 脚本可以完美运行。 It also runs fine when the same script is deployed to a test machine via SCCM with one exception: it won't call SFC.EXE /SCANNOW .当通过 SCCM 将相同的脚本部署到测试机器时,它也可以正常运行,但有一个例外:它不会调用SFC.EXE /SCANNOW

I've tried using:我试过使用:

Start-Process -FilePath "${env:Windir}\System32\SFC.EXE" -ArgumentList '/scannow' -Wait -NoNewWindow

Start-Process -FilePath "sfc.exe" -ArgumentList '/scannow' -Wait -NoNewWindow

Start-Process -FilePath "${env:Windir}\System32\SFC.EXE" -ArgumentList '/scannow' -RedirectStandardOutput "C:\SFC-Out.log" -RedirectStandardError "C:\SFC-Err.log" -Wait -NoNewWindow

& "sfc.exe" "/scannow"

Invoke-Command -ScriptBlock { sfc.exe /scannow }

Again, all of these examples work exactly as intended when run from an elevated PS prompt, but fail when run from the deployed PowerShell script.同样,所有这些示例在从提升的 PS 提示符运行时完全按预期工作,但在从部署的 PowerShell 脚本运行时失败。 When I used the -RedirectStandardOutput, I checked the file SFC-Out.log and it read:当我使用 -RedirectStandardOutput 时,我检查了文件 SFC-Out.log 并显示:

"Windows Resource Protection could not start the repair service" “Windows 资源保护无法启动修复服务”

I think this is because SCCM runs programs/scripts in the SYSTEM context instead of a user context (or even an elevated user context, but SYSTEM is supposed to be higher than an elevated session).我认为这是因为 SCCM 在 SYSTEM 上下文而不是用户上下文(甚至是提升的用户上下文,但 SYSTEM 应该高于提升的会话)中运行程序/脚本。

Is there a way to accomplish this?有没有办法做到这一点? Sorry for the bad formatting, this is my first post on this site.抱歉格式错误,这是我在此网站上的第一篇文章。

A bit late but I encountered the same issue.有点晚了,但我遇到了同样的问题。 Not sure if this is the case for you but the cause was configuring the deployment of the script with SCCM to run as a 32 bit process.不确定您是否属于这种情况,但原因是使用 SCCM 配置脚本部署以作为 32 位进程运行。 The script was being deployed to 64 bit systems.该脚本被部署到 64 位系统。 When I unchecked "run as 32 bit process" in the deployment configuration SFC worked without an issue under the context of a System account.当我在部署配置中取消选中“以 32 位进程运行”时,SFC 在系统帐户的上下文中没有问题。

I created a package (not an application) in SCCM and had to use the redirect using the elusive sysnative folder for x64 machines:我在 SCCM 中创建了一个包(不是应用程序),并且必须使用用于 x64 机器的难以捉摸的 sysnative 文件夹来使用重定向:

https://www.thewindowsclub.com/sysnative-folder-in-windows-64-bit https://www.thewindowsclub.com/sysnative-folder-in-windows-64-bit

So it would be:所以它会是:

C:\Windows\Sysnative\SFC.EXE /SCANNOW C:\Windows\Sysnative\SFC.EXE /SCANNOW

What you have will work, just missing "-Verb RunAs" to elevate permissions.您所拥有的将起作用,只是缺少“-Verb RunAs”来提升权限。 So your cmdlet should read:-所以你的 cmdlet 应该是:-

Start-Process -FilePath "${env:Windir}\System32\SFC.EXE" -ArgumentList '/scannow' -Wait -Verb RunAs

I've been reading and searching online for this, the only answer so far is that It can't be run due to sccm using the system account.我一直在网上阅读和搜索这个,到目前为止唯一的答案是它无法运行,因为sccm使用系统帐户。 It's also the same behavior when trying to run winmgt .尝试运行winmgt时的行为也是相同的。

Fast forward to SCCM Current Branch 2109 and I was able to solve this problem by using the new Scripts feature built into SCCM.快进到 SCCM Current Branch 2109,我能够通过使用 SCCM 中内置的新脚本功能来解决这个问题。 Using & 'sfc.exe' '/scannow' works, and I can manually run this script against any device collection showing devices in error.使用& 'sfc.exe' '/scannow'有效,我可以手动运行此脚本,以针对任何显示设备错误的设备集合。 Start-Process -FilePath "sfc.exe" -ArgumentList "/scannow" -NoNewWindow -Wait works too. Start-Process -FilePath "sfc.exe" -ArgumentList "/scannow" -NoNewWindow -Wait也可以。

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

相关问题 CreateProcess 无法运行 sfc.exe? 为什么 - CreateProcess can not run sfc.exe ? why 未获取进程的输出 (SFC.exe /VERIFYONLY)。 Process.StandardOutPut.ReadToEnd 提供包含在各种空间中的 /n/0/n 的输出 - Not getting output of a process (SFC.exe /VERIFYONLY). The Process.StandardOutPut.ReadToEnd is giving output with /n/0/n included in various spaces 从.bat中运行.exe - Running an .exe from within a .bat 从TaskScheduler调用的Powershell脚本调用一个exe - Powershell script called from TaskScheduler that calls an exe 在命令提示符 (cmd.exe) 中运行 psm1 PowerShell 脚本? - Running a psm1 PowerShell script in Command Prompt (cmd.exe)? 在SCCM 2007部署后,PowerShell脚本无法创建桌面快捷方式吗? - Powershell Script not creating shortcut to desktop after SCCM 2007 deployment? 在python脚本中运行powershell脚本,如何让python在运行时打印powershell输出 - Running powershell script within python script, how to make python print the powershell output while it is running Powershell SCCM 客户端模块 - Powershell module for SCCM Client 通过 Chef 运行 Powershell 脚本 - 转义字符语法无效 - Running Powershell Script via Chef - Invalid escape character syntax 从PowerShell语法错误中运行Python脚本 - Running Python Script from PowerShell Syntax Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM