Blue Flower

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

#Variables
$date = Get-Date -Format "yyyyMMdd"


function Create-Folder ($path)
{
If(!(test-path $path))
{
New-Item -ItemType Directory -Force -Path $path

}
return $path;
}

function Connect-SCCM
{
# Site configuration
$SiteCode = "XXX" # Site code
$ProviderMachineName = "<SCCM PSS Server>" # SMS Provider machine name

# Customizations
$initParams = @{}
#$initParams.Add("Verbose", $true) # Uncomment this line to enable verbose logging
#$initParams.Add("ErrorAction", "Stop") # Uncomment this line to stop the script on any errors

# Do not change anything below this line

# Import the ConfigurationManager.psd1 module
if((Get-Module ConfigurationManager) -eq $null) {
Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams
}

# Connect to the site's drive if it is not already present
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}

# Set the current location to be the site code.
Set-Location "$($SiteCode):\" @initParams
}

Function Get-ADMembers($Groupname)
{
$OutFile = $OutputFolder.Fullname + "\MGX_All_Users_" + $date + ".csv"
$ADGroup2 = Get-ADGroup $Groupname -Properties Member -server Prod.Mgnx.Cloud
$Arrayofmembers = Get-ADGroupMember -identity $ADGroup2 -Server Prod.Mgnx.Cloud -recursive

foreach ($Member in $Arrayofmembers) {
$SAM = $Member.SamAccountName
$User = Get-ADUser -LDAPFilter "(&(objectCategory=User)(memberof=$ADGroup2)((sAMAccountName=$SAM)))" -server Prod.Mgnx.Cloud -Properties * | Select-Object -Property Name,SamAccountName,EmailAddress,physicalDeliveryOfficeName | Export-CSV -Path $OutFile -append -force
}
}

function Get-SCCMMembers ($collname)
{
$OutFile = $OutputFolder.Fullname + "\MGX_All_EUDs_" + $date + ".csv"
$Results = @()
$Arrayofmembers = get-cmcollectionmember -collectionname $collname
foreach ($Member in $Arrayofmembers) {
$Name = $Member.Name
$Computer = Get-ADComputer -LDAPFilter "(&(objectCategory=Computer)((Name=$Name)))" -Properties * -server Prod.Mgnx.Cloud |Select-Object -Property Name,WhenCreated
$properties=@{
Name = $Member.Name
Site = $Member.ADSiteName
Create = $Computer.WhenCreated
}
$Results+=New-Object psobject -Property $properties
}
$Results | Select-Object Name,Site,Create | Export-CSV -Path $Outfile -append -force
}

# Variables
$OutputFolder = Create-Folder "C:\temp\$date"

pause
# Main Script

cls
Get-ADMembers e.g USR-PROD-Users
Connect-SCCM
e.g. Get-SCCMMembers "All EUDS with SCCM Client "