Powershell From C#?
Aug 5, 2010
I am trying to write an asp program to automate some tasks in the office - namely creating a mail-enabled contact. The code is:
RunspaceConfiguration runspaceConfig = RunspaceConfiguration.Create();PSSnapInException PSException = null;PSSnapInInfo info = RunspaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.Powershell.Admin", out PSException);Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfig);runspace.Open();Pipeline pipeline = runspace.CreatePipeline();Command command = new Command("New-MailContact");command.Parameters.Add("Name", "TestName");command.Parameters.Add("ExternalEmailAddress", "testname@testdomain.com");command.Parameters.Add("OrganizationalUnit",
[code]....
View 2 Replies
Mar 31, 2010
It seems like ASP.NET WebMethods are not "web servicey" enough to work with New-WebServiceProxy. Or maybe it is, and I haven't figured out how to initialize it?
So instead, I tried doing it manually, like so:
$wc = new-object System.Net.WebClient
$wc.Credentials = [System.Net.CredentialCache]::DefaultCredentials
$url = "http://www.domenicdenicola.com/AboutMe/SleepLog/default.aspx/GetSpans"
$postData = "{`"starting`":`"/Date(1254121200000)/`",`"ending`":`"/Date(1270018800000)/`"}"
$result = $wc.UploadString($url, $postData)
But this gives me "The remote server returned an error: (500) Internal Server Error." So I must be doing something slightly wrong.
how to call my PageMethod from PowerShell, and not get an error?
View 2 Replies
Dec 9, 2010
A customer wants us to create an ajax-enabled web application that can be used to manage their IT environment (Active directory, mail, file security, ...). However, as the team that will support the web application does not know anything about these IT systems, they would like to split the coding so that all interfacing with the IT environment is done via PowerShell, which is known by the administrators of these systems. This would mean that if new functionality is required, the web team can just create an additional web page and interface and use PowerShell scripts that get created by technically skilled people in that specific topic (ie mail administrators). I don't see an issue using this approach for requests like adding a user where latency is not an issue, but I do wonder if this is feasible for interactive content with Ajax.
Ie I would like to implement a google suggest like search for AD users. This would mean that ie for every x keystrokes (or x seconds whatever approach I implement) an Ajax request will be made and the web application or web service will than use PowerShell to get the data. Taken into account that up to about 75 users can use this feature, is this something that would work? Not only performace-wise but also the number of connections (I read about the Powershell 5 remote session limit but also that it could be put to a higher number)? The example above is probably the most resource heavy Ajax requests (and can be optimized by caching), so taken into account they don't want this nice feature, are basic PowerShell scripts in ajax request really an option. My gut feeling is telling me this is a no-go, but I don't want to exclude the option just because it's not something I usually would do.
View 1 Replies
Apr 16, 2010
I have developed an ASP.NET MVC Web Application to execute PowerShell scripts.I am using the VS web server and can execute scripts fine. However, a requirement is that users are able to execute scripts against AD to perform actions that their own user accounts are not allowed to do.Therefore I am using impersonation to switch the identity before creating the PowerShell runspace:
Runspace runspace = RunspaceFactory.CreateRunspace(config);
var currentuser = WindowsIdentity.GetCurrent().Name;
if (runspace.RunspaceStateInfo.State == RunspaceState.BeforeOpen) {
runspace.Open();
}
I have tested using a domain admin account and I get the following exception when calling runspace.Open():
Security Exception
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.
Exception Details: System.Security.SecurityException: Requested registry access is not allowed.
The web application is running in full trust and I have explicitly added the account I am using for impersonation to the local administrators group of the machine (even though the domain admins group was already there).I'm using advapi32.dll LogonUser call to perform the impersonation in a similar way to this post (http://blogs.msdn.com/webdav_101/archive/2008/09/25/howto-calling-exchange-powershell-from-an-impersonated-thead.aspx)
View 1 Replies
Aug 22, 2010
I need CLI for our asp.net mvc application to perform maintenance tasks and some status tasks.
I think powershell is designed to provide CLI. But i do not know anything about it other than name.
How can i host powershell in asp.net mvc running in iis to provide CLI for custom tasks?
View 2 Replies
Jun 29, 2010
I am not able to use the below namespaces in order to Run a powershell script:
using
System.Management.Automation;
using
System.Management.Automation.Runspaces;
I have installed Windows Powershell 2 and the framework is 2.0. I have added the referenece System.Management in the web.config file. Also i have browsed the above 2 namespaces and added to the project in add reference tab. Still i get the below message:
CS0234: The type or namespace name 'Automation' does not exist in the namespace 'System.Management' (are you missing an assembly reference?)
View 6 Replies
Nov 26, 2010
I want to execute some powershell commands on a remote windows 2008 machine. Right now i am using psexec to run the powershell from c# asp.net application. I would like to know is there any way to execute powershell commands without using a third party tool like psexec?
View 1 Replies
Mar 14, 2011
I have a couple of powershell command that are very simple.
disable-mailbox dadelgad -confirm:$false
enable-mailbox -identity 'dadelgad' -database 'NET5014DB10' -Alias 'dadelgad'
The first command is to disable an exchange mailbox and the second enables the mailbox. I am logged in as a user who is in the Organization Management group which has full admin priviledges to Exchange but is not a domain admin. If I run these commands directly in Powershell, they work fine but they do not work when called from C#.
I created a very simple windows forms app that has a couple of buttons that invokes these commands from C# code. Running the app as the user with full Exchange right, most commands work with no problem such as get-mailbox -identity 'dadelgad'. I can set flags in Exchange, add alias emails and do most functions but I cannot disable or enable an account.Do I need to be a domain admin to do these functions. It almost seems like a permission issue but the user has full rights to Exchange and can perform both of these commands directly in Powershell.
View 1 Replies
Nov 18, 2010
I have an application that I use to run Exchange Powershell commands inside C# code like below. This is an example of the relevant lines I use to run the powershell command.
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
//load Exchange shell
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException);
Runspace runSpace = RunspaceFactory.CreateRunspace(rsConfig);
//open runspace
runSpace.Open();
//setup pipeline
Pipeline pipeLine = runSpace.CreatePipeline();
String sScript = "get-mailbox -identity 'rj'";
//add script to pipeline
pipeLine.Commands.AddScript(sScript);
//run the script
pipeLine.Invoke();
pipeLine.Dispose();
This code works perfect in all cases until now. the script I am trying to run instead of the one above is to set the RetentionPolicy for a mailbox. The script I am trying to run looks like this: Set-Mailbox -Identity 'rj' -RetentionPolicy 'Main Campus Retention Policy' When I run this in powershell itself it works perfectly but when I try to run it using the code below I get the error, "Cannot invoke this function because the current host does not implement it."
From this error, it almost seems like the command that runs in C# cannot run the RetentionPolicy command but that doesn't make much sense. I have Googled this and tried everything suggested but no luck.
View 1 Replies