【问题标题】:Trying to delete a file with Powershell script ran by a service but issues with uac尝试使用服务运行的 Powershell 脚本删除文件但 uac 出现问题
【发布时间】:2014-10-28 12:56:23
【问题描述】:

我遇到了一个脚本问题,该脚本将删除服务器 C 驱动器上的文件。脚本如下所示:

param(
[Parameter(Mandatory=$true)][string]$hostname
)

$StrFileName = "C:\Program Files\NSClient++\nsclient.log"
$LogLocal="c:\Nagios\naf_delete_nscp_log_file.log"

$Date = Get-Date -Format "yyyy-MM-dd hh:mm:ss"
"$date : NSClient logfile deletion requested on $hostname" | Out-File -filepath     $LogLocal -Append

If (Test-Path "$strFileName"){
    Remove-Item $strFileName -Force
$Date = Get-Date -Format "yyyy-MM-dd hh:mm:ss"
"$date : NSClient logfile deleted on $hostname" | Out-File -filepath $LogLocal -Append
}

该脚本从 Nagios 快速操作启动,该操作将使用 nrpe 将主机作为参数传递,并使作为本地系统运行的 nscp 服务运行它。它在没有启用 uac 的服务器上完美运行,但我无法让它在具有 uac 的服务器上运行。 我尝试了许多选项,但似乎没有一个有效。非常感谢任何提示或建议在启用了 uac 的服务器上运行此功能。

谢谢。

威廉

【问题讨论】:

  • nsclient.ini 中的条目是什么样的?您应该能够将 -Verb runAs 添加到 powershell 包装器 ps1 = cmd /c echo scripts\\%SCRIPT% %ARGS%; exit($lastexitcode) | powershell.exe -command -Verb RunAs - 或您的 [/settings/external scripts/wrapped scripts] 定义中。
  • Remove-Item 有凭证参数,或许你可以这样解决
  • atm 看起来像这样:naf_delete_nscp_log_file=cmd /c echo scripts\naf_delete_nscp_log_file.ps1 $ARG1$;退出 $LastExitCode | powershell.exe -command - 我将尝试使用 -Verb RunAs
  • @Matt 似乎 -verb RunAs 确实成功了。谢谢!

标签: powershell uac nagios access-denied


【解决方案1】:

在您的 nsclient.ini 文件中,我认为您只需要调整 PowerShell 的包装器或使用 -Verb RunAs 调整脚本本身的定义即可。例如,在包装器中,默认情况下你有这个:

ps1 = cmd /c echo scripts\\%SCRIPT% %ARGS%; exit($lastexitcode) | powershell.exe -command -

你可以用这个更新什么

ps1 = cmd /c echo scripts\\%SCRIPT% %ARGS%; exit($lastexitcode) | powershell.exe -command -Verb RunAs -

来自 cmets 的更新

这个定义能代替使用包装器吗

naf_delete_nscp_log_file=powershell.exe -Verb RunAs -file scripts\naf_delete_nscp_log_file.ps1 "$ARG1$"

【讨论】:

  • 不好意思,看来我错了。它还不行。 naf_delete_nscp_log_file=cmd /c 回显脚本\naf_delete_nscp_log_file.ps1 $ARG1$;退出 $LastExitCode | powershell.exe -command -Verb RunAs - 给出:一元运算符“-”后缺少表达式。在 line:1 char:2 + -
  • @user2961029 您在文件中的哪个标题下有该条目。看起来您已经合并了一个包装器和一个命令定义。另请参阅我的更新答案。
  • 在[/settings/external scripts/scripts]下
  • 将 [/settings/external scripts/scripts] 中的命令更改为您建议的新命令后 (naf_delete_nscp_log_file=powershell.exe -file scripts\naf_delete_nscp_log_file.ps1 "$ARG1$" -Verb RunAs)该脚本在 Nagios 中“永远”运行......并且日志文件不会被删除。编辑:超时后我得到这个:CHECK_NRPE: Received 0 bytes from daemon。检查远程服务器日志中的错误消息。
  • @user2961029 在命令行上运行它会发生什么?将 $ARG1$ 替换为其他内容以进行测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-25
  • 2011-08-08
  • 1970-01-01
  • 2011-12-17
相关资源
最近更新 更多