0

I have made (my first) script in vscode with powershell extension. It has a couple of functions that checks a printer for some error state over snmp, and then it seeks to correct it via soft-reset.

This works as intended in the terminal inside vscode - but outside, in the normal terminal of powershell or powershell 7 (pwsh) - there is no chance...It claims my functions is not recognized as a name of a cmdlet etc.

Here is most of the code:

'''

    #Kall SNMP lib
        $SNMP = New-Object -ComObject olePrn.OleSNMP
        
        #Definer printere
        $printers =  @('10.0.31.108','10.0.31.182')

        #Sjekk hver printer SNMP for ERROR eller ikke. Soft-reset hvis Error.
        foreach ($ip in $printers){
            

        $state = getPrinterState -ip $ip
    

        if ($state -eq "ERROR"){
        #Error skjedde  -softreset printer
            ResetPrinter -ip $ip

        }
        else{
            #Fortsett loop om ikke error skjedde
            continue
        }

        }

            #Hent printerstatus
            function getPrinterState{

                param($ip)

                #Brother sin egen software sjekker 3 verdier - gjør det samme.
                $SNMP.open($ip,'internal',2,1000)
                $RESULT1 = $SNMP.get('.1.3.6.1.4.1.11.2.4.3.1.2.0')
                $RESULT2 = $SNMP.get('.1.3.6.1.2.1.43.16.5.1.2.1.1')
                $RESULT3 = $SNMP.get('.1.3.6.1.4.1.11.2.3.9.1.1.3.0')
                $SNMP.Close()
            
                #Sjekk om alle verdier er i ERROR 
                if ( ($RESULT1.Trim() -eq 'ERROR') -and ($RESULT2.Trim() -eq 'ERROR') -and ($RESULT3.Trim() -eq "ERROR") ){
                    #retuner verdi til foreach for å resette printer
                    return "ERROR"
                    
                    
                }
                else{
                    #returner verdi OK til foreach så den ikke trenger å reagere
                    return "OK"
                
                }

'''

Here is the pwsh output - run in elevated CMD.

enter image description here

Anyone got some idea what is going on here?

kimhaak
  • 3
  • 3
  • 1
    You need to load the functiion (`getPrinterState`) before you can call it. Apparently you had it already loaded in vscode. – iRon Aug 31 '22 at 16:13
  • I think Visual Studio Code is incidental to your problem, which stems from your attempt to call a function _before_ it is defined. See the linked duplicate for details. – mklement0 Aug 31 '22 at 16:13
  • 1
    To add to @iRon's comment re lingering state in Visual Studio Code: [this answer](https://stackoverflow.com/a/71970578/45375) explains the problem and shows a solution. – mklement0 Aug 31 '22 at 16:15
  • 1
    Thanks @iRon I just moved my foreach loop that calls the functions to the bottom of the script. – kimhaak Sep 01 '22 at 07:45
  • 1
    @mklement0 - thanks - i had problems that i removed code from the script, and saved it. Then, when running script, it ran the old code, and i could not understand why or where this cache was... – kimhaak Sep 01 '22 at 07:45

0 Answers0