Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用服务运行的Powershell脚本删除文件,但uac出现问题_Powershell_Uac_Nagios_Access Denied - Fatal编程技术网

尝试使用服务运行的Powershell脚本删除文件,但uac出现问题

尝试使用服务运行的Powershell脚本删除文件,但uac出现问题,powershell,uac,nagios,access-denied,Powershell,Uac,Nagios,Access Denied,我遇到一个脚本问题,该脚本将删除服务器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" "

我遇到一个脚本问题,该脚本将删除服务器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的服务器上工作,我们将不胜感激

谢谢


Willem

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

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 -
根据评论更新

这个定义能代替包装器工作吗

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

您的条目在nsclient.ini中是什么样子的?您应该能够将动词runAs添加到powershell包装中
ps1=cmd/c echo scripts\\\%SCRIPT%%ARGS%;exit($lastexitcode)| powershell.exe-command-动词RunAs-
或在您的
[/settings/external scripts/wrapped scripts]
定义中。
删除项
有一个凭证参数,也许您可以这样解决它:naf_delete_nscp_log_file=cmd/c echo scripts\naf_delete_nscp_log_file.ps1$ARG1$;退出$LastExitCode | powershell.exe-command-我将尝试使用-VerbRunAs@Matt看来,动词RunAs确实起了作用。非常感谢。是的,伙计,但看起来我错了。它还不起作用。naf_delete_nscp_log_file=cmd/c echo scripts\naf_delete_nscp_log_file.ps1$ARG1$;退出$LastExitCode | powershell.exe-command-Verb RunAs-Gives:一元运算符'-'后缺少表达式。在第1行char:2+-@user2961029,您的文件中有哪个条目。看起来您已经合并了包装器和命令定义。另请参阅我的更新答案。在[/settings/external scripts/scripts]下,将[/settings/external scripts/scripts]中的命令更改为您建议的新命令(naf_delete\u nscp\u log\u file=powershell.exe-file scripts\naf_delete\u nscp\u log\u file.ps1“$ARG1$”-动词RunAs)后,脚本将在Nagios中“永远”运行。。。而且日志文件不会被删除。编辑:超时后,我得到以下信息:CHECK\u NRPE:从守护进程接收到0字节。检查远程服务器日志中是否有错误消息。@user2961029在命令行上运行时会发生什么?出于测试目的,将$ARG1$替换为其他内容。