0
我有一个奇怪的问题。我试图通过从C#代码运行PowerShell脚本来创建Azure站点。我打开脚本文件,修改它以插入必要的数据,保存并运行它。这是脚本:从C#中的PowerShell创建Azure网站没有被发现
$azureUserName = "myusername"
$azurePass = "mypass"
$siteName = "myurl"
$clientID = "*******"
$clientSecret = "********"
$subscriptionID = "*******"
$securePassword = ConvertTo-SecureString -String $azurePass -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($azureUserName, $securePassword)
Add-AzureAccount -Credential $cred
Select-AzureSubscription -SubscriptionId $subscriptionID
New-AzureWebsite $siteName | Set-AzureWebsite -WebSocketsEnabled $true -AppSettings @{"ClientID"=$clientID;"ClientSecret"=$clientSecret}
#Get the website's propeties.
$website = Get-AzureWebSite -Name $siteName
$siteProperties = $website.SiteProperties.Properties
#extract url, username and password
$url = ($siteProperties | ?{ $_.Name -eq "RepositoryURI" }).Value.ToString() + "/MsDeploy.axd"
$userName = ($siteProperties | ?{ $_.Name -eq "PublishingUsername" }).Value
$pw = ($siteProperties | ?{ $_.Name -eq "PublishingPassword" }).Value
#build the command line argument for the deploy.cmd :
$argFormat = ' /y /m:"{0}" -allowUntrusted /u:"{1}" /p:"{2}" /a:Basic'
$arguments = [string]::Format($argFormat, $url, $userName, $pw)
事情是。如果我手动修改数据,然后从PowerShell ISE运行它,该站点正在创建并运行完美。如果我修改了代码中的数据并运行它,该网站也正在创建中,并且似乎可以正常工作,但是当我尝试导航时它未找到。 这就是我如何运行脚本:
using (PowerShell powershell = PowerShell.Create())
{
String file = System.IO.File.ReadAllText(System.IO.Path.Combine(filesPath, "CreateAzureSite.ps1"),Encoding.UTF8);
powershell.AddScript(file);
Collection<PSObject> results = powershell.Invoke();
}
好奇 - 为什么你想在C#中运行PowerShell?为什么不使用Azure的管理REST API? – viperguynaz 2015-02-11 14:56:31
不想玩整个证书的东西。此外,从PowerShell创建网站更容易,因为文档更好。当然,我可以使用.NET库来完成它。然而问题是,什么可能会导致这种影响。 – Ciamas 2015-02-11 15:29:40