我试图从 $IOC
中的数组中提取项目,然后对于每个项目替换 $API here-string 中的 $IMPORT 并将结果回显到控制台,然后对 $IOC
数组中的每个项目执行此操作。
#put IOC's into array
$IOC= ‘c:\Users\powershell_array.txt'
#api curl script with variable to be replaced
$API = @"
curl --insecure 'https://192.168.1.1:3000/hx/api/v2/indicators/Custom/Powershell_AD/conditions/execution' -X 'POST' --data-binary "
{
\"tests\":[
{
\"token\":\"processEvent/ActiveDirectory\",
\"type\":\"text\",
\"operator\":\"contains\",
\"preservecase\":false,
\"value\":\"$IMPORT\"
}
]
}" -H 'X-FeApi-Token: IAOaiq1s2' -H 'Accept: application/json' -H 'Content-Type: application/json'"
"@
ForEach ($i in Get-Content $IOC) {$API -replace $IMPORT, $i} echo $API
我没有收到错误,但它只是打印数组的内容,然后当然会回显一次 $API
而不进行替换。
最佳答案
Mathias has it right关于何时评估变量。另一种允许与您想要的逻辑相同的方法是使用 format operator 。更新您的字符串以包含各种变量(在本例中为 1)的占位符,然后我们可以在循环中替换这些变量。我们使用 {n}
(在本例中为 {0}
)并提供一个大小与占位符数量相等的数组。
$API = @'
curl --insecure 'https://192.168.1.1:3000/hx/api/v2/indicators/Custom/Powershell_AD/conditions/execution' -X 'POST' --data-binary "
{{
\"tests\":[
{{
\"token\":\"processEvent/ActiveDirectory\",
\"type\":\"text\",
\"operator\":\"contains\",
\"preservecase\":false,
\"value\":\"{0}\"
}}
]
}}" -H 'X-FeApi-Token: IAOaiq1s2' -H 'Accept: application/json' -H 'Content-Type: application/json'"
'@
ForEach ($i in Get-Content $IOC){$API -f $i}
不需要正则表达式开销,但是要使这种方法起作用,您需要将字符串中已存在的任何花括号加倍。有点尴尬,因为我忘记了I had that problem in the past 。
关于powershell - 如何在此处字符串中进行变量替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39599523/