在C#中从PowerShell创建的Azure站点未找到

时间:2015-02-11 13:52:58

标签: 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();
}