Home
%3CLINGO-SUB%20id%3D%22lingo-sub-392602%22%20slang%3D%22en-US%22%3EPowerShell%20script%20to%20execute%20DOS%20commands%20using%20KUDU%20APIs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-392602%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Jul%2013%2C%202018%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3ELets%20say%20you%20would%20like%20to%20get%20the%20machine%20name%20from%20all%20VM%20instances%20that%20are%20running%20your%20Azure%20AppService%20website%20or%20you%20would%20like%20to%20create%20a%20folder%20under%20the%20wwwroot%2C%20this%20below%20sample%20PowerShell%20scripts%20can%20execute%20any%20DOS%20command%3C%2FP%3E%0A%20%20%3CP%3E%3CBR%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22powershell%22%5D%20%3CBR%20%2F%3E%20%23Login-AzureRmAccount%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24resoureGroupName%20%3D%20%22wabac%22%20%3CBR%20%2F%3E%20%24websiteName%20%3D%20%22wabacblue%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24env%20%3D%20%40%7B%20%3CBR%20%2F%3E%20command%3D%20'Set%20COMPUTERNAME'%20%3CBR%20%2F%3E%20dir%3D%20'site'%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%24json%20%3D%20%24env%20%7C%20ConvertTo-Json%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24env2%20%3D%20%40%7B%20%3CBR%20%2F%3E%20command%3D%20'mkdir%20vdir'%20%3CBR%20%2F%3E%20dir%3D%20'site%5Cwwwroot'%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%24json2%20%3D%20%24env2%20%7C%20ConvertTo-Json%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24website%20%3D%20Get-AzureWebsite%20-Name%20%24websiteName%20%3CBR%20%2F%3E%20%24username%20%3D%20%24website.PublishingUsername%20%3CBR%20%2F%3E%20%24password%20%3D%20%24website.PublishingPassword%20%3CBR%20%2F%3E%20%24base64AuthInfo%20%3D%20%5BConvert%5D%3A%3AToBase64String(%5BText.Encoding%5D%3A%3AASCII.GetBytes((%22%7B0%7D%3A%7B1%7D%22%20-f%20%24username%2C%24password)))%20%3CBR%20%2F%3E%20%24apiBaseUrl%20%3D%20%22https%3A%2F%2F%24(%24website.Name).scm.azurewebsites.net%2Fapi%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5BSystem.Uri%5D%24Uri%20%3D%20%24apiBaseUrl%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24instances%20%3D%20Get-AzureRmResource%20-ResourceGroupName%20%24resoureGroupName%20%60%20%3CBR%20%2F%3E%20-ResourceType%20Microsoft.Web%2Fsites%2Finstances%20%60%20%3CBR%20%2F%3E%20-ResourceName%20%24websiteName%20%60%20%3CBR%20%2F%3E%20-ApiVersion%202018-02-01%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20foreach(%24instance%20in%20%24instances)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%24instanceName%20%3D%20%24instance.Name%20%3CBR%20%2F%3E%20Write-Host%20%22%60tVM%20Instance%20ID%20%60t%60t%3A%20%22%20%24instanceName%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23Now%20execute%20'SET%20COMPUTER'%20cmd%20%3CBR%20%2F%3E%20%24cookie%3D%20New-Object%20System.Net.Cookie%20%3CBR%20%2F%3E%20%24cookie.Name%20%3D%20%22ARRAffinity%22%20%3CBR%20%2F%3E%20%24cookie.Value%20%3D%20%24instanceName%20%3CBR%20%2F%3E%20%24Cookie.Domain%20%3D%20%24uri.DnsSafeHost%20%3CBR%20%2F%3E%20%24session%3DNew-Object%20Microsoft.Powershell.Commands.WebRequestSession%20%3CBR%20%2F%3E%20%24session.Cookies.add(%24cookie)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24response%20%3D%20Invoke-RestMethod%20-Uri%20%22%24apiBaseUrl%2Fcommand%22%20%60%20%3CBR%20%2F%3E%20-Headers%20%40%7BAuthorization%3D(%22Basic%20%7B0%7D%22%20%60%20%3CBR%20%2F%3E%20-f%20%24base64AuthInfo)%7D%20%60%20%3CBR%20%2F%3E%20-Method%20Post%20-Body%20%24json%20%60%20%3CBR%20%2F%3E%20-ContentType%20'application%2Fjson'%20%60%20%3CBR%20%2F%3E%20-WebSession%20%24session%20%3CBR%20%2F%3E%20Write-Host%20%22%60tVM%20Instance%20Name%20%60t%3A%20%22%20%24response%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%24response%20%3D%20Invoke-RestMethod%20-Uri%20%22%24apiBaseUrl%2Fcommand%22%20%60%20%3CBR%20%2F%3E%20-Headers%20%40%7BAuthorization%3D(%22Basic%20%7B0%7D%22%20%60%20%3CBR%20%2F%3E%20-f%20%24base64AuthInfo)%7D%20%60%20%3CBR%20%2F%3E%20-Method%20Post%20-Body%20%24json2%20%60%20%3CBR%20%2F%3E%20-ContentType%20'application%2Fjson'%20%60%20%3CBR%20%2F%3E%20-WebSession%20%24session%20%3CBR%20%2F%3E%20Write-Host%20%22%60tCreate%20folder%20Results%20%60t%3A%20%22%20%24response%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-392602%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Jul%2013%2C%202018%20Lets%20say%20you%20would%20like%20to%20get%20the%20machine%20name%20from%20all%20VM%20instances%20that%20are%20running%20your%20Azure%20AppService%20website%20or%20you%20would%20like%20to%20create%20a%20folder%20under%20the%20wwwroot%2C%20this%20below%20sample%20PowerShell%20scripts%20can%20execute%20any%20DOS%20command%20%5Bcode%20language%3D%22powershell%22%5D%23Login-AzureRmAccount%24resoureGroupName%20%3D%20%22wabac%22%24websiteName%20%3D%20%22wabacblue%22%24env%20%3D%20%40%7B%20command%3D%20'Set%20COMPUTERNAME'%20dir%3D%20'site'%7D%24json%20%3D%20%24env%20%7C%20ConvertTo-Json%24env2%20%3D%20%40%7B%20command%3D%20'mkdir%20vdir'%20dir%3D%20'site%5Cwwwroot'%7D%24json2%20%3D%20%24env2%20%7C%20ConvertTo-Json%24website%20%3D%20Get-AzureWebsite%20-Name%20%24websiteName%24username%20%3D%20%24website.%3C%2FLINGO-TEASER%3E
Microsoft
First published on MSDN on Jul 13, 2018


