【问题标题】:Strange result from powershell script when executing via scheduled task通过计划任务执行时来自powershell脚本的奇怪结果
【发布时间】:2016-12-07 22:01:42
【问题描述】:

我正在运行一个 powershell 脚本,当从 ISE 运行时会输出一组值,但当通过任务调度程序运行同一任务时,它似乎添加了第二个值,该值在手动运行时不会显示。正在执行的代码如下:

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)"

手动运行脚本时,返回的输出为:

日期、内存、CPU

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

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

通过任务调度程序运行脚本时,返回的输出是:

日期、内存、CPU

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

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

不同之处在于内存,出于某种原因,它增加了额外的价值。有谁知道这是为什么?

【问题讨论】:

  • 将 $memused 放到另一个文件中,这样您就可以看到它正在管理的两个进程的详细信息。
  • 谢谢,这让我走上了正确的道路

标签: windows powershell scheduled-tasks windows-server-2008 windows-server-2008-r2


【解决方案1】:

原来这是我自己的错误,有两个名称非常相似的应用程序池,-match 都捕获了。但它仍然没有解释为什么它只显示在任务调度程序而不是 ISE 中。嗯,现在通过添加 -and -notmatch "text" 部分解决了。

例如

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

添加评论

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-11
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 2017-06-20
    相关资源
    最近更新 更多