从网页执行远程Powershell命令



背景:我是公司的高级系统管理员。当谈到Powershell和Bash时,我是个新手,但在Web开发方面没有任何经验。我对OOP很熟悉。

要求:用户需要访问远程Win服务器上非常特定的任务,如运行某些计划任务、检查某些URL、回收IIS应用程序池等。所有这些都可以使用Powershell轻松编写脚本。与其让用户直接访问脚本,不如屏蔽门户网站后面的所有内容。然后,在使用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;
    }

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

最新更新