-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path1_ADMU_Collect.ps1
89 lines (81 loc) · 3.43 KB
/
1_ADMU_Collect.ps1
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Github vars
$GHUsername = ''
$GHToken = '' # https://github.com/settings/tokens needs token to write/create repo
$GHRepoName = 'Jumpcloud-ADMU-Discovery'
$password = ConvertTo-SecureString "$GHToken" -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($GHUsername, $password)
# Install PowerShellForGitHub
if ($null -eq (Get-InstalledModule -Name "PowerShellForGitHub" -ErrorAction SilentlyContinue)) {
Install-Module PowerShellForGitHub -Force
}
# If system is Mac, save to home directory
If ($IsMacOS) {
$tempDir = '~/'
$newjsonoutputdir = $tempDir + '/' + $env:COMPUTERNAME + '.json'
$workingdir = $tempDir + '\jumpcloud-discovery'
$discoverycsvlocation = $workingdir + '\jcdiscovery.csv'
} elseif ($IsWindows) {
# If system is Windows and running Powershell 7.x.xxx, save to %TEMP%\Jumpcloud-discovery
$windowstemp = [System.Environment]::GetEnvironmentVariable('TEMP', 'Machine')
$newjsonoutputdir = $windowstemp + '\' + $env:COMPUTERNAME + '.json'
$workingdir = $windowstemp + '\jumpcloud-discovery'
$discoverycsvlocation = $workingdir + '\jcdiscovery.csv'
} else {
# Assume PowerShell 5.1.xxx
$windowstemp = [System.Environment]::GetEnvironmentVariable('TEMP', 'Machine')
$newjsonoutputdir = $windowstemp + '\' + $env:COMPUTERNAME + '.json'
$workingdir = $windowstemp + '\jumpcloud-discovery'
$discoverycsvlocation = $workingdir + '\jcdiscovery.csv'
}
function Get-ADMUSystemsForMigration {
[CmdletBinding()]
param (
[Parameter()]
[system.string]
$systemID
)
begin {
If ('systemID' -in $PSBoundParameters) {
$systems = Get-JCSystem -SystemID $systemID
} else {
$systems = Get-JCSystem -os windows
}
$list = New-Object System.Collections.ArrayList
}
process {
foreach ($system in $systems) {
$users = Get-JCsdkSystemInsightUser -Filter @("system_id:eq:$($system.id)")
# get the administrator account:
$adminUser = $users | Where-Object { $_.uid -eq 500 }
$machineSID = ($adminUser.uuid -split "-")[0..6] -join "-"
$adUsers = $users | Where-Object { ($_.uuid -notmatch $machineSID) -AND ($_.RealUser -eq $true) }
$adUsers | ForEach-Object {
$list.Add(
[PSCustomObject]@{
SID = $_.Uuid
LocalPath = $_.Directory
LocalComputerName = $system.hostname
LocalUsername = if (-NOT [system.string]::IsNullOrEmpty($_.Username)) { $_.Username } else {
$_.Uuid
}
JumpCloudUserName = $null
}
) | Out-Null
}
}
}
end {
return $list
}
}
$allUsers = Get-ADMUSystemsForMigration
if (-NOT (Test-Path -Path $workingdir)) {
New-Item -ItemType Directory -Force -Path $workingdir | Out-Null
}
$combinedjson = $AllUsers | ConvertTo-Csv -NoTypeInformation | Out-File $discoverycsvlocation
# Auth to github
Set-GitHubAuthentication -Credential $cred
# set content string
$discoverycsvContent = (get-content -Path $discoverycsvlocation -Raw)
# upload to github
Set-GitHubContent -OwnerName $GHUsername -RepositoryName $GHRepoName -BranchName 'main' -Path "jcdiscovery.csv" -CommitMessage "CSV Upload $(Get-Date)" -Content $discoverycsvContent