-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetComputerListFromOuToFile.ps1
71 lines (63 loc) · 2.25 KB
/
getComputerListFromOuToFile.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
#
# Francesco Buresta UniUrb 2021
#
# Get AD Computer from choosen OU then output into file.
#
$isDebug = $false
$ou = @()
$i = 1
Get-ADOrganizationalUnit -Filter 'Name -like "*" -and Name -notlike "OU_*"' | Sort-Object -Property Name | Foreach {
$item = [PSCustomObject]@{
id = $i
name = $_.Name.Replace(" ","_")
dn = $_.DistinguishedName.Replace(" ","_")
}
$ou += $item
$i++
}
function Get-InitialPrompt {
param (
[Array]$ou
)
Clear-Host
For ($i = 0; $i -lt $ou.Count; $i++){
Write-Host "$($ou[$i].id): $($ou[$i].name)"
}
Write-Host "X: Exit"
Write-Host ""
Write-Host "[ ctrl+C ] : Exit immediately"
Write-Host ""
}
$sharedFolder = "C:\shared_choco"
do {
Get-InitialPrompt($ou)
$opt = Read-Host 'Choose an option'
if($opt -ne 'X'){
$sel = $opt-1
Write-Host "Querying ... $($ou[$sel].dn)"
$pattern = $($ou[$sel].dn).Replace("OU=", "#")
$pattern = $pattern.Replace("DC=example,DC=org", "")
$pattern = $pattern.Replace(",", "")
$arrExploded = $pattern.Split("#")
[Array]::Reverse($arrExploded)
$pathToCreate = $arrExploded -join '\'
$fullPath = $("$sharedFolder\$pathToCreate")
if(-not(Test-Path -Path $fullPath)) {
Write-Host "[ INFO ] Making folder path" : $fullPath
if($isDebug) {
New-Item -ItemType Directory -Path $fullPath -Force -Confirm:$false -WhatIf
}else{
New-Item -ItemType Directory -Path $fullPath -Force -Confirm:$false -ErrorAction Ignore
}
}
if(Test-Path -Path $fullPath) {
$filePath = "$fullPath\list-$($ou[$sel].name).csv"
Write-Host "OU" : $($ou[$sel].name)
Write-Host "Output file" : $filePath
Get-ADComputer -SearchBase $($ou[$sel].dn) -Filter 'operatingsystem -notlike "*server*" -and enabled -eq "true"' -Properties Name,Operatingsystem,OperatingSystemVersion,IPv4Address | Select-Object -Property Name,IPv4Address | ConvertTo-Csv -NoTypeInformation | % { $_ -replace '"', ""} | out-file $filePath -fo -en utf8
}else{
Write-Output "[ ERR ] Failed to create folder path"
}
}
pause
} until ($opt -eq 'X')