azure-powershell-1/tools/AzureRM.Example.psm1

150 строки
5.2 KiB
PowerShell

#
# Script module for module '%MODULE-NAME%' that is executed when '%MODULE-NAME%' is imported in a PowerShell session.
#
# Generated by: Microsoft Corporation
#
# Generated on: %DATE%
#
$PSDefaultParameterValues.Clear()
Set-StrictMode -Version Latest
function Test-DotNet
{
try
{
if ((Get-PSDrive 'HKLM' -ErrorAction Ignore) -and (-not (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\' -ErrorAction Stop | Get-ItemPropertyValue -ErrorAction Stop -Name Release | Where-Object { $_ -ge 461808 })))
{
throw ".NET Framework versions lower than 4.7.2 are not supported in Az. Please upgrade to .NET Framework 4.7.2 or higher."
}
}
catch [System.Management.Automation.DriveNotFoundException]
{
Write-Verbose ".NET Framework version check failed."
}
}
function Preload-Assembly {
param (
[string]
$AssemblyDirectory
)
if($PSEdition -eq 'Desktop' -and (Test-Path $AssemblyDirectory -ErrorAction Ignore))
{
try
{
Get-ChildItem -ErrorAction Stop -Path $AssemblyDirectory -Filter "*.dll" | ForEach-Object {
try
{
Add-Type -Path $_.FullName -ErrorAction Ignore | Out-Null
}
catch {
Write-Verbose $_
}
}
}
catch {}
}
}
if (%ISAZMODULE% -and ($PSEdition -eq 'Desktop'))
{
if ($PSVersionTable.PSVersion -lt [Version]'5.1')
{
throw "PowerShell versions lower than 5.1 are not supported in Az. Please upgrade to PowerShell 5.1 or higher."
}
Test-DotNet
}
%AZURECOREPREREQUISITE%
if (Test-Path -Path "$PSScriptRoot\StartupScripts" -ErrorAction Ignore)
{
Get-ChildItem "$PSScriptRoot\StartupScripts" -ErrorAction Stop | ForEach-Object {
. $_.FullName
}
}
if (Get-Module %AZORAZURERM%.profile -ErrorAction Ignore)
{
Write-Warning ("%AZORAZURERM%.Profile already loaded. Az and AzureRM modules cannot be imported in the same session or used in the same script or runbook. If you are running PowerShell in an environment you control you can use the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. " +
"If you are running in Azure Automation, take care that none of your runbooks import both Az and AzureRM modules. More information can be found here: https://aka.ms/azps-migration-guide.")
throw ("%AZORAZURERM%.Profile already loaded. Az and AzureRM modules cannot be imported in the same session or used in the same script or runbook. If you are running PowerShell in an environment you control you can use the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. " +
"If you are running in Azure Automation, take care that none of your runbooks import both Az and AzureRM modules. More information can be found here: https://aka.ms/azps-migration-guide.")
}
$preloadPath = (Join-Path $PSScriptRoot -ChildPath "PreloadAssemblies")
Preload-Assembly -AssemblyDirectory $preloadPath
$preloadPath = (Join-Path $PSScriptRoot -ChildPath "ModuleAlcAssemblies")
Preload-Assembly -AssemblyDirectory $preloadPath
$netCorePath = (Join-Path $PSScriptRoot -ChildPath "NetCoreAssemblies")
if($PSEdition -eq 'Core' -and (Test-Path $netCorePath -ErrorAction Ignore))
{
try
{
$loadedAssemblies = ([System.AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object {New-Object -TypeName System.Reflection.AssemblyName -ArgumentList $_.FullName} )
Get-ChildItem -ErrorAction Stop -Path $netCorePath -Filter "*.dll" | ForEach-Object {
$assemblyName = ([System.Reflection.AssemblyName]::GetAssemblyName($_.FullName))
$matches = ($loadedAssemblies | Where-Object {$_.Name -eq $assemblyName.Name})
if (-not $matches)
{
try
{
Add-Type -Path $_.FullName -ErrorAction Ignore | Out-Null
}
catch {
Write-Verbose $_
}
}
}
}
catch {}
}
%IMPORTED-DEPENDENCIES%
if (Test-Path -Path "$PSScriptRoot\PostImportScripts" -ErrorAction Ignore)
{
Get-ChildItem "$PSScriptRoot\PostImportScripts" -ErrorAction Stop | ForEach-Object {
. $_.FullName
}
}
$FilteredCommands = %DEFAULTRGCOMMANDS%
if ($Env:ACC_CLOUD -eq $null)
{
$FilteredCommands | ForEach-Object {
$existingDefault = $false
foreach ($key in $global:PSDefaultParameterValues.Keys)
{
if ($_ -like "$key")
{
$existingDefault = $true
}
}
if (!$existingDefault)
{
$global:PSDefaultParameterValues.Add($_,
{
if ((Get-Command Get-AzContext -ErrorAction Ignore) -eq $null)
{
$context = Get-AzureRmContext
}
else
{
$context = Get-AzContext
}
if (($context -ne $null) -and $context.ExtendedProperties.ContainsKey("Default Resource Group")) {
$context.ExtendedProperties["Default Resource Group"]
}
})
}
}
}