就是这样,代码在测试环境中运行流畅(使用windows 8.1,带有SP4的Visual Studio 2013)。
关键是我的代码在部署到生产环境时没有连接到 MSOnline。当我使用 Webdeploy 选项进行部署时,我无法弄清楚出了什么问题......我只是找不到它有什么问题......我真的需要帮助。它使用 MSOnline 和扩展模块运行 powershell。
我确实安装了模块 MSOnline: http ://www.microsoft.com/en-us/download/details.aspx?id= 39267 64 位的 MSOnline 模块。这个脚本:
set A=C:\Windows\System32\WindowsPowerShell\v1.0\Modules
set M1=MSOnline
set M2=MSOnlineExtended
set B=C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules
IF NOT EXIST %B%\%M1% mkdir %B%\%M1%
IF NOT EXIST %B%\%M2% mkdir %B%\%M2%
xcopy %A%\%M1% %B%\%M1% /s /e
xcopy %A%\%M2% %B%\%M2% /s /e
pause
这准备了从 32 位到 64 位运行的模块。
它在安装了 Visual Studio 2013 的地方运行良好,但是当我部署到 WebServers 时......只是不起作用......我试图在这里和互联网上找到类似的东西,没有快乐也没有运气...... .我很感激你能给我的任何帮助,谢谢!
这是我在 ASP.NET 中的代码:
<%@ Page Title="Domain Info" Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.Master" CodeBehind="DomainInfo.aspx.cs" Inherits="LSO365Portal.DomainInfo" %>
<%@ MasterType VirtualPath="~/Site.Master" %>
<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1> <%= titulo %> </h1>
<h3>Here where you'll find interesting information about your Office 365 Domains.</h3>
</hgroup>
</div>
</section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<h3> Execute Script pressing the following button</h3>
<p>
<asp:Table HorizontalAlign="Center" runat="server">
<asp:TableRow>
<asp:TableCell>
<asp:Button ID="Execute" runat="server" Text="Execute Script" OnClick="RunScript" Height="35px" Width="150px" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</p>
<h3> Results</h3>
<p>
<asp:Table ID="SubsTable" runat="server" GridLines="Both" HorizontalAlign="Center" CssClass="ResultTables">
</asp:Table>
</p>
<h3> Elapsed</h3>
<asp:Label ID="EnlapsedLabel" runat="server" Text=""></asp:Label>
<h3> Status</h3>
<p>
<asp:TextBox ID="Stats" runat="server" Width="850px" Height="80px" ReadOnly="True" TextMode="MultiLine"></asp:TextBox>
</p>
</asp:Content>
这是后面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel; //Collection<T>
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text; //stringbuilder
using System.Security; //securestring
using System.Text.RegularExpressions;
using System.Diagnostics; //stopwatch
namespace LSO365Portal
{
public partial class DomainInfo : System.Web.UI.Page
{
protected const string PasswordMask = "*********************************************************************************************************************************";
public String titulo = "Domains Information in Office 365";
Stopwatch watch = new Stopwatch();
StringBuilder status = new StringBuilder();
protected void Page_Load(object sender, EventArgs e){}
//protected void UpdateTimer_Tick(object sender, EventArgs e)
//{
// DateStampLabel.Text = watch.ElapsedMilliseconds.ToString();
//}
protected void RunScript(object sender, EventArgs e)
{
watch.Reset();
watch.Start();
// Clean the Result TextBox
Stats.Text = string.Empty;
status.Clear();
//check1
status.AppendLine("watch reseted and status cleared");
StringBuilder script = new StringBuilder();
try
{
// Import-Module MSOnline OPC #1
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { "MSOnline,MSOnlineExtended" });
//check2
//status.AppendLine("initial state and MSOnline Loaded");
Runspace rs = RunspaceFactory.CreateRunspace(iss);
rs.Open();
//status.AppendLine(rs.InitialSessionState.ToString());
//check3
//status.AppendLine("Created and open runspace");
Pipeline pipeLine = rs.CreatePipeline();
string un = String.Empty;
string pw = String.Empty;
TextBox tbUn = (TextBox)Master.FindControl("Username");
TextBox tbPw = (TextBox)Master.FindControl("Textpwd");
//check 4
//status.AppendLine("Created pipeline");
if (tbUn != null && tbPw != null)
{
un = tbUn.Text;
pw = tbPw.Text;
}
else
{
status.AppendLine("Error trying to find username and password from the master page");
}
//Set variables Username and password
script.AppendLine("$username = '" + un + "';");
//ConvertTo-SecureString ' MySPW ' –asplaintext –force
script.AppendLine("$password = Convertto-securestring " + "'" + pw + "'" + "-asplaintext -force");
script.AppendLine("$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password;");
// Check Username and password
script.AppendLine("write-Output $username");
script.AppendLine("write-Output $password");
//connect and check connection
script.AppendLine("Connect-MsolService -credential $cred -ErrorAction SilentlyContinue");
script.AppendLine("if($?){ write-output 'connected' } else{write-output 'disconnected'}");
//check 5
//$DomainInfo = Get-MsolDomain | select Name,Authentication,Capabilities,Status,isDefault,isInitial
//status.AppendLine("Get-MsolDomain | select Name,Authentication,Capabilities,Status,isDefault,isInitial");
script.AppendLine("Get-MsolDomain | select Name,Authentication,Capabilities,Status,isDefault,isInitial");
//Add scripts to the pipeline
pipeLine.Commands.AddScript(script.ToString());
//check 7
status.AppendLine("Added script to pipeline");
status.AppendLine("this is the script");
status.AppendLine(script.ToString());
//execute pipeline and get the results PSObjects
Collection<PSObject> resultObjects = pipeLine.Invoke();
//check 8
status.AppendLine("Total results Objects are: " + resultObjects.Count.ToString());
foreach (PSObject obj in resultObjects)
{
status.AppendLine(obj.ToString());
}
//Clear Script and close Runspace
script.Clear();
rs.Close();
if (resultObjects.Count > 0 || resultObjects != null)
{
GenerateTable(resultObjects);
}
}
catch (System.Management.Automation.RuntimeException ex)
{
status.AppendLine(ex.Message);
}
status.AppendLine("Ready");
Stats.Text = status.ToString();
watch.Stop();
double elap = watch.ElapsedMilliseconds / 1000;
EnlapsedLabel.Text = "Work Done in : " + elap.ToString() + " seconds";
}
private string SetRowHeadersName(int i)
{
//Name,Authentication,Capabilities,Status,isDefault,isInitial
string text = String.Empty;
switch (i)
{
case 0:
text = "Name";
break;
case 1:
text = "Authentication";
break;
case 2:
text = "Capabilities";
break;
case 3:
text = "Status";
break;
case 4:
text = "is Default";
break;
case 5:
text = "is Initial";
break;
default:
text = "Error on SetRowHeadersName Function";
break;
}
return text;
}
private string SetCellValue(int j, PSObject result)
{
string text = string.Empty;
switch (j)
{
//Name,Authentication,Capabilities,Status,isDefault,isInitial
case 0:
text = result.Properties["Name"].Value.ToString();
break;
case 1:
text = result.Properties["Authentication"].Value.ToString();
break;
case 2:
text = result.Properties["Capabilities"].Value.ToString();
break;
case 3:
text = result.Properties["Status"].Value.ToString();
break;
case 4:
text = result.Properties["isDefault"].Value.ToString();
break;
case 5:
text = result.Properties["isInitial"].Value.ToString();
break;
default:
text = "Value not found, column:" + j;
break;
}
return text;
}
private void GenerateTable(Collection<PSObject> objs)
{
int rowsCount = objs.Count;
//The number of Columns to be generated
const int colsCount = 6;//You can changed the value of 5 based on you requirements NextLifecycleDate,SkuId,SkuPartNumber,Status,TotalLicenses
// Now iterate through the table and add your controls
try
{
//ADD HEADER NAMES
TableRow rowHeader = new TableRow();
for (int i = 0; i < colsCount; i++)
{
//create a new header
TableCell header = new TableCell();
//create new label
Label headertext = new Label();
//set text of label
headertext.Text = SetRowHeadersName(i);
//set unique ID
header.ID = "headerCell_" + i;
header.CssClass = "tableheader";
//add control to the header
header.Controls.Add(headertext);
// Add the TableCell to the TableRow
rowHeader.Cells.Add(header);
}
SubsTable.Rows.Add(rowHeader);
//Add info on PSObjects
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();
Label tb = new Label();
string RowText = string.Empty;
cell.CssClass = "tablecell";
RowText = SetCellValue(j, objs[i]);
tb.Text = RowText;
// Set a unique ID for each TextBox added
tb.ID = "Row_" + i + "Col_" + j;
// Add the control to the TableCell
cell.Controls.Add(tb);
// Add the TableCell to the TableRow
row.Cells.Add(cell);
}
// And finally, add the TableRow to the Table
SubsTable.Rows.Add(row);
}
//Sore the current Rows Count in ViewState
ViewState["RowsCount"] = rowsCount;
}
catch (Exception ex)
{
status.Append("Error in GenerateTable() " + ex.Message);
}
}
}
}