Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

Code works, until I put it in a function

$
0
0

Hi,

I have some code which make an REST API call to Airwatch:

 Function Get-DeviceswithPublicApp ($locationID, $appID) { $APIURL = "api/mam/apps/public/$appID/devices?status=installed&locationgroupid=$locationid" $URI = $serverAW+$APIURL Try { $reply = $null $headers = FormatHTTPHeader $URI $reply = Invoke-WebRequest -uri $URI -DisableKeepAlive -Method Get -Headers $headers -ContentType "application/json" -Body $js #Write-Host -ForegroundColor magenta " Status : " $reply.StatusDescription } Catch { $reply = $null $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Write-Host " AW connection failed : $FailedItem. The error message was $ErrorMessage" } $closeConnection = $ServicePoint.CloseConnectionGroup("") return $reply } 

When I run it:

Get-DeviceswithPublicApp ($locationid, $Appid)

Or

Get-DeviceswithPublicApp (1045, 2116)

It returns:

“AW connection failed : . The error message was The remote server returned an error: (404) Not Found.”

If I just pull the code out and run it (after setting the variables), it works.

 $locationid = 1045 $Appid = 2116 $APIURL = "api/mam/apps/public/$appID/devices?status=installed&locationgroupid=$locationid" $URI = $serverAW+$APIURL Try { $reply = $null $headers = FormatHTTPHeader $URI $reply = Invoke-WebRequest -uri $URI -DisableKeepAlive -Method Get -Headers $headers -ContentType "application/json" -Body $js #Write-Host -ForegroundColor magenta " Status : " $reply.StatusDescription } Catch { $reply = $null $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Write-Host " AW connection failed : $FailedItem. The error message was $ErrorMessage" } $closeConnection = $ServicePoint.CloseConnectionGroup("") return $reply 

What am I missing?

submitted by /u/quarky_uk
[link] [comments]

The post Code works, until I put it in a function appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Trending Articles