0

I have a script that works on other PCs. It only on one PC I have following error message.

Error: Fehler beim Aufrufen der Methode, da [System.Object[]] keine Methode mit dem Namen "op_Addition" enthält. In C:\Users\stream1\Desktop\watchdog.ps1:8 Zeichen:5

  • $wmi += (Get-WMIObject -Class Win32_PerfFormattedData_Tcpip_Netwo ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
    • FullyQualifiedErrorId : MethodNotFound

Thats the script:

#$ErrorActionPreference = 'SilentlyContinue'
$threshold = 6000
$timer = new-timespan -Seconds 10
$wmi = 0
$count = 0
$clock = [diagnostics.stopwatch]::StartNew()
while ($clock.elapsed -lt $timer){
    $wmi += (Get-WMIObject -Class Win32_PerfFormattedData_Tcpip_NetworkInterface | Select-Object BytesSentPerSec).BytesSentPerSec
    $count++
}

$kbytes = ($wmi/$count)  / 1kb * 8 #to kbit/s 


if($kbytes -le $threshold)
{
    Write-Output $kbytes
    Write-Host "Neustart"
    taskkill /f /im obs64.exe
    timeout /t 5
    Start-Process -FilePath "obs64.exe" -WorkingDirectory "C:\Program Files\obs-studio\bin\64bit\" --startstreaming --minimize-to-tray
}
else    
{
    Write-Output $kbytes
    Write-Host "Alles OK"
}

Does somebody has any idea? Thanks so much :-)

I thought it was due to the permission to run scripts in Powershell. But I allowed the execution of scripts (Unrestricted)

  • 2
    `$wmi = @()` (start with an empty array) but in general, you should [try to avoid using the increase assignment operator (`+=`) to create a collection](https://stackoverflow.com/a/60708579/1701026) – iRon Feb 16 '23 at 16:25
  • Thank you for your answer :-) so I should change "$wmi = 0" to "$wmi = @()"? Now I have this message: Fehler beim Aufrufen der Methode, da [System.Object[]] keine Methode mit dem Namen "op_Division" enthält. In C:\Users\stream1\Desktop\watchdog.ps1:12 Zeichen:1 + $kbytes = ($wmi/$count) / 1kb * 8 #to kbit/s + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (op_Division:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound ------------------------ Sorry, I have no idea about programming. Someone wrote me the script. – Thomas Geers Feb 16 '23 at 16:34
  • 2
    ```Get-WMIObject -Class Win32_PerfFormattedData_Tcpip_NetworkInterface``` returns multiple objects if you have multiple network cards so you need to decide if you want to filter it to a specific card, or whether you ant to add up the ```BytesSentPerSec``` for al the cards together. At the moment youre doing ```(gwmi ..).BytesSentPerSec``` which returns an *array* with a value for each network card. If you want to add them you can do ```$wmi += ((Get-WMIObject -Class Win32_PerfFormattedData_Tcpip_NetworkInterface).BytesSentPerSec | measure-object -Sum).Sum```... – mclayton Feb 16 '23 at 16:48
  • Thank you :-) Now I get this: "Fehler beim Aufrufen der Methode, da [System.Object[]] keine Methode mit dem Namen "op_Division" enthält. In C:\Users\stream1\Desktop\watchdog.ps1:12 Zeichen:1 + $kbytes = ($wmi/$count) / 1kb * 8 #to kbit/s + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (op_Division:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound" – Thomas Geers Feb 16 '23 at 17:12

0 Answers0