Using CSharp (C#) code in Powershell scripts
来源:互联网 发布:淘宝书籍特种经营认证 编辑:程序博客网 时间:2024/05/21 09:54
Using CSharp (C#) code in Powershell scripts
http://blogs.technet.com/b/stefan_gossner/archive/2010/05/07/using-csharp-c-code-in-powershell-scripts.aspx
With Powershell we now got a new very powerful scripting language and many products like SharePoint provide their own extensions in form of CmdLets for administration purposes.
Customer like scripting languages as it allows them to write custom code without a need to run a compiler or to copy new executables to their production machines which usually requires a more complex approval process than deploying a script file or even to execute the commands within a command shell.
But on the other hand writing Powershell scripts requires to learn a new scripting language and to to use different tools than many of us are used to. As a developer I like the power of C# and IntelliSense in Visual Studio. In addition in the last couple of years I wrote many different tools in C# - and I don't want to reinvent the wheel by porting them to Powershell.
So it would be great if the existing C# code could be reused inside Powershell without a need to implement it as Cmdlet.
And indeed the Powershell Version 2 provides a way to achieve this using the Add-Type Cmdlet which allows to generate a new .NET assembly using the provided C# source code in memory which can then be used by powershell scripts executed in the same session.
For demonstration purposes let's assume we have the following simple C# code which allows to retrieve and set the RemoteTimeout value used in Content Deployment of SharePoint:
using System;
namespace StefanG.Tools
{
public staticclass CDRemoteTimeout
{
public staticvoid Get()
{
ContentDeploymentConfiguration cdconfig = ContentDeploymentConfiguration.GetInstance();
Console.WriteLine("Remote Timeout: "+cdconfig.RemoteTimeout);
}
public staticvoid Set(int seconds)
{
ContentDeploymentConfiguration cdconfig = ContentDeploymentConfiguration.GetInstance();
cdconfig.RemoteTimeout = seconds;
cdconfig.Update();
}
}
}
Beside the standard .NET framework references this tool has references to two SharePoint DLLs (Microsoft.SharePoint.dll and Microsoft.SharePoint.Publishing.dll) which provide access to the SharePoint object model. To ensure that Powershell can correctly generate the assembly we need to provide this reference information to the Add-Type Cmdlet using the-ReferencedAssemblies parameter.
To specify the language of the source code (CSharp, CSharpVersion3, Visual Basic and JScript can be used) you need to provide the-Language parameter. CSharp is default.
On my system I have a csharptemplate.ps1 file which I can quickly copy and adjust to run my C# code if required which looks like this:
)
$Source = @"
...add C# source code here...
"@
Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp
For the above listed C# example the final powershell script - would look like this:
"Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" ,
"Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
)
$Source = @"
using Microsoft.SharePoint.Publishing.Administration;
using System;
namespace StefanG.Tools
{
public static class CDRemoteTimeout
{
public static void Get()
{
ContentDeploymentConfiguration cdconfig = ContentDeploymentConfiguration.GetInstance();
Console.WriteLine("Remote Timeout: "+cdconfig.RemoteTimeout);
}
public static void Set(int seconds)
{
ContentDeploymentConfiguration cdconfig = ContentDeploymentConfiguration.GetInstance();
cdconfig.RemoteTimeout = seconds;
cdconfig.Update();
}
}
}
"@
Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp
[StefanG.Tools.CDRemoteTimeout]::Get()
The last lines in the above listed sample demonstrates how to call the C# methods from Powershell.
- Using CSharp (C#) code in Powershell scripts
- Powershell: Launch multiple scripts in parallel using Start-Job
- Using C code in symbian
- 5.Using PowerShell Scripts and 5.Using WMI
- sql: MySQL and Microsoft SQL Server Stored Procedures IN, OUT using csharp code
- sql: MySQL and Microsoft SQL Server Stored Procedures IN, OUT using csharp code
- Packet Sniffer Code in C using sockets
- csharp:Learn how to post JSON string to generic Handler using jQuery in ASP.Net C
- Using local Logon Scripts in Windows XP
- Using shell scripts for CGI in lighttpd
- scripts is disabled on this system error in PowerShell
- csharp:SQLite and Access using C# code read data
- csharp:SQLite and Access using C# code read data
- Demo on bar code printing using SAP Scripts/Smart forms
- Running Windows PowerShell Scripts
- Powershell invokes FTP scripts
- Signing PowerShell Scripts
- PowerShell 脚本(Scripts)
- PHP+Memcache使用入门中文版
- VC++中的DDX和DDV
- java中的继承关系
- 产品经理的主要工作职责
- vc多线程
- Using CSharp (C#) code in Powershell scripts
- STL 容器小结
- 链特异性转录组
- oracle sql developer怎么打不开呢,出现enter the full pathname for java.exe
- 这场大秋雨下了两天
- java dom4j操作xml
- ResultSet
- Android应用程序设置Home Screen
- Proc中使用char数组、VARCHAR数组和char变量进行DELETE操作具体行数的细微区别