JhennSys
JhennSys

Reputation: 23

PowerShell - My first attempt at a function can't find the function

Trying to include a function in a PowerShell script. I get the message that the function does not exist.

I have the function just below where I am creating the parameters. I assume I'm missing something. I have a number of folders that I want to backup and want to call the function for each folder.

CODE STARTS HERE (code above and pram creation left off for brevity).

    $ImagesSaveTo = "s3://crisma-backup/HERTRICH/Milford/$dow/images"
    #
    # Call Backup
    BackupCrismaByShop -$LogFile $CrismaSource $CrismaSaveTo $ImagesSource

# Begin Backup Function
# ------------------------------------------------------------------------

function BackupCrismaByShop {
       param(
    [string]$LogFile,
    [string]$CrismaSource,
    [string]$CrismaSaveTo,
    [string]$ImagesSource
    )
    
# Backup script....

   }

Upvotes: 0

Views: 161

Answers (1)

Klausen
Klausen

Reputation: 101

Powershell is a language which is interpreted, it means files are read top to bottom and being interpreted as if we speak.

So, if function is called before you have defined it, the Powershell interpreter does not know what you are talking about.

You can try to reorder your code and this should do the trick:

# DEFINE FUNCTION
function BackupCrismaByShop {
       param(
    [string]$LogFile,
    [string]$CrismaSource,
    [string]$CrismaSaveTo,
    [string]$ImagesSource
    )
   # Backup script....

}

# YOUR VARIABLES AND OTHER STUFF

$ImagesSaveTo = "s3://crisma-backup/HERTRICH/Milford/$dow/images"

# CALLING THE FUNCTION
BackupCrismaByShop -$LogFile $CrismaSource $CrismaSaveTo $ImagesSource

I can imagine you are using Powershell ISE to code. Let me suggest you to try Visual Studio Code. It would provide you with some recommendations and warnings as you code such variables you are not using, functions called but not still defined, etc.

Thanks.

Upvotes: 1

Related Questions