简体   繁体   English

PowerShell - 我第一次尝试函数时找不到该函数

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

Trying to include a function in a PowerShell script.试图在 PowerShell 脚本中包含一个函数。 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....

   }

Powershell is a language which is interpreted, it means files are read top to bottom and being interpreted as if we speak. Powershell 是一种解释型语言,这意味着文件从上到下读取并被解释为就像我们在说话一样。

So, if function is called before you have defined it, the Powershell interpreter does not know what you are talking about.因此,如果函数在您定义之前被调用,Powershell 解释器不知道您在说什么。

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.我可以想象您正在使用 Powershell ISE 进行编码。 Let me suggest you to try Visual Studio Code.让我建议您尝试一下 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.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM