[英]Missing url parameters values after running powershell script as scheduled task
我有一个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'"
您可以直接从Task Scheduler运行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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.