若是在雲服務程序啓動時候,須要執行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