Avatar billede homemicky Nybegynder
25. marts 2014 - 14:21

Powershell - har ikke få det her script til at virke i powershell ise?

Hej eksperter ... Har fundet et script, som jeg lige ville teste på min egen PC med mcshield tjenesten. Scriptet ser sådan ud:
################################################################################
#                                                                              #
# TITLE:    Get a process working-set Memory Usage                            #
# Author:...... Marc Butler                                                    #
# Date Created: Tuesday 03/06/2013                                            #
# Last Modified:                                                              #
# Modified By:                                                                #
#                  ******************************                            #
# PURPOSE:  PowerShell Monitor that retrieves the working set memory usage of  #
#          of a process and determines if it is high or low than a given      #
#          Threshold                                                          #
#                                                                              #
#                                                                              #
#                  ******************************                            #
# RETURN:      $Context.SetResult = 0  Below Threshold Value                  #
#          $Context.SetResult = 0  Below Threshold Value                  #
#                                                                              #
# Future:      Items in the progress                                          #
#                  ******************************                            #
# HOW TO USE: 1. Use the following power shell command to get the name of the  # 
#                service you want to monitor:                      #           
#                [Get-WmiObject -ComputerName 'servername' -Class              #                       
#                                Win32_PerfFormattedData_PerfProc_Process]    #
#                                                                              #
#            2. Replace the variable values # marked with the arrows          #
#                                                                              #
#                                                                              #
# HOW TO TEST:                                                                #
#              1. Create an PowerShell Active Monitor in WhatsUpGold: Just cut #
#                and paste the contents below,                                #
#                a. Modify the Variables                                      #
#                                                                              #
#                                                                              #
#                                                                              #
#        *** Note password will not be hidden ***                            #
# ##############################################################################
#PowerShell Active Monitor Script

$Context.SetResult(0,'Memory Usage Below Threshold Value');

# Use available context variables as needed
$resultText =  $Context.GetProperty("Address");

#Use available context variables to get the server's ip address
$ip =  $Context.GetProperty("Address");

#These lines convert the server's ip address to its FQDN
$DnsEntry = [System.Net.Dns]::gethostentry($ip) ;
$DnsName = [string]$DnsEntry.HostName ;


#Name of ther service that you want restarted <<***********************###Change Here
$ServiceName = "Mcshield";

#Threshold Value in bytes <<***********************###Change Here
$Threshold =  3000000000
#$Threshold =  3


# Get the Windows credentials for the device
$WinUser = $Context.GetProperty("CredWindows:DomainAndUserid");
#$WinUser | Out-File c:\WUGlOGS\WinUseroutput.txt


# Get the Windows credentials for the device
$WinPass = $Context.GetProperty("CredWindows:Password");
#$Winpass | Out-File c:\WUGlOGS\WinPassoutput.txt


$pwd = convertto-securestring $WinPass -asplaintext -force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $WinUser,$pwd


#Actions that get the service
$Process = Get-WmiObject -ComputerName $DnsName -Class Win32_PerfFormattedData_PerfProc_Process -Filter "Name='$ServiceName'"  -Credential $cred

if($Process.workingset -gt $Threshold){$Context.SetResult(1,$Process.workingset)}

Jeg er totalt på farevand her, men kunne godt sciptet i vores ipswitch whatsup gold, hvis det virkede!! Men kender ikke så meget til programming andet end jeg godt kan regne ud at jeg skal rette to steder i scriptet.

Jeg får følgende fejlmeddelse i Powershell ISE:
PS C:\Users\MB\Desktop> C:\Users\MB\Desktop\GetWorkSetMemoryWindowsServiceny.ps1
You cannot call a method on a null-valued expression.
At C:\Users\MB\Desktop\GetWorkSetMemoryWindowsServiceny.ps1:39 char:1
+ $Context.SetResult(0,'Memory Usage Below Threshold Value');
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Users\MB\Desktop\GetWorkSetMemoryWindowsServiceny.ps1:42 char:1
+ $resultText =  $Context.GetProperty("Address");
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Users\MB\Desktop\GetWorkSetMemoryWindowsServiceny.ps1:45 char:1
+ $ip =  $Context.GetProperty("Address");
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Exception calling "GetHostEntry" with "1" argument(s): "En værdi må ikke være null.
Parameternavn: address"
At C:\Users\MB\Desktop\GetWorkSetMemoryWindowsServiceny.ps1:48 char:1
+ $DnsEntry = [System.Net.Dns]::gethostentry($ip) ;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

You cannot call a method on a null-valued expression.
At C:\Users\MB\Desktop\GetWorkSetMemoryWindowsServiceny.ps1:61 char:1
+ $WinUser = $Context.GetProperty("CredWindows:DomainAndUserid");
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Users\MB\Desktop\GetWorkSetMemoryWindowsServiceny.ps1:66 char:1
+ $WinPass = $Context.GetProperty("CredWindows:Password");
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is null.
At C:\Users\MB\Desktop\GetWorkSetMemoryWindowsServiceny.ps1:70 char:31
+ $pwd = convertto-securestring $WinPass -asplaintext -force
+                              ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [ConvertTo-SecureString], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand

new-object : Cannot find an overload for "PSCredential" and the argument count: "2".
At C:\Users\MB\Desktop\GetWorkSetMemoryWindowsServiceny.ps1:71 char:9
+ $cred = new-object -typename System.Management.Automation.PSCredential -argument ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\Users\MB\Desktop\GetWorkSetMemoryWindowsServiceny.ps1:75 char:40
+ $Process = Get-WmiObject -ComputerName $DnsName -Class Win32_PerfFormattedData_P ...
+                                        ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Og så er jeg total blank. Jeg står jo selv man bare kan copy paste og ændre to steder... hmmm håber der nogen der kan hjælpe?
Så vil jeg blive meget glad. Har søgt søgt efter en løsning som denne i lang tid vores whatsup gold :-)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester