作为计划任务运行powershell脚本后缺少url参数值

作为计划任务运行powershell脚本后缺少url参数值,powershell,batch-file,Powershell,Batch File,我有一个Powershell脚本,它以字符串形式获取计算机名称,以json数组形式在计算机上运行进程,从变量创建参数,然后向url发出web请求,并通过POST方法发送2个参数,如下所示: $comp_id = (Get-WmiObject win32_operatingsystem) | Select-Object csname | Format-Table -HideTableHeaders | out-string $processes = get-process | Where-Obj

我有一个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'" 

您可以直接从任务计划程序运行PowerShell脚本,因此不需要批处理脚本。至于为什么JSON字符串最终为空,最可能的原因是Where对象没有捕获任何内容

在脚本中添加一些基本的调试日志可以帮助您了解发生了什么。例如:

$logFile = "script.log"

$env:USERNAME | Out-File -FilePath $logFile -Append
Get-Process | Select-Object Id, Description, FileVersion, MainWindowTitle, StartTime `
    | Out-File -FilePath $logFile -Append

我想这是一个路径问题;分享你的批处理脚本;您如何分配$comp_id和$processs?您好。我编辑了这个问题,以包括从批处理文件发出的调用以及$comp_id和$process如何获得它们的值。希望现在更清楚..任务计划程序使用哪个帐户运行脚本?我使用普通用户帐户运行脚本,我已将其设置为使用最高权限运行,并且所有脚本的Powershell都设置为“无限制”模式。嗨,Allexander,感谢您的日志提示。当我直接运行get进程行时,它工作正常,但在作为计划任务运行时出现问题。我将尝试按照您的建议将输出记录到文件中,并查看得到的结果。