目前正在起草一个RMM脚本。我正在使用Get-MpThreat powershell命令检查Windows在设备上发现的任何威胁。如果没有找到任何威胁,则返回0代码,意味着没有任何问题。如果它返回例如"1",则打印出输出并返回该代码。
我已经集思广益了一两个小时来讨论如何处理这个问题,而我想出的唯一一件事正好相反。我之前发现的链接如下所示。
powershell: if result is empty after a command, out some text
这是我目前所拥有的,但它打印出了错误的东西。我的防御者确实检测到了威胁,所以它应该打印错误代码1,而不是0。写输出只用于测试,不会出现在最终脚本中。
#$ThreatDetection = $null;
$ThreatDetection = Get-MpThreat
if ($ThreatDetection -ne $null){
$exitcode = 0
function ExitWithCode { param($exitcode) $host.SetShouldExit($exitcode)
exit }
Write-Output $exitcode
}
else {
$exitcode = 1
function ExitWithCode { param($exitcode) $host.SetShouldExit($exitcode)
exit}
Write-Output $exitcode
}而且,我不是最棒的powershell,python是我的面包和黄油,但对于Powershell却不能这么说。任何帮助都是非常感谢的。谢谢!
编辑:好的,所以我意识到,只要多点谷歌搜索,我就可以得到解决方案。这是我最后的解决方案,它似乎正在为我工作。
#$ThreatDetection = $null;
$ThreatDetection = Get-MpThreat
if ($ThreatDetection -eq $null){
$exitcode = 0
function ExitWithCode { param($exitcode) $host.SetShouldExit($exitcode)
exit }
}
else {
$exitcode = 1
function ExitWithCode { param($exitcode) $host.SetShouldExit($exitcode)
exit}
}第二编辑:不,这也不起作用。将返回代码更改为不同的值,它仍然从RMM的角度返回所有内容的0或1。
发布于 2022-02-06 09:20:40
您正在多次定义该函数,而不是使用它。
function ExitWithCode {
param($exitcode)
$host.SetShouldExit($exitcode)
exit
}
$ThreatDetection = Get-MpThreat
if ($ThreatDetection -eq $null){
ExitWithCode(0)
}
else {
ExitWithCode(1)
}但是TBH,我不知道你从哪得到这个功能的。这似乎完全没有必要。
Exit接受返回代码作为参数
这应该是所有必要的:
$ThreatDetection = Get-MpThreat
if ($ThreatDetection -ne $null){
Exit 1
}你不需要Exit 0。这是默认的退出代码。
https://stackoverflow.com/questions/71190588
复制相似问题