将另一种语言转换为powershell或在powershell中使用该语言
本文关键字:语言 powershell 或在 转换 另一种 | 更新日期: 2023-09-27 18:04:33
我找到了这段代码
// "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?
一般来说,c#代码很容易转换为PowerShell,因为c#使用与PowerShell (. net)完全相同的底层类型系统和运行时环境。
注意事项:
变量赋值
c#是一种类型安全的语言,这意味着编译器保证变量的类型及其值,如您的示例所示:HttpWebRequest request = HttpWebRequest.Create(url);
// ^ ^ '________________________/^
// | | | |
// | variable name | statement terminator ";"
// type-name |
// static method call that returns a
// value we can assign to "request"
在PowerShell: - 类型是隐式的,变量不绑定到单个类型(因此不需要类型名)
- 变量引用以
$
为前缀要访问静态成员(如上面的 -
[Namespace.TypeName]::Member
-
;
是不必要的,语句后的换行意味着终止
Create()
方法),我们使用以下语法:$request = [System.Net.HttpWebRequest]::Create($url)
布尔值
两个c#布尔关键字(true
和false
)在PowerShell中由两个自动变量$true
和$false
表示:
[System.Net.ServicePointManager]::Expect100Continue = $false
using
PowerShell没有类似于c#的using
语句的结构。要确保处理实现IDisposable
的对象,必须使用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:
$rdr = New-Object -TypeName System.IO.StreamReader -ArgumentList $res
而不是:
StreamReader rdr = new StreamReader(res);
在PowerShell 5.0及更新版本中,您现在也可以使用new
静态方法调用构造函数:
$rdr = [System.IO.StreamReader]::new($res)
类型铸造
PowerShell支持显式强制转换,在c#中看起来像(typename)variable
,但是再次使用方括号代替圆括号:
[System.Net.HttpWebResponse]$request.GetResponse()
并且(从3.0版本开始)它还支持未检查的强制转换(仍然使用方括号):
$request.GetResponse() -as [System.Net.HttpWebResponse]
如果不能强制转换,后者将返回$null
,而不是抛出错误。
这应该会让你在任何时间翻译。从你的代码查找注释,似乎你可能需要翻译"维克多的代码",以及,为了生成$cookie
容器。
见下面的例子。我必须把代码包装在类和函数中。
$code = @"
using System.Security.Policy;
using System.IO;
using System.Net;
public class MySharepointTools
{
public void UploadFile(System.String url, System.Net.CookieContainer cookie, System.Byte[] data)
{
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();
}
}
"@
Add-Type -TypeDefinition $code -Language CSharp
$tools = New-Object MySharepointTools
现在你可以使用
来调用这个方法$Tools.UploadFile(url, cookie, data)