使用PowerShell

时间:2016-08-22 01:59:08

标签: powershell vsix

使用私人图库(使用Inmeta Visual Studio Gallery Service),您是否可以使用PowerShell自动上传新的/更新的Visual Studio扩展程序?

这可以作为构建管道中的构建步骤添加。

1 个答案:

答案 0 :(得分:0)

在PowerShell中嵌入C#,这有效(基于http://www.codeproject.com/Articles/8600/UploadFileEx-C-s-WebClient-UploadFile-with-more-fu):

$source = @"
using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;


public static class WebClientEx
{
    public static string UploadFileEx(string uploadfile, string url)
    {
        const string fileFormName = "File";
        const string contenttype = "application/vsix";

        var postdata = "?";
        Uri uri = new Uri(url + postdata);


        string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
        HttpWebRequest webrequest = (HttpWebRequest) WebRequest.Create(uri);
        webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
        webrequest.Method = "POST";
        webrequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        webrequest.ServicePoint.Expect100Continue = false;
        webrequest.AllowAutoRedirect = false;

        // Build up the post message header
        StringBuilder sb = new StringBuilder();
        sb.Append("--");
        sb.Append(boundary);
        sb.Append("\r\n");
        sb.Append("Content-Disposition: form-data; name=\"");
        sb.Append(fileFormName);
        sb.Append("\"; filename=\"");
        sb.Append(Path.GetFileName(uploadfile));
        sb.Append("\"");
        sb.Append("\r\n");
        sb.Append("Content-Type: ");
        sb.Append(contenttype);
        sb.Append("\r\n");
        sb.Append("\r\n");

        string postHeader = sb.ToString();
        byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);

        // Build the trailing boundary string as a byte array
        // ensuring the boundary appears on a line by itself
        byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

        Stream requestStream;
        using (FileStream fileStream = new FileStream(uploadfile, FileMode.Open, FileAccess.Read))
        {
            long length = postHeaderBytes.Length + fileStream.Length +
                          boundaryBytes.Length;
            webrequest.ContentLength = length;

            requestStream = webrequest.GetRequestStream();
            requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

            // Write out the file contents
            byte[] buffer = new byte[checked((uint) Math.Min(4096, (int) fileStream.Length))];
            int bytesRead;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                requestStream.Write(buffer, 0, bytesRead);
        }

        // Write out the trailing boundary
        requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

        using (HttpWebResponse response = (HttpWebResponse) webrequest.GetResponse())
        {
            return string.Format("{0} {1} ({2}", (int) response.StatusCode, response.StatusDescription, response.Headers[HttpResponseHeader.Location]);
        }
    }
}
"@

Add-Type -TypeDefinition $source -Language CSharpVersion3

[WebClientEx]::UploadFileEx("D:\dev\sandbox\VSIXProject2\VSIXProject2\bin\Debug\VSIXProject2.vsix", "http://mywebserver/InmetaGallery/Upload/Post")