Skip to content

Commit

Permalink
Azure Function PowerShell files
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthebrit committed May 9, 2020
1 parent 5407331 commit 71144c2
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
16 changes: 16 additions & 0 deletions FunctionResGraphPowerShell/host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "2.0",
"managedDependency": {
"Enabled": true
},
"logging": {
"logLevel": {
"default": "Trace"
},
"fileLoggingMode": "always"
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
20 changes: 20 additions & 0 deletions FunctionResGraphPowerShell/profile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Azure Functions profile.ps1
#
# This profile.ps1 will get executed every "cold start" of your Function App.
# "cold start" occurs when:
#
# * A Function App starts up for the very first time
# * A Function App starts up after being de-allocated due to inactivity
#
# You can define helper functions, run commands, or specify environment variables
# NOTE: any variables defined that are not environment variables will get reset after the first execution
# Authenticate with Azure PowerShell using MSI.
# Remove this if you are not planning on using MSI or Azure PowerShell.

if ($env:MSI_SECRET -and (Get-Module -ListAvailable Az.Accounts)) {
    Connect-AzAccount -Identity
}

# Uncomment the next line to enable legacy AzureRm alias in Azure PowerShell.
# Enable-AzureRmAlias
# You can also define functions or aliases that can be referenced in any of your PowerShell functions.
8 changes: 8 additions & 0 deletions FunctionResGraphPowerShell/requirements.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This file enables modules to be automatically managed by the Functions service.
# See https://aka.ms/functionsmanageddependency for additional information.
#
@{
# For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'.
'Az' = '3.*'
'Az.ResourceGraph' = '0.*'
}
54 changes: 54 additions & 0 deletions FunctionResGraphPowerShell/run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# Interact with query parameters or the body of the request.
$computername = $Request.Query.ComputerName
if (-not $computername) {
$computername = $Request.Body.ComputerName
}

Import-Module Az.ResourceGraph

$statusGood = $true

$GraphSearchQuery = "Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| where properties.osProfile.computerName =~ '$computername'
| join (ResourceContainers | where type=='microsoft.resources/subscriptions' | project SubName=name, subscriptionId) on subscriptionId
| project VMName = name, CompName = properties.osProfile.computerName, RGName = resourceGroup, SubName, SubID = subscriptionId"

try {
$VMresource = Search-AzGraph -Query $GraphSearchQuery
}
catch {
$statusGood = $false
Write-Error "Failure running Search-AzGraph, $_"
}

if($statusGood -and ($null -ne $VMresource))
{
$ValueObject= [PSCustomObject]@{"Status"="Success";"ComputerName"="$ComputerName";"VMName"="$($VMresource.VMName)";"ResourceGroup"="$($VMresource.RGName)";"Subscription"="$($VMResource.SubID)"}
$ValueJSON = ConvertTo-Json($ValueObject)
$status = [HttpStatusCode]::OK
}
elseif ($statusGood -and ($null -eq $VMresource))
{
$ValueJSON = "{`"Status`": `"Computer name not found`"}"
$status = [HttpStatusCode]::OK
}
else
{
$ValueJSON = "{`"Status`": `"Failed`"}"
$status = [HttpStatusCode]::BadRequest
}

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $status
Body = $ValueJSON
})

0 comments on commit 71144c2

Please sign in to comment.