Giter Club home page Giter Club logo

pwshazcontainer's Introduction

PwshAzContainer Module

PwshAzContainer is a PowerShell binary module built using .NET Core and designed to work with PowerShell Core. It provides cmdlets, written in C#, for interacting with Azure Container Apps, Azure Container App Jobs, and Azure Container Instances.

Dependencies

.NET SDK

.NET SDK 8.0.102

NuGet Packages

PwshAzContainer depends on the following NuGet packages:

Installation

To install the PwshAzContainer module, you can use the PowerShell Gallery:

Install-Module -Name PwshAzContainer -Scope CurrentUser -verbose

Usage

PwshAzContainer provides cmdlets for various Azure Container Apps, Jobs and Instances operations, including creating, getting, updating, and deleting.

Initialize Azure RM Client

first you need to initialize an Azure Resource Manager client.

Azure PowerShell Credential

Clear-AzContext -Force
Connect-AzAccount -Tenant XXXXX

Connect-AzResourceManager -verbose

Azure CLI Credential

az login

Connect-AzResourceManager -verbose

User Assigned Managed Identity

$env:MANAGED_IDENTITY_CLIENT_ID = '<user-assigned-managed-identity-client-id'

Connect-AzResourceManager -verbose

Get Azure Container Apps

Get-AzContainerAppResource -verbose
Get-AzContainerAppResource -ResourceGroupName MyGroup -verbose
Get-AzContainerAppResource -Name MyApp -ResourceGroupName MyGroup -verbose

Create Azure Container App

$IdentityResourceId = '<User-Assigned Managed Identity Resource Id>'
$ContainerAppName = "ca-$(-join ((65..90) + (97..122) | Get-Random -Count 8 | ForEach-Object {[char]$_}))".ToLower()
$ResourceGroup = '<MyGroup>'
$ContainerRegistryServer = '<Container Registry Server>'
$ContainerImage = "$ContainerRegistryServer/<container-name>:<tag>"
$ContainerName = "$(-join ((65..90) + (97..122) | Get-Random -Count 5 | ForEach-Object {[char]$_}))".ToLower()
$EnvironmentName = '<Container App Managed Environment Name>'
$Environment = Get-AzContainerAppEnvironment -Name $EnvironmentName -ResourceGroupName $ResourceGroup
$EnvironmentId = $Environment.id

$Ingress = New-AzContainerAppIngress -External $false -TargetPort 80
$Template = New-AzContainerAppTemplate -ContainerImage $ContainerImage -ContainerName $ContainerName
$Registries = New-AzContainerAppRegistryCredentials -Identity $IdentityResourceId -Server $ContainerRegistryServer

$ContainerApp = New-AzContainerAppResource -Name $ContainerAppName -ResourceGroupName $ResourceGroup -EnvironmentId $EnvironmentId -ConfigIngressObject $ingress -ContainerTemplate $Template -ConfigRegistries $Registries -Identity $IdentityResourceId -verbose
$ContainerApp.Data

Delete Azure Container App

Remove-AzContainerAppResource -Name MyApp -ResourceGroupName MyGroup -Verbose
Remove-AzContainerAppResource -ResourceId $ContainerAppResourceId -Verbose

Create Azure Container App Job

$IdentityResourceId = '<User-Assigned Managed Identity Resource Id>'
$IdentityClientId = '<User-Assigned Managed Identity Client Id>'
$ContainerAppJobName = "caj-$(-join ((65..90) + (97..122) | Get-Random -Count 8 | ForEach-Object {[char]$_}))".ToLower()
$ResourceGroup = '<MyGroup>'
$ContainerImage = "mcr.microsoft.com/azure-powershell:latest"
$ContainerName = "$(-join ((65..90) + (97..122) | Get-Random -Count 5 | ForEach-Object {[char]$_}))".ToLower()
$EnvironmentName = '<Container App Managed Environment Name>'
$Environment = Get-AzContainerAppEnvironment -Name $EnvironmentName -ResourceGroupName $ResourceGroup
$EnvironmentId = $Environment.id

$customObjects = @([PSCustomObject]@{Name = "MANAGED_IDENTITY_CLIENT_ID"; Value = $IdentityClientId})
$Template = New-AzContainerAppJobTemplate -ContainerImage $ContainerImage -ContainerName $ContainerName -ContainerEnv $customObjects -verbose

$ContainerAppJob = New-AzContainerAppJobResource -Name $ContainerAppJobName -ResourceGroupName $ResourceGroup -EnvironmentId $EnvironmentId -ContainerTemplate $Template -Identity $IdentityResourceId -verbose
$ContainerAppJob.Data

Start Azure Container App Job

Start-AzContainerAppJobResource -Name "MyJob" -ResourceGroupName "MyGroup" -Verbose
$commands = @("pwsh","-c","whoami")
Start-AzContainerAppJobResource -Name "MyJob" -ResourceGroupName "MyGroup" -ContainerCommand $commands -Verbose
$envVariables = @([PSCustomObject]@{Name = "EMAIL_SENDER"; Value = "acccount@<domain>.com"}) 
$commands = @('pwsh','-c','write-host $env:EMAIL_SENDER')
Start-AzContainerAppJobResource -Name "MyJob" -ResourceGroupName "MyGroup" -ContainerCommand $commands -ContainerEnv $envVariables -Verbose

