Code works on copy paste but not on execution

This topic contains 4 replies, has 2 voices, and was last updated by  Aaron 1 month, 4 weeks ago.

  • Author
    Posts
  • #96977

    Aaron
    Participant

    I have some code that when I copy and paste into the console it works without issue.

    But if I right click the script and run with powershell it gives an error:

    send-tographite : The term 'send-tographite' is not recognized as the name of a cmdlet, function, script file, or
    operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
    again.
    

    This function sure does exist as seen below:

    [CmdletBinding()]
    param(
        [string]$User = "", 
       [string]$Password = "",
        [ValidateSet("http","https")][string]$Protocol = "https",
    	[string[]]$Graphiteserver = "10.10.10.10",
    	[ValidateRange(1024,65536)][int]$Graphiteserverport = 2003,
        [string]$Group = "CardinalDefault",
    	[ValidateRange(0,65536)][int]$Iterations = 0,
    	[string]$FromLastPoll = "",
    	[ValidateRange(0,3600)][int]$Sleepseconds = 5, 
        [Switch]$Whatif,
        [ValidateSet("None","Information","Warning","Error")][String]$EventLogLevel = "Warning"
    )
    
    
    add-type @"
        using System.Net;
        using System.Security.Cryptography.X509Certificates;
        public class TrustAllCertsPolicy : ICertificatePolicy {
            public bool CheckValidationResult(
                ServicePoint srvPoint, X509Certificate certificate,
                WebRequest request, int certificateProblem) {
                return true;
            }
        }
    "@
    [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
    
    
    
    # ---------------------------------------
    # Write Events to the Windows Event Log
    # ---------------------------------------
    function Write-To-Windows-EventLog ($severity, $id, $message) {
    $levels="None","Information","Warning","Error"
    $ell=[array]::IndexOf($levels,$EventLogLevel)
    $sev=[array]::IndexOf($levels,$severity)
    $logsouce=""
    
    if (($ell -ne 0) -AND ($sev -ge $ell)) {
        $myscriptname = split-path $MyInvocation.PSCommandPath -Leaf
        $myscriptfull = $MyInvocation.ScriptName
        $l = [Environment]::NewLine
    
        New-EventLog –LogName Application –Source $myscriptname -ErrorAction SilentlyContinue
    
        $msg = $message + $l+$l + "Called by:" + $l + "$myscriptfull -Server $server -User $user -Password [**HIDDEN**] -Protocol $protocol -Datacenter $datacenter -Cluster $cluster -Group $Group -Graphiteserver $Graphiteserver -Graphiteserverport $Graphiteserverport -Sleepseconds $Sleepseconds -Iterations $Iterations -FromLastPoll $FromLastPoll -WhatIf:`$$Whatif -EventLogLevel $EventLogLevel"
        Write-EventLog –LogName Application –Source $myscriptname –EntryType $severity –EventID $id –Message $msg -Category 0
    
    }
    
    
    }
    
    # ---------------------------------------
    # Send and array with metrics to Graphite
    # ---------------------------------------
    function send-tographite ($metrics)
    {
    	$maxretries = 10	# Maximum number of retries to connect to a Graphite server. Only applicable if multiple servers are specified. With only 1 server it will try forever.
    	$aservers = $graphiteserver -split ' '
    
    	foreach($s in 0..($aservers.count-1)){
    
    		$cserver = $aservers[$s]
    		if ($cserver.contains(":")) 
    			{
    				$atmp = $cserver -split ':'
    				$cserver = $atmp[0]
    				$cport = $atmp[1]
    			}
    			else
    			{
    				$cport = $graphiteserverport
    			}
    		
    		$msg = "Sending $scount metrics for $vcount VMs of iteration # $iteration to Graphite server $cserver`:$cport"
    		Write-Verbose "$(Get-Date -format G) $msg"
    		Write-To-Windows-EventLog "Information" 1005 $msg
    		$trycount = 1
    		do {
    			$iserr = $false
    			Try
    			{
    				$socket = new-object system.net.sockets.tcpclient
    				
    				$socket.connect($cserver, $cport)
    				
    				$stream = $socket.getstream()
    				$writer = new-object system.io.streamwriter($stream)
    				
    				foreach($i in 0..($metrics.count-1)){
    				$writer.writeline($metrics[$i])
    				}
    				
    				$writer.flush()
    				$writer.close()
    				$stream.close()
    				$socket.close()
    			}
    			Catch
    			{
    				$IsErr = $true
    				$ErrorMessage = $_.Exception.Message
    				$FailedItem = $_.Exception.ItemName
    				$msg = "Connection to $cserver`:$cport failed with $ErrorMessage! Will retry in 10 seconds"
    				Write-Warning "$(Get-Date -format G) $msg"
    				Write-To-Windows-EventLog "Warning" 2007 $msg
    				Start-Sleep -s 10
    				
    				if (($trycount -ge $maxretries) -and ($aservers.count -gt 1)) {
    					# Giving up this Server, it does not work, lets try the other one(s)
    					$msg = "Giving up connection to $cserver`:$cport after $trycount failed attempts"
    					Write-Error "$(Get-Date -format G) $msg"
    					Write-To-Windows-EventLog "Error" 2007 $msg
    					$IsErr = $false
    				}
    				
    				$trycount++
    				
    			}
    		
    		} while ($IsErr)
    	}
    
    }
    

    With the function call looking like:

    if (!$Whatif){
    			# Sends the metrics to Graphite.
    			send-tographite $results
    			write-host "Cardinal"
    			}
    		else{
    			# Instead of sending the metrics to Graphite, send them to the console or pipe.
    			$results.Values
    			}
    
    

    What am I doing wrong here?

  • #96980

    Don Jones
    Keymaster

    So, that's a lot of code to just cold-read and troubleshoot :).

    Functions have to be defined before they can be called. Have you ensured that's the case?

    • #97142

      Aaron
      Participant

      Sure, sorry about that...I just wanted to show that the [cmdletbinding()] was the top line and the functions were below it.

      The send-tographite function is started on line 59 and the call to it is on line 247

      if (!$Whatif){
      			# Sends the metrics to Graphite.
      			send-tographite $results
      			write-host "Cardinal"
      			}
      		else{
      			# Instead of sending the metrics to Graphite, send them to the console or pipe.
      			$results.Values
      			}
      	}
      

      It's just strange to me!

  • #97146

    Don Jones
    Keymaster

    Indeed odd. It's a little tough to troubleshoot these via a message, because so much can depend on environment configuration and stuff that you wouldn't notice in a post.

    First, I'd try saving the file as a .ps1 and seeing if you could run the .ps1 as-is from the console. Right-Click-run-with isn't a well-supported thing, and it'd help to know if launching it as a script from within the console helped. Beyond that, you start getting into some fairly gnarly troubleshooting.

  • #97151

    Aaron
    Participant

    Interesting, so I just changed the name of the function and now it's working without an issue...how odd...I copy and pasted the name of the function on the call and it got the same error.

    Just named it 'test-function' and it works...so I dunno, but thanks!

You must be logged in to reply to this topic.