2016-01-06 176 views
0

Backgroud:我擔任我公司的高級系統管理員。當談到Powershell和Bash時,我很新手,但在Web開發方面沒有任何經驗。總的來說,我對OOP很熟悉。從網頁執行遠程PowerShell命令

要求:用戶需要訪問遠程Win服務器上的特定任務,例如運行某些計劃任務,檢查某些URL,回收IIS應用程序池等等。所有操作都可以使用Powershell輕鬆編寫腳本。 而不是讓用戶直接訪問腳本,掩蓋Web Portal後面的所有內容。然後,在使用LDAP進行身份驗證後,將爲用戶提供一組可直接從門戶運行的預安裝腳本。

挑戰:沒有事先編程經驗做了這一切由我自己。

問題:從哪裏開始?我是否首先開始學習C#? ASP .NET? MVC? JavaScript的? HTML?我很遺憾,並希望得到一些一般指導。

回答

0

我.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

1

看一下Powershell Web Access。也許這是一種避免學習所有你提到的技術的方法。

+0

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