I knew that powershell allows you compile and execute c# inline, hopefully i could use that feature. It was pretty easy to throw together a test, as it was just c# i was using.
The next problem i encountered; was powershell not loading the client access assemblies. No matter what i tried, i could only get powershell to load them IF the dlls were in the same directory as the powershell executable. Well for me that is not an ideal solution, i like to be able to copy a directory somewhere, and it just works.
What i found was a way you can override the powershell assembly loading
http://stackoverflow.com/questions/2664028/how-can-i-get-powershell-added-types-to-use-added-types
Then it was just a matter of intergrating it with the rest of the script.
Add-Type -Language CSharpVersion3 -TypeDefinition @'
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace Internal.Powershell {
public static class AssemblyResolver {
private static Dictionary<string, string> _assemblies = new Dictionary<string,string>(StringComparer.CurrentCultureIgnoreCase);
static AssemblyResolver() {
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(delegate(object sender, ResolveEventArgs args) {
var assemblyName = new AssemblyName(args.Name);
if (_assemblies.ContainsKey(assemblyName.Name)) {
return Assembly.LoadFrom(_assemblies[assemblyName.Name]);
}
return null;
});
}
public static void AddAssemblyLocation(string path) {
_assemblies.Add(Path.GetFileNameWithoutExtension(path), path);
}
}
}
'@
[Internal.Powershell.AssemblyResolver]::AddAssemblyLocation("..\..\lib\Microsoft.SharePoint.Client.Runtime.dll")
[Internal.Powershell.AssemblyResolver]::AddAssemblyLocation("..\..\lib\Microsoft.SharePoint.Client.dll")
$ReferencedAssemblies = @(
"Microsoft.SharePoint.Client.Runtime",
"Microsoft.SharePoint.Client"
)
$TypeDefinition = @"
using System;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using System.IO;
using System.Text;
using Microsoft.SharePoint.Client;
namespace Internal.Powershell {
public static class SharePointClient {
public static void Main() {
using (ClientContext client = new ClientContext("http://url")) {
client.AuthenticationMode = ClientAuthenticationMode.Default;
client.Credentials = new NetworkCredential("username", "password", "domain");
var web = client.Web;
client.Load(web,
item => item.Title,
item => item.Description);
client.ExecuteQuery();
Console.WriteLine(web.Title);
Console.WriteLine(web.Description);
}
}
}
}
"@
Add-Type -ReferencedAssemblies $ReferencedAssemblies -TypeDefinition $TypeDefinition -Language CSharpVersion3
[Internal.Powershell.SharePointClient]::Main()
2 comments:
Спасибо понравилось !
خدمات دبى - النجم
بناء حمامات سباحة دبى
أعمال نجارة فى دبى
Post a Comment