# Module de recensement des applications SCCM obsolètes + poids sur les DPs + emprunte Carbone
function connexCM{
param($PM}
$ici = (get-location).path
$COMPUTERNAME = $env:COMPUTERNAME
$USERDOMAIN = $env:USERDOMAIN
# Importer le module SCCM
Import-Module "$($Env:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1"
# Récupérer le nom du lecteur pour le chemin spécifié
if($PM){[string]$SCCMDrive = (Get-PSDrive | Where-Object {(($_.PSProvider -match "CMSite") -and ($_.Root -match $PM))}).Name}
else{[string]$SCCMDrive = (Get-PSDrive | Where-Object { $_.PSProvider -match "CMSite" }).Name}
if(!$SCCMDrive){
$PM = "$COMPUTERNAME.$USERDOMAIN"
[string]$SCCMDrive = (Get-PSDrive | Where-Object { $_.Root -match $PM }).Name
}
if(!$SCCMDrive){[string]$SCCMDrive = (Get-PSDrive | Where-Object { $_.PSProvider -match "CMSite" }).Name}
# if(!$SCCMDrive){[string]$SCCMDrive = (Get-PSDrive | Where-Object { $_.Root -match $PM }).Name}
$SCCMDrive = $SCCMDrive.replace('\','')
$SCCMDrive = "$SCCMDrive`:"
$SCCMDrive = $SCCMDrive.replace('::',':')
set-location $ici
return $SCCMDrive
}
function calculemprunteCarbone {
# source : https://blog.antimuonium.com/blog-post/l-empreinte-carbone-du-stockage-des-donnees
param([int]$Mo)
# Définir la constante de conversion
$conversion = 0.176
# Calculer le résultat
$resultat = $conversion * $Mo
return $resultat
}
function GetApplication($AppName){
$ici = (get-location).path
set-location $CMDrive
$result = Get-CMApplication -name $AppName
set-location $ici
return $result
}
function GetlastDeploy($AppName){
$ici = (get-location).path
set-location $CMDrive
$DernierDeploiement = Get-CMApplicationDeployment -Name $AppName | ForEach-Object | Sort-Object LastDeploymentTime -Descending | Select-Object -First 1
set-location $ici
return $DernierDeploiement
}
function GetSizeMo($AppName){
$Application = GetApplication($AppName)
$TailleMo = $Application.ContentSize / 1MB
$ArrTailleMo = ($TailleMo.ToString("N2"))
return $ArrTailleMo
}
function GetDPs($app){
$ici = (get-location).path
set-location $CMDrive
$ServerNames = $null
# Obtenir l'ID de l'application
$IDApplication = $app.Id
# Obtenir les points de distribution où le contenu de l'application est distribué
$Distributions = Get-CMDistributionPointContent -ApplicationId $IDApplication
# Parcourir les points de distribution
foreach ($Distribution in $Distributions) {
$DPTarget = Get-CMDistributionPoint -Id $Distribution.TargetId
$ServerName = $DPTarget.ServerName
$ServerNames += "$ServerName;"
}
$ServerNames = $ServerNames.trimend(';`
')
set-location $ici
return $ServerNames
}
function GetApplicationDeployment($AppName){
$ici = (get-location).path
set-location $CMDrive
$result = (Get-CMApplicationDeployment -Name $AppName) -join ';'
set-location $ici
return $result
}
function GetApplicationCollectionName($AppName){
$ici = (get-location).path
set-location $CMDrive
$GetApplicationDeployment = GetApplicationDeployment($AppName)
$result = ($GetApplicationDeployment).CollectionName -join ';'
set-location $ici
return $result
}
function GetSupersedences($AppName){
$ici = (get-location).path
set-location $CMDrive
[System.Collections.ArrayList]$CMDeploymentTypeSupersedences = @()
$CMDeploymentTypes = Get-CMDeploymentType -ApplicationName $AppName
foreach($CMDeploymentType in $CMDeploymentTypes){
$Supersedence = Get-CMDeploymentTypeSupersedence -InputObject $CMDeploymentType
$ApplicationName = $Supersedence.ApplicationName
if($ApplicationName){
$val = [pscustomobject]@{'AppName'=$AppName;`
'SupersedApp'=$ApplicationName}; $CMDeploymentTypeSupersedences.add($val) | Out-Null; $val=$null
}
}
set-location $ici
return $CMDeploymentTypeSupersedences
}
function GetCMAppObso{
param($AppName,$PM,[int]$retention)
$Global:erroractionpreference = 'silentlycontinue'
$Global:warningactionpreference = 'silentlycontinue'
$thisfldr = $PSScriptRoot
if(!$thisfldr){$thisfldr = (get-location).path}
$global:here = $thisfldr
if($PM){$global:CMDrive = connexCM -PM $PM}else{$global:CMDrive = connexCM}
if(!$retention){$retention = 6}
$date = (get-date)
$limitretention = $date.AddMonths(-$retention)
$appDeployments = $null
$appCollectionNames = $null
$appSupersedences = $null
$lastDeploy = $null
$app = GetApplication($AppName)
$appDeployments = GetApplicationDeployment($AppName)
$appCollectionNames = GetApplicationCollectionName($AppName)
$appSupersedences = GetSupersedences($AppName)
$lastDeploy = GetlastDeploy($AppName)
$GetDPNames = GetDPs($app)
[System.Collections.ArrayList]$FlagOBSO = @()
if(!$appCollectionNames){ # = si(deployment = 0):
if($appSupersedences){
foreach($appSupersedence in $appSupersedences){
$SupersedApp = $appSupersedence.SupersedApp
$SupersedApps += "$SupersedApp;"
}
$SupersedApps = $SupersedApps.trimend(';')
$Reason = "SupersedApps: $SupersedApps"
$CMAppObsoTag = "OBSO-Supersedences"
} # si superseded
if($lastDeploy -lt $limitretention){
$Reason = "retention > $retention Months"
$CMAppObsoTag = "OBSO-limitage"
} # si retention > $retention
}
# Taille Mo de $AppName
[int]$SizeMo = GetSizeMo($AppName)
# calcul poids Mo:
[int]$GetDPNum = $GetDPNames.split(';').count
[int]$GetFullSize = $GetDPNum * $SizeMo
# calcul cout AppOsbo:
[int]$Price = GetPriceMo($SizeMo)
[int]$PricebyDP = $Price
[int]$FullPrice = $GetDPNum * $PricebyDP
# calculs poids gC02:
[int]$GetgC02SizebyDP = calculemprunteCarbone -Mo $SizeMo
[int]$GetgC02FullSize = calculemprunteCarbone -Mo $GetFullSize
# export resultats:
$val = [pscustomobject]@{`
'AppName'=$AppName;`
'TypeObso'=$CMAppObsoTag;`
'Reason'=$Reason;`
'DPs'=$GetDPNames;`
'AppSize'=$SizeMo;`
'AppFullSize'=$GetFullSize;`
'PricebyDP'=$PricebyDP;`
'PriceFull'=$FullPrice;`
'EmpgCO2byDP'=$GetgC02SizebyDP;`
'EmpgCO2byFull'=$GetgC02FullSize`
}
$FlagOBSO.add($val) | Out-Null
$val=$null
Export-Csv -InputObject $FlagOBSO -Path "$here\Obso.csv" -Append -Delimiter ';' -Encoding ASCII -NoTypeInformation -Force
return "$here\Obso.csv"
}
Commenter cet article