我有一个Powershell脚本,它将计算机的名称作为字符串,并将其上运行的进程作为json数组,从变量创建参数,然后向URL发出Web请求,并通过POST方法发送2个参数如下图所示:
$comp_id = (Get-WmiObject win32_operatingsystem) | Select-Object csname | Format-Table -HideTableHeaders | out-string
$processes = get-process | Where-Object {$_.StartTime -ne $null -and $_.MainWindowTitle.Length -gt 10} | Select-Object id,description,FileVersion,mainWindowTitle,startTime| ConvertTo-Json
# create parameters from variables
$param1= @{id=$comp_id}
$param2 = @{data=$processes} #the variable $process is a JSON array string
$bothParams = $param1+$param2
Invoke-WebRequest -Uri http://my.webserver.comp:8080/galileo/peeker -Method POST-Body $bothParams
直接运行脚本时一切正常,即两个参数值都可以从servlet访问。
我想将此脚本作为计划任务运行,因此我创建了一个调用powershell脚本的批处理脚本文件。问题是当脚本作为计划任务运行时,第二个参数(应该是JSON数组字符串)为空。可能是什么问题?
我的批处理脚本有以下两行:
@echo OFF
# used batch script tag(%~dpn0) to get path (p) and file name similar to this file
Powershell.exe -Command "& '%~dpn0.ps1'"
答案 0 :(得分:0)
您可以直接从任务计划程序运行PowerShell脚本,因此您不需要批处理脚本。至于为什么JSON字符串最终为空,最可能的原因是Where-Object
没有捕获任何内容。
在脚本中添加一些基本的调试日志记录可以帮助您了解正在进行的操作。例如:
$logFile = "script.log"
$env:USERNAME | Out-File -FilePath $logFile -Append
Get-Process | Select-Object Id, Description, FileVersion, MainWindowTitle, StartTime `
| Out-File -FilePath $logFile -Append