将另一种语言转换为 powershell 或在 powershell 中使用该语言 [英] Converting another language to powershell or using the language in powershell

查看:18
本文介绍了将另一种语言转换为 powershell 或在 powershell 中使用该语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这个代码

// "url" is the full destination path (including filename, i.e. https://mysite.sharepoint.com/Documents/Test.txt) 

// "cookie" is the CookieContainer generated from Wichtor's code 
// "data" is the byte array containing the files contents (used a FileStream to load) 

System.Net.ServicePointManager.Expect100Continue = false; 
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest; 
request.Method = "PUT"; 
request.Accept = "*/*"; 
request.ContentType = "multipart/form-data; charset=utf-8"; 
request.CookieContainer = cookie; request.AllowAutoRedirect = false; 
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"; 
request.Headers.Add("Accept-Language", "en-us"); 
request.Headers.Add("Translate", "F"); request.Headers.Add("Cache-Control", "no-cache"); request.ContentLength = data.Length; 

using (Stream req = request.GetRequestStream()) 
{ req.Write(data, 0, data.Length); } 

HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
Stream res = response.GetResponseStream(); 
StreamReader rdr = new StreamReader(res); 
string rawResponse = rdr.ReadToEnd(); 
response.Close();
rdr.Close();

这显然成功地将一个大文件上传到了 sharepoint 站点,但是我的代码在 powershell 中.无论如何现在以它的形式在powershell中使用它,甚至将其转换为powershell?

Which apparently successfully uploads a large file to a sharepoint site, however my code is in powershell. Is there anyway to use this in powershell in its form now or even convert this to powershell?

推荐答案

作为一般规则,C# 代码很容易转换为 PowerShell,因为 C# 使用与 PowerShell (.NET) 完全相同的底层类型系统和运行时环境.

C# code is, as a general rule, easily translated to PowerShell because C# uses the exact same underlying type system and runtime environment as PowerShell (.NET).

需要注意的几点:

C# 是一种类型安全的语言,这意味着编译器保证变量的 type 及其值,如您的示例所示:

C# is a type-safe language, meaning that the compiler guarantees the type of a variable and its value, as seen in your example:

HttpWebRequest request = HttpWebRequest.Create(url); 
//     ^          ^      \________________________/^      
//     |          |                  |             |
//     |    variable name            |    statement terminator ";"
// type-name                         |
//                    static method call that returns a 
//                     value we can assign to "request"

在 PowerShell 中:

In PowerShell:

  1. 类型是隐式的,变量不绑定到单一类型(因此不需要类型名称)
  2. 变量引用以$
  3. 为前缀
  4. 要访问静态成员(如上面的 Create() 方法),我们使用以下语法:
    • [Namespace.TypeName]::Member
  1. Types are implicit, variables are not bound to a single type (thus no need for a type name)
  2. Variable references are prefixed with $
  3. To access static members (like the Create() method above), we use the following syntax:
    • [Namespace.TypeName]::Member

因此,上面的语句变为:

Thus, the above statement becomes:

$request = [System.Net.HttpWebRequest]::Create($url)

<小时>

布尔值

两个 C# 布尔关键字(truefalse)在 PowerShell 中由两个名为 $true 的自动变量表示>$false:


Booleans

The two C# boolean keywords (true and false) are, in PowerShell, represented by two automatic variables called $true and $false:

[System.Net.ServicePointManager]::Expect100Continue = $false

<小时>

使用

PowerShell 没有与 C# 的 using<相当的结构/code> 语句.为了确保处理实现了 IDisposable 的对象,你必须使用 try/catch/finally:


using

PowerShell doesn't have a construct comparable to C#'s using statement. To ensure disposal of an object that implements IDisposable, you'll have to use try/catch/finally:

$req = $request.GetRequestStream()
try{
    $req.Write($data, 0, $data.Length)
} catch {
    throw $_
} finally {
    if($req){
        $req.Dispose()
    }
}

<小时>

构造函数

PowerShell 没有用于对象实例化的 new 关键字,但提供了可以包装 C# 构造函数的 New-Object cmdlet:


Constructors

PowerShell doesn't have a new keyword for object instantiation, but provides the New-Object cmdlet that can wrap C# constructors:

$rdr = New-Object -TypeName System.IO.StreamReader -ArgumentList $res

而不是:

StreamReader rdr = new StreamReader(res); 

在 PowerShell 5.0 和更新版本中,您现在也可以使用 new 静态方法调用构造函数:

In PowerShell 5.0 and newer, you can now invoke constructors using the new static method as well:

$rdr = [System.IO.StreamReader]::new($res)

<小时>

类型转换

PowerShell 支持在 C# 中看起来像 (typename)variable显式转换,但同样使用方括号而不是圆括号:


Type casting

PowerShell supports both explicit casting that in C# would look like (typename)variable, but again, with square brackets instead of parentheses:

[System.Net.HttpWebResponse]$request.GetResponse()

并且(从 3.0 版开始)它也支持unchecked cast(仍然带有方括号):

And (as of version 3.0) it supports unchecked casting as well (still with square brackets):

$request.GetResponse() -as [System.Net.HttpWebResponse]

如果无法进行强制转换,后者将返回 $null,而不是抛出错误.

The latter will return $null, rather than throw an error, if the cast is not possible.

这应该可以让您立即进行翻译.从您的代码查找中的注释来看,您似乎还需要翻译Wichtor 的代码",以便生成 $cookie 容器.

This should get you translating in no time. From the comments in your codefind, it seems you might need to translate "Wichtor's code" as well, in order to generate the $cookie container.

这篇关于将另一种语言转换为 powershell 或在 powershell 中使用该语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