未加星标

Making PowerShell Based Azure Functions to Produce HTML Outputs

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Over the last few weeks, I’ve been working with my MVP buddy Alex Verkinderen ( @AlexVerkinderen ) on some Azure Function related stuff. We have both written few PowerShell based functions that output a HTML page.

These functions use the ConvertTo-HTML cmdlet to produce the HTML output. For example, here’s a simple one that list 2 cars in a HTML table:

$Cars = @() #Car #1 $CarProperties = @{ 'Make' = 'BMW' 'Colour' = 'Black' 'RegistrationNumber' = 'ABC123' } $Cars += New-Object psobject -Property $CarProperties #Car #2 $CarProperties = @{ 'Make' = 'Toyota' 'Colour' = 'Red' 'RegistrationNumber' = 'DEF456' } $Cars += New-Object psobject -Property $CarProperties $HTMLOutput = ($Cars | ConvertTo-Html -Title 'Car List') | Out-String Out-file -encodingAscii -FilePath $res -InputObject $HTMLOutput

Today we ran into an issue while preparing for our next blog posts, after some diagnostics, we realised the issue was caused by the HTML output returned from the PowerShell based functions.

If I use Invoke-WebRequest cmdlet in Powershell to trigger this PowerShell function, I am able to get the HTML output in the request output content and everything looks good:


Making PowerShell Based Azure Functions to Produce HTML Outputs

However, if we simply invoke this function from a browser, although the output is in HTML format, the browser does not display the HTML page. it displays the HTML source code instead:


Making PowerShell Based Azure Functions to Produce HTML Outputs

after some research, we found the cause of this issue the content type returned by the PowerShell function is always set to “text/plain”:


Making PowerShell Based Azure Functions to Produce HTML Outputs

I suspect this is because for PowerShell based functions, we have to output to a file ($res variable by default). I have tried to construct a proper HTTP response message (System.Net.Http.HttpResponseMessage), but it didn’t work in the PowerShell functions. Based on my testing results, it seems PowerShell functions cannot handle complex types.

Luckily I found this post and it pointed me to the right direction: http://anthonychu.ca/post/azure-functions-serve-html/ . According on this post, we can certainly serve out a proper HTML page in C# based functions.

I don’t really want to rewrite all my PowerShell functions to C#, not only because I don’t want to reinvent the wheels, but also I want to keep using the PowerShell modules in those existing functions. In the end, I came up with C# based “wrapper” function. I named this function HTTPTriggerProxy :

This C# based HTTPTriggerProxy function simply takes the URL you have specified, get the response and wrap it in a proper HTTPResponseMessage object. All you need to do is to specify the original URL that you want to request in the “RequestURL” parameter as part of the wrapper function URL:

https://<Your Azure Function Account>.azurewebsites.net/api/HttpTriggerProxy?code=<Access code for Http Trigger Proxy function> &RequestURL=<Your original request URL> .

Now if I use this wrapper to invoke the sample GetCars PowerShell function, the HTML page is displayed in the browser as expected:


Making PowerShell Based Azure Functions to Produce HTML Outputs

and you can see the content type is now set as “text/html”:


Making PowerShell Based Azure Functions to Produce HTML Outputs
Note: This wrapper function only supports the Get HTTP method. The Post method is not supported so you can only pass the RequestURL in the the wrapper URL (as opposed to placing it in the request body). I didn’t bother to cater the POST method in this function because what we are going to use this for only supports HTTP Get method. if your original request requires authentication, then this is not going to work for you. If you original URL contains the ampersand character (“ & ”), please replace it with “ %26 ”. for example, if your original request is https://myazurefunction.azurewebsites.net/api/GetCars?code=rgpxmm0p87fh2z1wd0a6vargfxxogb6cf & colour=red , then you need to change it to https://myazurefunction.azurewebsites.net/api/GetCars?code=rgpxmm0p87fh2z1wd0a6vargfxxogb6cf %26 colour=red

Lastly, this is just something we came up today while making another set of posts. Please stay turned. our new posts will be published in the next day or two.

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

主题: HTMLPowerShellC#.NetBMW
分页:12
转载请注明
本文标题:Making PowerShell Based Azure Functions to Produce HTML Outputs
本站链接:http://www.codesec.net/view/482373.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(19)