简体   繁体   English

通过计划任务执行时,PowerShell脚本产生奇怪的结果

[英]Strange result from powershell script when executing via scheduled task

I'm running a powershell script, that when run from the ISE outputs one set of values but when the same task is run through task scheduler it seems to add a second value that doesn't display when run manually. 我正在运行一个powershell脚本,当从ISE运行时会输出一组值,但是当通过任务计划程序运行同一任务时,似乎添加了第二个在手动运行时不显示的值。 The code that's being executed is as below: 正在执行的代码如下:

import-module WebAdministration

$app_pool_name = <<app_pool_name_goes_here>>

$memused = ""
$cpuused = ""

$datetime = get-date -format s
$memused = Get-WmiObject Win32_process  | where CommandLine -Match "$app_pool_name"
$id = dir IIS:\AppPools\$app_pool_name\WorkerProcesses\ | Select-Object -expand processId
$cpuUsed = Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | where IDProcess -Match $id

Add-Content -path C:\Winsys\PSOutput\$app_pool_name-CPU-RAM_test.txt -value "$datetime,$($memUsed.workingsetsize),$($cpuUsed.PercentProcessorTime)"

When running the script manually the output returned is: 手动运行脚本时,返回的输出为:

Date,Mem,CPU 日期,记忆,CPU

2016-08-02T14:09:36,15062687744,0 2016-08-02T14:09:36,15062687744,0

2016-08-02T14:09:38,15062425600,0 2016-08-02T14:09:38,15062425600,0

When running the script through task scheduler the output returned is: 通过任务计划程序运行脚本时,返回的输出为:

Date,Mem,CPU 日期,记忆,CPU

2016-08-02T13:58:25,15065047040 624189440,0 2016-08-02T13:58:25,15065047040 624189440,0

2016-08-02T14:05:01,15061901312 624713728,0 2016-08-02T14:05:01,15061901312 624713728,0

The difference being the Mem, for some reason it's adding an extra value. 区别在于Mem,由于某种原因,它增加了额外的价值。 Does anyone know why this is? 有人知道为什么是这样吗?

Turns out this was my own error, there are two app pools with very similar names, the -match was catching both. 原来这是我自己的错误,有两个名称非常相似的应用程序池,-match捕获了这两个池。 But it still didn't explain why it was only showing both in task scheduler and not ISE. 但是它仍然没有解释为什么它只在任务计划程序中而不在ISE中显示。 Ah well, resolved now by adding a -and -notmatch "text" section. 好的,现在通过添加-和-notmatch“ text”部分来解决。

Eg 例如

Get-WmiObject Win32_process  | where {$_.CommandLine -Match "$app_pool_name" -and $_.CommandLine -notmatch "<<text in other command line>>"}

Add Comment 添加评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM