-
Notifications
You must be signed in to change notification settings - Fork 3
/
VMsEnumerate.ps1
95 lines (89 loc) · 3.69 KB
/
VMsEnumerate.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
90
91
92
93
94
95
<#
.Synopsis
.Description
.Example
.\VMsEnumerate.ps1 -SrvPrefix 'ServersA' -OutDir '.'
#>
param(
[string]$SrvPrefix,
[string]$OutDir = '.'
)
while ($SrvPrefix -eq '') {
Write-Host "Missing servers name prefix to search for.`nPlease provide prefix w/o wildcards." -ForegroundColor Cyan
$SrvPrefix = Read-Host
}
$outputCSV = Join-Path -Path $OutDir -ChildPath "Comps$SrvPrefix.csv"
$nonTrustedDomains = 'otherdomain.com'
$credsadm = try {
Import-CliXml -Path "$($env:USERPROFILE)\adm.cred"
} catch {
Get-Credential
}
$ErrorActionPreference = 'SilentlyContinue'
function Get-FQND {
param (
[string]$CompName,
[array]$Domains = $nonTrustedDomains
)
try {
$ipAddress = Resolve-DnsName $compName | Select-Object -ExpandProperty IPAddress
$dName = Resolve-DnsName $ipAddress | Where-Object {$_.NameHost -notlike 'vtemp*'} | Select-Object -First 1 -ExpandProperty NameHost
$fqdn = New-Object PSObject -Property @{DomainName = $dName; IsValidated = $true}
} catch {
foreach ($domain in $Domains) {
$dName = $compName, $domain -join '.'
if(Test-Connection $dName -Quiet) {
$fqdn = New-Object PSObject -Property @{DomainName = $dName; IsValidated = $true}
} else {
$fqdn = New-Object PSObject -Property @{DomainName = $compName; IsValidated = $false}
}
}
}
Write-Output $fqdn
}
$vmHostsFilter = "$SrvPrefix*"
# Get cluster nodes
$vmHosts = Get-ADComputer -Filter {Name -like $vmHostsFilter}
$VMs = @()
foreach ($vmHost in $vmHosts.Name) {
$VMs += Invoke-Command -ComputerName $vmHost -Credential $credsadm –ScriptBlock {Get-VM | Select-Object -Property ComputerName, Name, State, IsClustered, CreationTime}
}
$vmDomain =@()
foreach ($vm in $VMs) {
$domainName = Get-FQND -CompName $vm.Name -Domains $nonTrustedDomains
if($domainName.IsValidated) {
$compRole = Invoke-Command -ComputerName $vm.Name -Credential $credsadm -ScriptBlock {
$envComp = $env:ABCDATA_ENVIRONMENT_NAME
$isSQL = if (Get-Item 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL' -ErrorAction SilentlyContinue) {
Write-Output $true
} else {
Write-Output $false
}
$isOLAP = if (Get-Item 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\OLAP' -ErrorAction SilentlyContinue) {
Write-Output $true
} else {
Write-Output $false
}
$isIIS = if (Get-Item 'HKLM:\SOFTWARE\Microsoft\InetStp' -ErrorAction SilentlyContinue) {
Write-Output $true
} else {
Write-Output $false
}
$a = New-Object PSObject -Property @{Environment = $envComp; IsSQL = $isSQL; IsOLAP = $isOLAP; IsIIS = $isIIS}
Write-Output $a
}
} else {
$compRole = New-Object PSObject -Property @{Environment = $null; IsSQL = $null; IsOLAP = $null; IsIIS = $null}
}
$vmDomain += $vm | Select-Object -Property ComputerName, Name,
@{Name = 'DomainName'; Expression = {$domainName.DomainName}},
@{Name = 'IsValidated'; Expression = {$domainName.IsValidated}},
@{Name = 'Env'; Expression = {$compRole.Environment}},
@{Name = 'IsSQL'; Expression = {$compRole.IsSQL}},
@{Name = 'IsOLAP'; Expression = {$compRole.IsOLAP}},
@{Name = 'IsIIS'; Expression = {$compRole.IsIIS}},
State, IsClustered, CreationTime
}
$vmDomain | Select-Object -Property * | Export-Csv $outputCSV -NoTypeInformation
Write-Host "`nResults have been saved to file:" -ForegroundColor Yellow
Write-Output (Resolve-Path $outputCSV).Path''