Get Job Execution History

Get-AzContainerAppJobExecution -JobName "MyJob" -ResourceGroupName "MyGroup" -verbose
Get-AzContainerAppJobExecution -JobName "MyJob" -ResourceGroupName "MyGroup" -ExecutionName "NewExecution" -verbose

Create Azure Container Instance (Private Access)

$IdentityResourceId = '<User-Assigned Managed Identity Resource Id>'
$ContainerGroupName = "ci-$(-join ((65..90) + (97..122) | Get-Random -Count 8 | ForEach-Object {[char]$_}))".ToLower()
$ResourceGroup = '<MyGroup>'
$ContainerRegistryServer = '<Container Registry Server>'
$ContainerImage = "$ContainerRegistryServer/<container-name>:<tag>"
$ContainerName = "$(-join ((65..90) + (97..122) | Get-Random -Count 5 | ForEach-Object {[char]$_}))".ToLower()
$SubnetId = "/subscriptions/<SubscriptionId>/resourceGroups/<MyRG>/providers/Microsoft.Network/virtualNetworks/<vnet-name>/subnets/<subnet-name>"

$ContainerInstancePorts = @(
    $ContainerInstancePort80 = New-AzContainerInstancePort -Port 80 -Protocol 'Tcp'
    $ContainerInstancePort443 = New-AzContainerInstancePort -Port 443 -Protocol 'Tcp'
    $ContainerInstancePort53 = New-AzContainerInstancePort -Port 53 -Protocol 'Udp'
)
$ContainerGroupPorts = @(
    $ContainerGroupPort80 = New-AzContainerGroupPort -Port 80 -Protocol 'Tcp'
    $ContainerGroupPort443 = New-AzContainerGroupPort -Port 443 -Protocol 'Tcp'
    $ContainerGroupPort53 = New-AzContainerGroupPort -Port 53 -Protocol 'Udp'
)
$Command = @("tail","-f","/dev/null")
$ContainerInstance = New-AzContainerInstanceContainer -Name $ContainerName -Image $ContainerImage -Ports $ContainerInstancePorts -Command $Command

$Registries = New-AzContainerGroupRegistryCredentials -Identity $IdentityResourceId -Server $ContainerRegistryServer

$ContainerGroup = New-AzContainerGroupResource -Name $ContainerGroupName -ResourceGroupName $ResourceGroup -ContainerInstance $ContainerInstance -ImageRegistryCredential $Registries -Ports $ContainerGroupPorts -IpAddressType "Private" -SubnetId $SubnetId -Identity $IdentityResourceId -verbose

$ContainerGroup.Data

Create Azure Container Instance (Public Access)

$IdentityResourceId = '<User-Assigned Managed Identity Resource Id>'
$ContainerGroupName = "ci-$(-join ((65..90) + (97..122) | Get-Random -Count 8 | ForEach-Object {[char]$_}))".ToLower()
$ResourceGroup = '<MyGroup>'
$ContainerRegistryServer = '<Container Registry Server>'
$ContainerImage = "$ContainerRegistryServer/<container-name>:<tag>"
$ContainerName = "$(-join ((65..90) + (97..122) | Get-Random -Count 5 | ForEach-Object {[char]$_}))".ToLower()
$SubnetId = "/subscriptions/<SubscriptionId>/resourceGroups/<MyRG>/providers/Microsoft.Network/virtualNetworks/<vnet-name>/subnets/<subnet-name>"

$ContainerInstancePorts = @(
    $ContainerInstancePort80 = New-AzContainerInstancePort -Port 80 -Protocol 'Tcp'
    $ContainerInstancePort443 = New-AzContainerInstancePort -Port 443 -Protocol 'Tcp'
    $ContainerInstancePort53 = New-AzContainerInstancePort -Port 53 -Protocol 'Udp'
)
$ContainerGroupPorts = @(
    $ContainerGroupPort80 = New-AzContainerGroupPort -Port 80 -Protocol 'Tcp'
    $ContainerGroupPort443 = New-AzContainerGroupPort -Port 443 -Protocol 'Tcp'
    $ContainerGroupPort53 = New-AzContainerGroupPort -Port 53 -Protocol 'Udp'
)
$Command = @("tail","-f","/dev/null")
$envVariables = @([PSCustomObject]@{Name = "SITE_FQDN"; Value = "<mydomain>[.]com"}) 
$ContainerInstance = New-AzContainerInstanceContainer -Name $ContainerName -Image $ContainerImage -Ports $ContainerInstancePorts -Command $Command -ContainerEnv $envVariables

$Registries = New-AzContainerGroupRegistryCredentials -Identity $IdentityResourceId -Server $ContainerRegistryServer

$ContainerGroup = New-AzContainerGroupResource -Name $ContainerGroupName -ResourceGroupName $ResourceGroup -ContainerInstance $ContainerInstance -ImageRegistryCredential $Registries -Ports $ContainerGroupPorts -IpAddressType "Public" -Identity $IdentityResourceId -verbose

$ContainerGroup.Data

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License.

References

pwshazcontainer's People

Contributors

cyb3rward0g avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

pwshazcontainer's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.