Lets say you would like to get the machine name from all VM instances that are running your Azure AppService website or you would like to create a folder under the wwwroot, this below sample PowerShell scripts can execute any DOS command




[code language="powershell"]
#Login-AzureRmAccount

$resoureGroupName = "wabac"
$websiteName = "wabacblue"

$env = @{
command= 'Set COMPUTERNAME'
dir= 'site'
}
$json = $env | ConvertTo-Json

$env2 = @{
command= 'mkdir vdir'
dir= 'site\wwwroot'
}
$json2 = $env2 | ConvertTo-Json

$website = Get-AzureWebsite -Name $websiteName
$username = $website.PublishingUsername
$password = $website.PublishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$apiBaseUrl = "https://$($website.Name).scm.azurewebsites.net/api"

[System.Uri]$Uri = $apiBaseUrl

$instances = Get-AzureRmResource -ResourceGroupName $resoureGroupName `
-ResourceType Microsoft.Web/sites/instances `
-ResourceName $websiteName `
-ApiVersion 2018-02-01


foreach($instance in $instances)
{
$instanceName = $instance.Name
Write-Host "`tVM Instance ID `t`t: " $instanceName

#Now execute 'SET COMPUTER' cmd
$cookie= New-Object System.Net.Cookie
$cookie.Name = "ARRAffinity"
$cookie.Value = $instanceName
$Cookie.Domain = $uri.DnsSafeHost
$session=New-Object Microsoft.Powershell.Commands.WebRequestSession
$session.Cookies.add($cookie)

$response = Invoke-RestMethod -Uri "$apiBaseUrl/command" `
-Headers @{Authorization=("Basic {0}" `
-f $base64AuthInfo)} `
-Method Post -Body $json `
-ContentType 'application/json' `
-WebSession $session
Write-Host "`tVM Instance Name `t: " $response

$response = Invoke-RestMethod -Uri "$apiBaseUrl/command" `
-Headers @{Authorization=("Basic {0}" `
-f $base64AuthInfo)} `
-Method Post -Body $json2 `
-ContentType 'application/json' `
-WebSession $session
Write-Host "`tCreate folder Results `t: " $response
}

[/code]