Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Azure Powershell Create Or Delete Job
WhatsApp
Bipin Patil
Jul 15
2015
1.1
k
0
0
workflow CreateorDeleteJob
{
# Specify input parameters here
param (
[parameter(Mandatory=$
true
)]
[boolean]$IsCreate,
[parameter(Mandatory=$
true
)]
[
string
]$SubscriptionName=
"Windows Azure Enterprise"
,
[parameter(Mandatory=$
true
)]
[
string
]$StorageAccount =
"storageName"
,
[parameter(Mandatory=$
true
)]
[
string
]$ConfigFileName =
"ServiceConfiguration.cscfg"
,
[parameter(Mandatory=$
true
)]
[
string
]$BlobContainer =
"package"
,
[parameter(Mandatory=$
true
)]
[
string
]$service =
"DevService"
,
[parameter(Mandatory=$
true
)]
[
string
]$slot =
"production"
,
[parameter(Mandatory=$
true
)]
[
string
]$packageFileName =
"DevService.cspkg"
)
$TempFileLocation =
"C:\temp\ServiceConfiguration.cscfg"
$package =
"https://$StorageAccount.blob.core.windows.net/$BlobContainer/$packageFileName"
$timeStampFormat =
"g"
$deploymentLabel =
"PowerShell Deploy to $service"
Write-Output
"Slot: $slot"
Write-Output
"Service: $service"
Write-Output
"Package: $package"
Write-Output
"TempFileLocation: $TempFileLocation"
Write-Output
"create or delete : $IsCreate"
function Copy-FileFromAzureStorageToSandbox($ConfigFileName, $BlobContainer, $TempFileLocation){
$blob = Get-AzureStorageBlobContent -Blob $ConfigFileName -Container $BlobContainer -Destination $TempFileLocation -Force
Write-Output $TempFileLocation
return
$TempFileLocation
}
function CreateNewDeployment($slot, $package, $TempFileLocation, $deploymentLabel, $service, $timeStampFormat)
{
write-progress -id 3 -activity
"Creating New Deployment"
-Status
"In progress"
Write-Output
"$(Get-Date -f $timeStampFormat) - Creating New Deployment: In progress"
$opstat = New-AzureDeployment -Slot $slot -Package $package -Configuration $TempFileLocation -label $deploymentLabel -ServiceName $service
$completeDeployment = Get-AzureDeployment -ServiceName $service -Slot $slot
$completeDeploymentID = $completeDeployment.deploymentid
write-progress -id 3 -activity
"Creating New Deployment"
-completed -Status
"Complete"
Write-Output
"$(Get-Date -f $timeStampFormat) - Creating New Deployment: Complete, Deployment ID: $completeDeploymentID"
write-progress -id 4 -activity
"Starting Instances"
-status $opstat
Write-Output
"$(Get-Date -f $timeStampFormat) - Starting Instances: $opstat"
}
function UpgradeDeployment($slot, $package, $TempFileLocation, $deploymentLabel, $service, $timeStampFormat)
{
write-progress -id 3 -activity
"Upgrading Deployment"
-Status
"In progress"
Write-Output
"$(Get-Date -f $timeStampFormat) - Upgrading Deployment: In progress"
# perform Update-Deployment
$setdeployment = Set-AzureDeployment -Upgrade -Slot $slot -Package $package -Configuration $TempFileLocation -label $deploymentLabel -ServiceName $service -Force
$completeDeployment = Get-AzureDeployment -ServiceName $service -Slot $slot
$completeDeploymentID = $completeDeployment.deploymentid
write-progress -id 3 -activity
"Upgrading Deployment"
-completed -Status
"Complete"
Write-Output
"$(Get-Date -f $timeStampFormat) - Upgrading Deployment: Complete, Deployment ID: $completeDeploymentID"
}
function SuspendDeployment($slot, $service)
{
write-progress -id 1 -activity
"Suspending Deployment"
-status
"In progress"
Write-Output
"$(Get-Date -f $timeStampFormat) - Suspending Deployment: In progress"
$suspend = Set-AzureDeployment -Slot $slot -ServiceName $service -Status Suspended
write-progress -id 1 -activity
"Suspending Deployment"
-status
"Suspended"
Write-Output
"$(Get-Date -f $timeStampFormat) - Suspending Deployment"
}
# START starting point of code ------------------------------------------------------------------
# do not run deployment on saturday or sunday
if
($(Get-Date).DayOfWeek.ToString() -ne
"Saturday"
-and $(Get-Date).DayOfWeek.ToString() -ne
"Sunday"
)
{
$AzureConn = Get-AutomationConnection -Name
"Test"
if
($AzureConn -eq $
null
)
{
throw
"Could not retrieve '$AzureConnectionName' connection asset. Check that you created this first in the Automation service."
}
# Get the Azure management certificate that is used to connect to this subscription
$Certificate = Get-AutomationCertificate -Name $AzureConn.AutomationCertificateName
if
($Certificate -eq $
null
)
{
throw
"Could not retrieve '$AzureConn.AutomationCertificateName' certificate asset. Check that you created this first in the Automation service."
}
# $cred = Get-AutomationPSCredential -Name $UserName
# Add-AzureAccount -Credential $cred
Set-AzureSubscription -SubscriptionName $SubscriptionName -SubscriptionId $AzureConn.SubscriptionID -Certificate $Certificate -CurrentStorageAccount $StorageAccount
# Set-AzureSubscription –SubscriptionName $SubscriptionName -CurrentStorageAccount $StorageAccount
Select-AzureSubscription -SubscriptionName $SubscriptionName
if
($IsCreate)
{
Write-Output
"Create Azure Deployment"
#Publish $slot $package $TempFileLocation $deploymentLabel $service $timeStampFormat $ConfigFileName $BlobContainer
$deployment = Get-AzureDeployment -ServiceName $service -Slot $slot -ErrorVariable a -ErrorAction silentlycontinue
Copy-FileFromAzureStorageToSandbox -ConfigFileName $ConfigFileName -BlobContainer $BlobContainer -TempFileLocation $TempFileLocation
if
($deployment.Name -ne $
null
) {
#Update deployment inplace (usually faster, cheaper, won't destroy VIP)
Write-Output
"$(Get-Date -f $timeStampFormat) - Deployment exists in $service. Upgrading deployment."
UpgradeDeployment $slot $package $TempFileLocation $deploymentLabel $service $timeStampFormat
}
else
{
Write-Output
"$(Get-Date -f $timeStampFormat) - No deployment is detected. Creating a new deployment. "
CreateNewDeployment $slot $package $TempFileLocation $deploymentLabel $service $timeStampFormat
}
# remove the config file from temp sandbox location
remove-item -path $TempFileLocation -force
#update-BlobFiles -BlobContainer $BlobContainer -FileName $packageFileName
#update-BlobFiles -BlobContainer $BlobContainer -FileName $ConfigFileName
}
else
{
# SuspendDeployment $slot $service
Write-Output
"$(Get-Date -f $timeStampFormat) - Deleting the $slot deployment "
Remove-AzureDeployment -ServiceName $service -Slot $slot -Force
write-progress -id 3 -activity
"Deleting Deployment"
-completed -Status
"Complete"
}
}
# END ending point of code ------------------------------------------------------------------
}
Azure
Powershell
Up Next
Azure Powershell Create Or Delete Job