Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自函数结果的Powershell设置变量_Powershell - Fatal编程技术网

来自函数结果的Powershell设置变量

来自函数结果的Powershell设置变量,powershell,Powershell,我不确定我到底做错了什么,但下面的代码返回以下错误消息: 术语“identityTest”不能识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试 以下是我的示例/测试代码: #Bunch of Global vars $UrlToUse = identityTest function identityTest { $internalMatch = ".*inbound" $externalMatch = ".*

我不确定我到底做错了什么,但下面的代码返回以下错误消息:

术语“identityTest”不能识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试

以下是我的示例/测试代码:

#Bunch of Global vars
$UrlToUse = identityTest

function identityTest
{
    $internalMatch = ".*inbound"
    $externalMatch = ".*outbound"
    $fqdn = "blahblah_inbound"

    if ( $fqdn -match $internalMatch )
    {
        return "http://in3"
        Write-Host "Inbound Hit"
    }
    if ( $fqdn -match $externalMatch )
    {
        return "http://out"
        Write-Host "Outbond Hit"
    }
    else
    {
        return "http://default"
        write-host "Default Hit"
    }
}    


function sampleTest
{
    write-host "will upload to the following URL: + $UrlToUse
}

Write-Host $UrlToUse
不确定我在这里是否采取了正确的方法,但这正是我试图实现的目标。我计划将UrlToUse设置为一个全局变量,具体取决于indettyTest函数中if语句的结果,该函数将确定并返回正确的结果。从这里开始,我将在代码的其余部分使用相同的全局变量。我创建的一个示例将在另一个函数中使用相同的var$UrlToUse,在本例中为sampleTest

我不明白这为什么不起作用。我来自Perl背景,可能会对powershell中的工作方式感到困惑。任何提示、指针等都将不胜感激


非常感谢

在调用函数identityTest之前将其移动。像这样:

function identityTest
{
    $internalMatch = ".*inbound"
    $externalMatch = ".*outbound"
    $fqdn = "blahblah_inbound"

    if ( $fqdn -match $internalMatch )
    {
        return "http://in3"
        Write-Host "Inbound Hit"
    }
    if ( $fqdn -match $externalMatch )
    {
        return "http://out"
        Write-Host "Outbond Hit"
    }
    else
    {
        return "http://default"
        write-host "Default Hit"
    }
}    

#Bunch of Global vars
$UrlToUse = identityTest



function sampleTest
{
    write-host "will upload to the following URL: + $UrlToUse"
}

Write-Host $UrlToUse

脚本文件从上到下读取。您引用的每个对象/脚本/函数/等等都必须在会话或脚本中提前初始化。谢谢您的帮助。没有意识到功能顺序很重要。