2015-03-19 57 views
0

我更新PS 1.0到4.0,我的腳本不工作。更新後PowerShell腳本不工作

它說:

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

我已瞭解,有與千字節我想divison問題,但我需要以KB爲單位此值。那麼請怎麼做?

的Thanko你

回答

0

我懂了:

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

字符串變量必須在 「引號」

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