Overblog
Editer l'article Suivre ce blog Administration + Créer mon blog

GetDPGroupsContent.ps1

Publié le par damcuvelier

# .\GetDPGroupsContent.ps1 -Application

    param(
        [Alias("Name")]
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [String[]]$DistributionPointGroup,

        [Parameter()]
        [Switch]$Package,

        [Parameter()]
        [Switch]$DriverPackage,
        
        [Parameter()]
        [Switch]$DeploymentPackage,
        
        [Parameter()]
        [Switch]$OperatingSystemImage,
        
        [Parameter()]
        [Switch]$OperatingSystemInstaller,
        
        [Parameter()]
        [Switch]$BootImage,
        
        [Parameter()]
        [Switch]$Application,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [String]$SiteServer = $CMSiteServer,
        
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [String]$MP,
        
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [String]$SiteCode = $CMSiteCode
    )

#region [Init]
$erroractionpreference = 'silentlycontinue'
$here = $PSScriptRoot
if(!$here){$here = (Get-Location).path}

import-module "$here\readparams.psm1" -force | Out-Null        
#endregion [Init]

#region [Function]

function scriptlog{
    param($val)
write-host $val
}

function ecrireQry($SwitchArgs){
$resqry = "`$query AND ( "

foreach($SwitchArg in $SwitchArgs.split(';')){
    $Val = "ObjectType = '{0}'" -f "[Int][SMS_DPContentInfo]$SwitchArg"
    # $val = "ObjectType = '{0}'" -f [Int][SMS_DPContentInfo]"$SwitchArg"
    $resqry += " OR $Val"
}

$resqry += " )"
return $resqry
}

function Get-MPHostname($Site){
# Déterminer le FQDN du MP à partir du serveur de site SCCM
    # Obtenir le site SCCM local à partir de WMI
    # $Site = Get-WmiObject -Namespace "root\sms" -Class "SMS_ProviderLocation" | Where-Object { $_.ProviderForLocalSite -eq $true }

    # Vérifier si le site SCCM est trouvé
    if ($Site) {
        # Obtenir le FQDN du MP à partir du site SCCM
        $MP = Get-WmiObject -Namespace "root\sms\site_$($Site.SiteCode)" -Class "SMS_ProxyMP" -Filter "RoleName='SMSMP'"

        # Vérifier si le MP est trouvé
        if ($MP) {
            return $MP.ServerName
        } else {
            Write-Error "Le serveur de gestion (MP) n'a pas été trouvé."
        }
    } else {
        Write-Error "Le site SCCM local n'a pas été trouvé."
    }


# Appeler la fonction pour obtenir le FQDN du MP
$MPFQDN = Get-MPHostname

# Afficher le FQDN du MP
if ($MPFQDN) { return $MPFQDN }
}

function Connex($MP){
# 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é
[string]$SCCMDrive = (Get-PSDrive | Where-Object { $_.Root -match $MP }).Name
if(!$SCCMDrive){$SCCMDrive = (Get-PSdrive | where-object{$_.Provider -eq "CMSite"}).Name}

$DriveCM = $SCCMDrive.replace('\','')
$DriveCM = "$DriveCM`:"
$DriveCM = $DriveCM.replace('::',':')

return $DriveCM
}

#endregion [Function]

#region [Get-Variables]
set-location $here
$Global:CMDrive = Connex($MP)
[string]$SiteCode = $CMDrive.split(':')[0]

# if(!$SiteCode){[string]$SiteCode = Get-WmiObject -Namespace "root\sms" -Class "SMS_ProviderLocation" | Where-Object { $_.ProviderForLocalSite -eq $true }}
# if(!$MP){[string]$MP = Get-MPHostname($SiteCode)}
# if(!$SiteCode){
# [string]$Global:CMDrive = Connex($MP)
# [string]$SiteCode = $CMDrive.split(':')[0]
# }

if(!$SiteServer){[string]$SiteServer = (Get-PSdrive -Name $SiteCode | where-object{$_.Provider -eq "CMSite"}).Root}

$EnumParams = $PSBoundParameters.GetEnumerator()
$PassArgs = getparams($EnumParams)

$Global:SwitchArgs = $PassArgs.SwitchParams
$Global:ValArgs = $PassArgs.ValParams

[string]$resDPContentInfo = ecrireQry($SwitchArgs)

$Namespace = "ROOT/SMS/Site_{0}" -f $SiteCode

scriptlog -val "CMDrive = $CMDrive"
scriptlog -val "SiteCode = $SiteCode"
scriptlog -val "MP = $MP"
scriptlog -val "SiteServer = $SiteServer"
scriptlog -val "Namespace = Namespace"
scriptlog -val "SwitchArgs = {$SwitchArgs}"
scriptlog -val "ValArgs = {$ValArgs}"
#endregion [Get-Variables]

#region [Set-PSCMContentMgmt]
set-location $CMDrive
foreach ($TargetDPGroup in $DistributionPointGroup.Name) {

$Query = "SELECT * FROM SMS_DPGroupContentInfo WHERE SMS_DPGroupContentInfo.GroupID in ( SELECT SMS_DPGroupInfo.GroupID FROM SMS_DPGroupInfo WHERE Name = '{0}')" -f $TargetDPGroup

$Qry = "{0} AND ( {1} )" -f $Query, $resDPContentInfo

if($Application){
$ModelName = $_.ObjectID -SiteServer $SiteServer -SiteCode $SiteCode
$ObjectIDQry = "SELECT CI_ID FROM SMS_ApplicationLatest WHERE ModelName = '{0}'" -f $ModelName
$AppObjectID = (Get-CimInstance -ComputerName $SiteServer -Namespace $Namespace -Query $ObjectIDQry).CI_ID
}

    Get-CimInstance -ComputerName $SiteServer -Namespace $Namespace -Query $Qry | ForEach-Object {
    [PSCustomObject]@{
    PSTypeName             = "PSCMContentMgmt"
    ObjectName             = $_.Name
    Description            = $_.Description
    ObjectType             = ([SMS_DPContentInfo]$_.ObjectType).ToString()
    ObjectID               = $(if ($_.ObjectType -eq [SMS_DPContentInfo]"Application") { $AppObjectID } else { $_.ObjectID })
    SourceSize             = $_.SourceSize
    DistributionPointGroup = $TargetDPGroup
    }
    }
}

#endregion [Set-PSCMContentMgmt]


 

Commenter cet article