更新后PowerShell脚本无法正常工作

时间:2015-03-19 07:47:15

标签: powershell

我将PS 1.0更新为4.0并且我的脚本无效。

它说:

Method invocation failed because [System.Object[]] does not contain a method na
med 'op_Division'.
At C:\Users\sabrnpet\Documents\rsm-monitoring-killer.ps1:18 char:5
+ if ($test/1KB -ge $consumed)
+     ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Division:String) [], Runti 
   meException
    + FullyQualifiedErrorId : MethodNotFound

这是脚本:

### EDIT ME ####
$process = "opera" # BMCRSM
$consumed = 4500000 # in kilobytes
################

# checking if process is running
if (-not (Get-Process $process -ea 0) )
{
    Write-Host "Process $process is not running"
    Exit
}

# variables
$getProcess = Get-Process $process 

# checking if process eating much ram
if ($getProcess.WorkingSet64/1KB -ge $consumed)
{
    Write-Host "I will termiante it..."
    $getProcess.Kill()
}
else
{
    Write-Host "OK"
}

我认为我认为千字节的分区存在问题,但是我需要这个值以千字节为单位。那怎么办呢?

谢谢你

2 个答案:

答案 0 :(得分:1)

查看您的错误消息:

Method invocation failed because [System.Object[]] does not contain a method named 'op_Division'.

[System.Object[]]表示一个数组。 Get-Process发现了一个以上的Opera运行实例。您需要遍历该数组,并逐个处理它们:

# checking if process eating much ram

Foreach ($FoundProcess in $getProcess)
  {
   if ($FoundProcess.WorkingSet64/1KB -ge $consumed)
   {
    Write-Host "I will termiante it..."
    $FoundProcess.Kill()
   }


  else
  {
    Write-Host "OK"
  }
}

答案 1 :(得分:0)

我明白了:

# variables
$getProcess = Get-Process "$process"

字符串变量必须在"引号"