-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5407331
commit 71144c2
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.*' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |