若是在云服务程序启动时候,须要执行Powershell脚本,咱们须要将脚本嵌入到程序中,而且编写一个cmd来执行这个脚本,具体以下:shell
1.编写测试的Powershell脚本:每隔10分钟 检测dnsapi
$TimeStart = Get-Date $TimeEnd = $timeStart.addminutes(1440) $name = "cnppmedia.blob.core.chinacloudapi.cn." $result = "d:\nslookuplog.txt" Write-Host "Start Time: $TimeStart" write-host "End Time: $TimeEnd" Do { $TimeNow = Get-Date if ($TimeNow -ge $TimeEnd) { Write-host "It's time to finish." } else { try { Write-host "===========NslookUp====Current Time : $(Get-Date)==============" invoke-command -scriptblock{ Write-Output "===========Test At Azure ====Current Time : $(Get-Date)==============" | out-file -FilePath $result -Append -Force } invoke-command -scriptblock{ nslookup $name | out-file -FilePath $result -Append -Force } } Catch { } } Start-Sleep -Seconds 600 } Until ($TimeNow -ge $TimeEnd)
2.编写一个cmd文件来执行这个脚本:测试
echo Configuring powershell permissions powershell -c "set-executionpolicy unrestricted" echo start test dns PowerShell .\testdns.ps1 if %ERRORLEVEL% neq 0 goto error echo SUCCESS exit /b 0
3.将这2个文件嵌入到程序中,而且都设置"Copy always"属性:spa
4.修改csdef文件,添加启动任务Startuprest
<?xml version="1.0" encoding="utf-8"?> <ServiceDefinition name="testdnswithsdk26" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2015-04.2.6"> <WebRole name="WebRole1" vmsize="Small"> <Startup> <Task commandLine="testdns.cmd" executionContext="elevated" taskType="background"/> </Startup> <Sites> <Site name="Web"> <Bindings> <Binding name="Endpoint1" endpointName="Endpoint1" /> </Bindings> </Site> </Sites> <ConfigurationSettings> <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" /> </ConfigurationSettings> <Endpoints> <InputEndpoint name="Endpoint1" protocol="http" port="80" /> </Endpoints> </WebRole> </ServiceDefinition>
由于测试的powershell脚本要连续执行一段时间,因此须要将任务类型设置为background,不然没法完成部署code