行PowerShell脚本的Azure的cmdlet在已发布的W

行PowerShell脚本的Azure的cmdlet在已发布的W

本文介绍了如何运行PowerShell脚本的Azure的cmdlet在已发布的Web角色VS2012项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VS 2012 MVC 4 Web角色项目,我成功地调用从角色的bin目录中的PowerShell脚本后,我跑设置ExecutionPolicy不受限制从启动CMD。该脚本包含了一些湛蓝的cmdlet,当我在本地运行该项目剧本完美的作品。然而,当我发布在Azure上的项目,在脚本一样湛蓝的cmdlet不运行。例如:

 参数([字符串] $ websiteName =itudkcloudqwerdfs,[字符串] $ serverLogin =用户,[字符串] $ serverPass =paSS123!@#,[字符串] $位置=北欧)#创建新网站
#$网站=新AzureWebsite -Location $位置-Name $ websiteName如果($网站-eq $空)
{
    扔网站创建错误
    出口
}

在本地运行成功创建了一个Azure的网站,但公布,并从云服务中运行时抛出,然后退出。

当然,我用Google搜索周围的解决方案,我发现我不得不pre-安装才能运行Azure中的cmdlet有天青PowerShell将Web角色。要做到这一点,我安装了Web平台安装程序4.5,复制它的命令行工具,可执行文件,Microsoft.Web.PlatformInstaller DLL和我的Azure publishsettings文件到我的Web角色的bin文件夹,我创建了一个从webpicmd安装的Azure PowerShell来一个cmd在Web角色的bin目录,进口对Web角色启动我publishsettings文件的特殊文件夹:

  MD%〜dp0appdata
CD%〜dp0appdata
光盘..REG ADDHKU \\ .DEFAULT \\软件\\微软\\的Windows \\ CurrentVersion \\ Explorer中\\用户的shell文件夹/ V本地应用程序数据/吨REG_EXPAND_SZ / D%〜dp0appdata/˚F%〜DP0 \\ WebpiCmd.exe/安装/ ACCEPTEULA /产品:WindowsAzurePowershell /log:%~dp0WindowsAzurePowershell.logREG ADDHKU \\ .DEFAULT \\软件\\微软\\的Windows \\ CurrentVersion \\ Explorer中\\用户的shell文件夹/ V本地应用程序数据/吨REG_EXPAND_SZ / D %% USERPROFILE %% \\应用程序数据\\本地/˚FPowerShell的进口AzurePublishSettingsFile
退出/ B 0

这仍然没有奏效,所以我远程登录到该实例亲眼看看有什么可能是错误的。我试图运行上面的CMD脚本,它需要启用了.NET Framework 3.5的功能,看起来怪我,因为实例已经在默认情况下(Windows Server 2012中)启用了.NET 4.5功能。无论如何,我启用了从服务器管理和脚本没有错误成功运行。我打开PowerShell和我测试了一些Azure的cmdlet和他们的工作。然而,即使在这之后,当我从Web应用程序再次调用脚本,它仍然不运行蔚蓝的cmdlet和退出。

我在想什么? ?为什么webpicmd需要.NET 3.5的功能?是否有其他解决这个?

更新:拉夫的评论之后,我加入到这一点我的WebRole标记下ServiceDefinition.csdef中运行在提升执行上下文中webrole但蔚蓝的cmdlet仍然没有在脚本运行:

 <运行时的ExecutionContext =架空/>

更新:这里的C#code调用脚本

// create命令
字符串路径= HttpContext.Server.MapPath(/斌/ InitializeResources.ps1);
PSCommand CMD =新PSCommand();cmd.AddCommand(路径);
cmd.AddParameter(websiteName,websiteName);
cmd.AddParameter(serverLogin,用户名);
cmd.AddParameter(serverPass,密码);
cmd.AddParameter(位置,位置);//设置命令到PowerShell的对象,并调用它
运行空间运行空间= RunspaceFactory.CreateRunspace();
PowerShell的PS = PowerShell.Create();
ps.Commands = CMD;尝试
{
    runspace.Open();
    RunspaceInvoke runSpaceInvoker =新RunspaceInvoke(运行空间);
    runSpaceInvoker.Invoke(设置-ExecutionPolicy -Scope的currentUser -ExecutionPolicy下RemoteSigned -Force);
    ps.Runspace =运行空间;    收集和LT; PSObject> commandResults = ps.Invoke();    //保存结果
    WebsiteInfo websiteInfo =新WebsiteInfo();
    的foreach(在commandResults PSObject结果)
    {
        websiteInfo.Connectionstring =(字符串)result.Members [的ConnectionString]值。
        websiteInfo.WebsiteURL =(字符串)result.Members [WebsiteUrl]值。
        websiteInfo.ftpUrl =(字符串)result.Members [SelfLink]值。
        websiteInfo.ftpUrl = websiteInfo.ftpUrl.Substring(8);
        INT指数= websiteInfo.ftpUrl.IndexOf(:);
        如果(指数大于0)
            websiteInfo.ftpUrl = websiteInfo.ftpUrl.Substring(0,索引);
        websiteInfo.ftpUrl = websiteInfo.ftpUrl.Replace(API,FTP);
        websiteInfo.publishUsername =(字符串)result.Members [资源库]值+\\\\+(字符串)result.Members [PublishUsername]价值。;
        websiteInfo.publishPassword =(字符串)result.Members [PublishPassword]​​值。
    }    runspace.Dispose();
    运行空间= NULL;
    ps.Dispose();
    PS = NULL;
}


解决方案

目前,当我们做了与almamouz该项目的时候,它是不可能通过Azure的REST API生成的资源,也有可能做到这一点通过从PowerShell脚本webrole出于安全考虑。
我们结束了使用Web和工人角色和PowerShell脚本在Worker角色运行。它包含了所有敏感的服务器连接信息。

I have a VS 2012 MVC 4 web role project and I successfully call a powershell script from the bin directory of the role, after I run Set-ExecutionPolicy Unrestricted from a startup cmd. The script contains some azure cmdlets and when I run the project locally the script works perfectly. However when I publish the project on Azure, the same azure cmdlets in the script do not run. For example:

param([string]$websiteName="itudkcloudqwerdfs", [string]$serverLogin="user", [string]$serverPass="paSS123!@#", [string]$location="North Europe")

# Create new Website
#

$website = New-AzureWebsite -Location $location -Name $websiteName

if ($website -eq $null)
{
    throw "Website creation error"
    exit
}

creates an Azure website successfully when run locally but throws and exits when published and run from the cloud service.

Of course I googled around for a solution and I found that I have to pre-install Azure Powershell to the Web Role in order to run Azure cmdlets there. To do that, I installed the web platform installer 4.5, copied its command line tool executable with the Microsoft.Web.PlatformInstaller dll and my Azure publishsettings file to my Web Role's bin folder and I created a cmd that installs the Azure Powershell from webpicmd to a special folder in the Web Role's bin directory and imports my publishsettings file on Web Role startup:

md "%~dp0appdata"
cd "%~dp0appdata"
cd..

reg add "hku\.default\software\microsoft\windows\currentversion\explorer\user shell folders" /v "Local AppData" /t REG_EXPAND_SZ /d "%~dp0appdata" /f

"%~dp0\WebpiCmd.exe" /Install /AcceptEula /Products:WindowsAzurePowershell /log:%~dp0WindowsAzurePowershell.log

reg add "hku\.default\software\microsoft\windows\currentversion\explorer\user shell folders" /v "Local AppData" /t REG_EXPAND_SZ /d %%USERPROFILE%%\AppData\Local /f

powershell Import-AzurePublishSettingsFile
exit /b 0

It still didn't work, so I remotely logged in to the instance to see for myself what could be wrong. I tried to run the cmd script above and it required the .NET Framework 3.5 feature enabled, which looks weird to me since the instance already has the .NET 4.5 feature enabled by default (Windows Server 2012). Anyway, I enabled it from the server manager and the script ran successfully with no errors. I opened the powershell and I tested some Azure cmdlets and they worked. However, even after that, when I invoke the script again from the web app, it still doesn't run the azure cmdlets and exits.

What am I missing? And why does webpicmd requires the .NET 3.5 feature? Is there an alternative solution to this?

Update: After Gaurav's comment, I added this into my servicedefinition.csdef under the WebRole tag to run the webrole in an elevated execution context but the azure cmdlets are still not running in the script:

<Runtime executionContext="elevated" />

Update: Here's the C# code that invokes the script

//create command
string path = HttpContext.Server.MapPath("/bin/InitializeResources.ps1");
PSCommand cmd = new PSCommand();

cmd.AddCommand(path);
cmd.AddParameter("websiteName", websiteName);
cmd.AddParameter("serverLogin", username);
cmd.AddParameter("serverPass", password);
cmd.AddParameter("location", location);

//set the command into a powershell object and invoke it
Runspace runspace = RunspaceFactory.CreateRunspace();
PowerShell ps = PowerShell.Create();
ps.Commands = cmd;

try
{
    runspace.Open();
    RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
    runSpaceInvoker.Invoke("Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force");
    ps.Runspace = runspace;

    Collection<PSObject> commandResults = ps.Invoke();

    //save results
    WebsiteInfo websiteInfo = new WebsiteInfo();
    foreach (PSObject result in commandResults)
    {
        websiteInfo.Connectionstring = (string)result.Members["ConnectionString"].Value;
        websiteInfo.WebsiteURL = (string)result.Members["WebsiteUrl"].Value;
        websiteInfo.ftpUrl = (string)result.Members["SelfLink"].Value;
        websiteInfo.ftpUrl = websiteInfo.ftpUrl.Substring(8);
        int index = websiteInfo.ftpUrl.IndexOf(":");
        if (index > 0)
            websiteInfo.ftpUrl = websiteInfo.ftpUrl.Substring(0, index);
        websiteInfo.ftpUrl = websiteInfo.ftpUrl.Replace("api", "ftp");
        websiteInfo.publishUsername = (string)result.Members["Repository"].Value + "\\" + (string)result.Members["PublishUsername"].Value;
        websiteInfo.publishPassword = (string)result.Members["PublishPassword"].Value;
    }

    runspace.Dispose();
    runspace = null;
    ps.Dispose();
    ps = null;
}
解决方案

At the time when we did that project with almamouz, it was not possible to generate resources via Azure REST API nor it was possible to do that via powershell scripts from webrole due to security considerations.We ended up using web and worker roles and the powershell script was running in worker role. It contained all the sensitive server connection info.

这篇关于如何运行PowerShell脚本的Azure的cmdlet在已发布的Web角色VS2012项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:11