2016-10-05 199 views
-1

我正在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中看到。

該函數僅在我使用Customer-name而不是使用變量內容進行硬編碼時才起作用。

我也檢查了什麼類型的值是$IPAM_Customer,這是一個字符串,所以也是好的。

我不知道爲什麼這樣下去失敗,所以我希望有人能告訴我這個(或勸我寫我的功能有所不同。

+0

請嘗試像這樣調用您的函數:'Get-IP -IPAM_Space「Customer」-IPAM_customer $ CustomerName' –

+0

難道是'$ CustomerName'包含空白嗎?你如何首先獲得'$ CustomerName'? –

+0

@MartinBrandl這也沒有工作 $客戶不包含空格。它是一個單詞。我使用Excel獲取我的$ CustomerName。 –

回答

0

我終於找到了答案。空格!....

最後一個電話在我的功能失效,$客戶名稱已配發白色的空間,我沒有看到,不知道的..

我發現這樣做;!

write-host "|$IPAM_customer|" 

在我的腳本。 我看到| CustomerName |

我已經刪除了空格手冊,並且該函數工作的很棒!