Code works on copy paste but not on execution

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

This function sure does exist as seen below:

    [string]$User = "", 
   [string]$Password = "",
    [ValidateSet("http","https")][string]$Protocol = "https",
	[string[]]$Graphiteserver = "",
	[ValidateRange(1024,65536)][int]$Graphiteserverport = 2003,
    [string]$Group = "CardinalDefault",
	[ValidateRange(0,65536)][int]$Iterations = 0,
	[string]$FromLastPoll = "",
	[ValidateRange(0,3600)][int]$Sleepseconds = 5, 
    [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) {

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]
				$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
				$socket = new-object
				$socket.connect($cserver, $cport)
				$stream = $socket.getstream()
				$writer = new-object$stream)
				foreach($i in 0..($metrics.count-1)){
				$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
		} while ($IsErr)


With the function call looking like:

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

What am I doing wrong here?

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?

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"
			# Instead of sending the metrics to Graphite, send them to the console or pipe.

It’s just strange to me!

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.

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!