将变量传递给函数失败

时间:2016-10-05 06:16:10

标签: function powershell

我在PowerShell中编写一个函数,通过使用他的API从PHP-IPAM获取IP。

我的功能看起来像;

function Get-IP($IPAM_Space, $IPAM_customer) {
    Write-Host $IPAM_Space
    Write-Host $IPAM_customer

    $Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $Headers.Add("Authorization", 'Basic ZGF2ZS5ncmVlYmU6RGF2MWRzb24=')

    $GetToken = Invoke-RestMethod $IPAM_URL/api/VMDeploy/user/ -Method Post -Headers $Headers

    $token = $GetToken.data.token
    $Headers.Add("token", $token)

    $All_sections = Invoke-RestMethod $IPAM_URL/api/VMDeploy/sections/ -Method Get -Headers $Headers

    $Space_Section_details = $All_sections.data | where Name -eq "$IPAM_Space"
    $Space_id = $Space_Section_details.id
    $Space_Subnets = Invoke-RestMethod $IPAM_URL/api/VMDeploy/sections/$Space_id/subnets/ -Method Get -Headers $Headers

    $Customer_Subnet_details = $Space_Subnets.data | where "description" -eq "$IPAM_customer"
    IF (!($Customer_Subnet_details)) {
        $ErrorMessage = "$IPAM_customer in unknown in PHP-IPAM, fix that first"
        SendErrorMail($ScriptName, $ErrorMessage)
        throw
    }

    $Customer_Subnet_id = $Customer_Subnet_details.id
    $Customer_IP_details = Invoke-RestMethod $IPAM_URL/api/VMDeploy/subnets/$Customer_Subnet_id/addresses/ -Method Get -Headers $Headers

    $FreeAddress = Invoke-RestMethod $IPAM_URL/api/VMDeploy/subnets/$Customer_Subnet_id/first_free/ -Method Get -Headers $Headers
    $netmask = $Customer_Subnet_details.mask
    $gateway = ($Customer_IP_details.data) | where is_gateway -like '1' | Select -ExpandProperty IP
    if (!($gateway)) {
        $ErrorMessage = "$IPAM_customer in $IPAM_Space has no gateway setup in PHP-IPAM, fix that first"
        SendErrorMail($ScriptName, $ErrorMessage)
        throw
    }

    if ($IPAM_customer -ne "IS" -and $IPAM_customer -ne "Cogent" ) {
        $InUse = Get-VMGuest -Vm "*$IPAM_customer*" | where IPAddress -like ($FreeAddress.data)
    }

    if (!($Inuse)) {
        return ($FreeAddress.data), $netmask, $gateway
    } else {
        $Hostname = $InUse.HostName
        $ErrorMessage = "$Address in use by $HostName"
        SendErrorMail($ScriptName, $ErrorMessage)
        throw
    }
}

当我这样称呼时它会加载:

Get-IP "Customers" "CustomerName"

该函数返回正确的信息。

问题在于我在调用函数时使用变量:

Get-IP "Customers" "$CustomerName"

然后在部分挂起:

$Customer_Subnet_details = $Space_Subnets.data | where "description" -eq "$IPAM_customer"
if (!($Customer_Subnet_details)) {
    $ErrorMessage = "$IPAM_customer in unknown in PHP-IPAM, fix that first"
    SendErrorMail($ScriptName, $ErrorMessage)
    throw
}

它告诉我找不到$IPAM_Customer。 奇怪的是,他确实输出了该变量的值。这也可以在write-host $IPAM_customer中找到。

该功能仅在我对客户名称进行硬编码而不是使用变量内容时才起作用。

我还检查$IPAM_Customer的值是什么类型,这是一个字符串,这样也很好。

我不知道为什么会一直失败,所以我希望有人能告诉我这个(或建议我把我的功能写得不同。

1 个答案:

答案 0 :(得分:0)

我终于找到了答案。空格!....

我的函数失败的最后一次调用,$ CustomerName有一些我没有看到并且不知道的空格!...

我发现了;

    write-host "|$IPAM_customer|"

在我的脚本中。 我看到了|客户名称|

我已经手动删除了这些空白,功能很棒!。