SCCM CMPivot Powershell ile Local Admin Raporu
Herhangi bir collection’da yer alan clientlardaki local adminleri bulmak için SCCM üzerinden CMPivot aracı kullanılabiliyor. Bunun için collection’a sağ tıklayıp CMPivot seçeneğini seçtiğinizde query kısmına uygun sorguyu yazmanız gerekiyor. Örneğin Domain Adminler hariç bütün admin hesaplarının listesi için şu sorgu gerekiyor.
1 2 3 |
Administrators | where Name !contains 'Domain Admins' |
Bu işlemi daha kolay ve otomatik hale getirmek için CMpivot’u powershell rest api aracılığıyla çağırıp kullanabiliyoruz. Yukarıdaki kodun powershell örneği aşağıda yer almaktadır. Siz de kendinize göre değişiklik yapabilirsiniz.
SCCMSERVER kısmına kendi sccm server adınızı yazacaksınız
HAS00074 kısmı collection ID. Burayı işlem yapmak istediğiniz collection ID ile değiştireceksiniz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#Sertifika problemini gidermek için kullanılan kod parçası add-type @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy #SCCM_CMPIVOT_Query $Body = @{"InputQuery" = "Administrators | where Name !contains 'Domain Admins'"} | ConvertTo-Json #Query'i çalıştıracağınız collection seçimi $DDD = Invoke-RestMethod -Method Post -Uri "https://<em>SCCMSERVER</em>/AdminService/v1.0/Collections('<em>HAS00074</em>')/AdminService.RunCMPivot" -UseDefaultCredentials -Body $body -ContentType "application/json" #CMPivot taraması belirli bir zaman istiyor o yüzden burda bir zaman tanımı yapmamız gerekiyor. 500 sunucu için yaklaşık olarak 120 saniyelik bir süre yeterli oluyor. $durationInSeconds = 120 $counter = 0 while ($counter -lt $durationInSeconds) { Write-Host "Scanning running - Elapsed time: $counter seconds" -ForegroundColor Green Start-Sleep -Seconds 1 $counter++ } Write-Host "Tarama tamamlandı." #GetJobIDfrom_CMPivot $Operid = $DDD.OperationId $url = 'https://SCCMSERVER/AdminService/v1.0//SMS_CMPivotStatus?$filter=ClientOperationId eq ' + $Operid $ResOfOperID = Invoke-RestMethod -Method Get -Uri $url -UseDefaultCredentials -ContentType "application/json" $ResultOfQuery = $ResOfOperID.value | select DeviceName,ScriptOutput $Result = @( foreach ($g in $ResultOfQuery ) { [pscustomobject] @{ "Name" = $g.DeviceName "ADObject" = $g.ScriptOutput -replace 'PrincipalSource="ActiveDirectory" /><e _i="0"', '' -replace '<result ResultCode="0" moreResults="False"><e _i="0"', '' -replace 'PrincipalSource="ActiveDirectory" /></result>','' -replace ' <result ResultCode="0" moreResults="False"><e _i="0"','' -replace 'ObjectClass="', "`nObjectClass=""" -replace ' /><e _i="0"','' -replace '/></result>','' -replace 'ObjectClass=','' -replace "Group",'Group:' -replace "Name",'' -replace '"','' -replace 'User','User:' -replace ' =','' -replace ' PrincipalSource=Local ' ,':"Local"' }} ) $LocalAdmin = $Result $uniqueLocalAdmin = $LocalAdmin | Group-Object Name | ForEach-Object { $_.Group | Select-Object -First 1 } $FinalReport = $uniqueLocalAdmin # Create an array to hold the structured data $structuredData = @() # Iterate through each item in $FinalReport foreach ($item in $FinalReport) { $structuredItem = [PSCustomObject]@{ Name = $item.Name ADObject = $item.ADObject GroupMembers = $item.Group -replace "Group:", "" -join ", " UserMembers = $item.User -replace "User:", "" -join ", " } $structuredData += $structuredItem } # Raporu csv olarak export ediyoruz $structuredData | Export-Csv -Path "C:\LocalAdminListesi.csv" -NoTypeInformation -Encoding UTF8 |
Kaynak: https://www.linkedin.com/pulse/sccm-restapi-cmpivot-powershell-local-admin-report-meir-peleg/