从网页执行远程PowerShell命令

问题描述:

Backgroud:我担任我公司的高级系统管理员。当谈到Powershell和Bash时,我很新手,但在Web开发方面没有任何经验。总的来说,我对OOP很熟悉。从网页执行远程PowerShell命令

要求:用户需要访问远程Win服务器上的特定任务,例如运行某些计划任务,检查某些URL,回收IIS应用程序池等等。所有操作都可以使用Powershell轻松编写脚本。 而不是让用户直接访问脚本,掩盖Web Portal后面的所有内容。然后,在使用LDAP进行身份验证后,将为用户提供一组可直接从门户运行的预安装脚本。

挑战:没有事先编程经验做了这一切由我自己。

问题:从哪里开始?我是否首先开始学习C#? ASP .NET? MVC? JavaScript的? HTML?我很遗憾,并希望得到一些一般指导。

我.NET开发人员,有一次,我的任务,使MVC UI与Microsoft Exchange服务器进行交互和管理邮箱AD用户,我不得不学习PowerShell和如何通过C#PowerShell的互动。所以根据我的经验,我会建议你开始学习C#使用控制台应用程序,了解c#如何与Powershell和AD协同工作,并开始学习MVC构建UI。

你应该从NuGet包管理器安装包System.management.Automation。

C#=> Powershell(执行Powershell命令)=> Microsoft Exchange。

简单示例,获取用户PrimarySmtpAddress属性。

using System.Management.Automation; 
using System.Management.Automation.Runspaces; 

private static WSManConnectionInfo _connectionInfo; 

static void Main(string[] args) 
{ 
    string userName = "DOMAIN\\User"; 
    string password = "UserPassowrd"; 
    PSCredential psCredential = new PSCredential(userName, GenerateSecureString(password)); 

    _connectionInfo = new WSManConnectionInfo(
      new Uri("http://server.domain.local/PowerShell"), 
      "http://schemas.microsoft.com/powershell/Microsoft.Exchange", psCredential); 
    _connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; 

    Console.WriteLine(GetPrimarySmtpAddressBy("Firstname Lastname"); 
} 

public static string GetPrimarySmtpAddressBy(string identity) 
    { 
     using (Runspace runspace = RunspaceFactory.CreateRunspace(_connectionInfo)) 
     { 

      using (PowerShell powerShell = PowerShell.Create()) 
      { 
       powerShell.AddCommand("Get-Mailbox"); 
       powerShell.AddParameter("Identity", identity); 
       runspace.Open(); 

       powerShell.Runspace = runspace; 

       PSObject psObject = powerShell.Invoke().FirstOrDefault(); 

       if (psObject != null && psObject.Properties["PrimarySmtpAddress"] != null) 
        return psObject.Properties["PrimarySmtpAddress"].Value.ToString(); 
       else return ""; 
      } 
     } 
    } 

public static System.Security.SecureString GenerateSecureString(string input) 
    { 
     System.Security.SecureString securePassword = new System.Security.SecureString(); 
     foreach (char c in input) 
      securePassword.AppendChar(c); 
     securePassword.MakeReadOnly(); 

     return securePassword; 
    } 
+0

感谢您的洞察,谢谢! – JustAGuy

看一下Powershell Web Access。也许这是一种避免学习所有你提到的技术的方法。

+0

不怕。 没有GUI。只是普通的CLI ... – JustAGuy