Skip to content

Commit

Permalink
Add agent guids to functions (#27)
Browse files Browse the repository at this point in the history
* Added multiple parameter sets to allow searching by AgentGuid's as well

* Added better foreach looping so that we can search for multiple things at once, regardless of parameter set.

* Forgot a *

* Fixed parameterset name
  • Loading branch information
marranaga authored Aug 30, 2019
1 parent 8e591be commit 1d3ede3
Show file tree
Hide file tree
Showing 5 changed files with 382 additions and 167 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@{
Parameters = @{
ComputerName = "Computer"
ComputerName = "Computer*"
ForceWildcardHandling = $True
}
Output = @{
Expand Down
178 changes: 121 additions & 57 deletions ePOwerShell/Public/Get-ePOComputer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -124,87 +124,147 @@ function Get-ePOComputer {
try {
switch ($PSCmdlet.ParameterSetName) {
"ComputerName" {
$Request.Query.searchText = $Computer
foreach ($Comp in $Computer) {
Write-Debug ('Searching by computer name for: {0}' -f $Comp)
if ($Comp -is [ePOComputer]) {
Write-Verbose 'Using ePOComputer object'
Write-Output $Comp
} else {
if ($ForceWildcardHandling) {
if (-not ($script:AllePOComputers)) {
$Request.Query.searchText = ''
$script:AllePOComputers = Invoke-ePORequest @Request
}

foreach ($ePOComputer in $script:AllePOComputers) {
if ($ePOComputer.'EPOComputerProperties.ComputerName' -like $Comp) {
$ePOComputerObject = ConvertTo-ePOComputer $ePOComputer
Write-Output $ePOComputerObject
}
}
} else {
$Request.Query.searchText = $Comp
$ePOComputers = Invoke-ePORequest @Request

foreach ($ePOComputer in $ePOComputers) {
if ($ePOComputer.'EPOComputerProperties.ComputerName' -eq $Comp) {
$ePOComputerObject = ConvertTo-ePOComputer $ePOComputer
Write-Output $ePOComputerObject
}
}
}
}
}
}

"MACAddress" {
$MACAddress = $MACAddress.ToUpper()

switch -Regex ($MACAddress) {
'^([0-9a-f]{2}:){5}([0-9a-f]{2})$' {
Write-Verbose 'Delimiter: Colons'
$MACAddress = $MACAddress.Replace(':', '')
break
}

'^([0-9a-f]{2}-){5}([0-9a-f]{2})$' {
Write-Verbose 'Delimiter: Dashs'
$MACAddress = $MACAddress.Replace('-', '')
break
}

'^([0-9a-f]{2}\.){5}([0-9a-f]{2})$' {
Write-Verbose 'Delimiter: Periods'
$MACAddress = $MACAddress.Replace('.', '')
break
}

'^([0-9a-f]{2}\s){5}([0-9a-f]{2})$' {
Write-Verbose 'Delimiter: Spaces'
$MACAddress = $MACAddress.Replace(' ', '')
break
foreach ($Address in $MACAddress) {
$Address = $Address.ToUpper()

switch -Regex ($Address) {
'^([0-9a-f]{2}:){5}([0-9a-f]{2})$' {
Write-Verbose 'Delimiter: Colons'
$Address = $Address.Replace(':', '')
break
}

'^([0-9a-f]{2}-){5}([0-9a-f]{2})$' {
Write-Verbose 'Delimiter: Dashs'
$Address = $Address.Replace('-', '')
break
}

'^([0-9a-f]{2}\.){5}([0-9a-f]{2})$' {
Write-Verbose 'Delimiter: Periods'
$Address = $Address.Replace('.', '')
break
}

'^([0-9a-f]{2}\s){5}([0-9a-f]{2})$' {
Write-Verbose 'Delimiter: Spaces'
$Address = $Address.Replace(' ', '')
break
}

'^([0-9a-f]{12})$' {
Write-Verbose 'Delimiter: None'
break
}

default {
Write-Error ('MAC Address does not match known format: {0}' -f $Address)
continue
}
}

'^([0-9a-f]{12})$' {
Write-Verbose 'Delimiter: None'
break
}
$Request.Query.searchText = $Address
$ePOComputers = Invoke-ePORequest @Request

default {
Throw ('MAC Address does not match known format: {0}' -f $MACAddress)
foreach ($ePOComputer in $ePOComputers) {
$ePOComputerObject = ConvertTo-ePOComputer $ePOComputer
Write-Output $ePOComputerObject
}
}

$Request.Query.searchText = $MACAddress
}

"IPAddress" {
$Request.Query.searchText = $IPAddress
foreach ($Address in $IPAddress) {
$Request.Query.searchText = $Address
$ePOComputers = Invoke-ePORequest @Request

foreach ($ePOComputer in $ePOComputers) {
$ePOComputerObject = ConvertTo-ePOComputer $ePOComputer
Write-Output $ePOComputerObject
}
}
}

"Tag" {
$Request.Query.searchText = $Tag
foreach ($T in $Tag) {
if ($T -is [ePOTag]) {
$Request.Query.searchText = $T.Name
} else {
$Request.Query.searchText = $T
}
$ePOComputers = Invoke-ePORequest @Request

foreach ($ePOComputer in $ePOComputers) {
$ePOComputerObject = ConvertTo-ePOComputer $ePOComputer
Write-Output $ePOComputerObject
}
}
}

"AgentGuid" {
$Request.Query.searchText = $AgentGuid
foreach ($Guid in $AgentGuid) {
$Request.Query.searchText = $Guid
$ePOComputers = Invoke-ePORequest @Request

foreach ($ePOComputer in $ePOComputers) {
$ePOComputerObject = ConvertTo-ePOComputer $ePOComputer
Write-Output $ePOComputerObject
}
}
}

"Username" {
$Request.Query.searchText = $Username
foreach ($User in $Username) {
$Request.Query.searchText = $User
$ePOComputers = Invoke-ePORequest @Request

foreach ($ePOComputer in $ePOComputers) {
$ePOComputerObject = ConvertTo-ePOComputer $ePOComputer
Write-Output $ePOComputerObject
}
}
}

"All" {
$Request.Query.searchText = ''
}
}
$ePOComputers = Invoke-ePORequest @Request

if ($PSCmdlet.ParameterSetName -eq 'ComputerName' -and $Computer -is [ePOComputer]) {
Write-Verbose 'Using pipelined ePOComputer object'
Write-Output $Computer
} else {
Write-Verbose 'Either not pipelined, or pipeline object is not an ePOComputer object'
$ePOComputers = Invoke-ePORequest @Request

foreach ($ePOComputer in $ePOComputers) {
$ePOComputerObject = ConvertTo-ePOComputer $ePOComputer
if ($PSCmdlet.ParameterSetName -eq 'ComputerName') {
if ($ForceWildcardHandling) {
Write-Output $ePOComputerObject
} elseif ($ePOComputer.'EPOComputerProperties.ComputerName' -eq $Computer) {
Write-Output $ePOComputerObject
}
} else {
foreach ($ePOComputer in $ePOComputers) {
$ePOComputerObject = ConvertTo-ePOComputer $ePOComputer
Write-Output $ePOComputerObject
}
}
Expand All @@ -214,7 +274,11 @@ function Get-ePOComputer {
}
}

end {}
end {
if (Get-Variable 'AllePOComputers' -Scope Script -ErrorAction SilentlyContinue) {
Remove-Variable -Name 'AllePOComputers' -Scope Script
}
}
}

Export-ModuleMember -Function 'Get-ePOComputer' -Alias 'Find-ePOwerShellComputerSystem', 'Find-ePOComputerSystem'
130 changes: 94 additions & 36 deletions ePOwerShell/Public/Get-ePORecoveryKey.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
#>

function Get-ePORecoveryKey {
[CmdletBinding(DefaultParametersetname = 'Computer')]
[CmdletBinding(DefaultParameterSetName = 'Computer')]
[Alias('Get-ePOwerShellMneRecoveryKey', 'Get-ePOMneRecoveryKey')]
[OutputType([System.Object[]])]
param (
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)]
[Parameter(ParameterSetName = 'Computer', Mandatory = $True, Position = 0, ValueFromPipeline = $True)]
[Alias('ComputerName', 'Name')]
$Computer
$Computer,

[Parameter(ParameterSetName = 'AgentGuid', Mandatory = $True)]
$AgentGuid
)

begin {
Expand All @@ -56,51 +59,106 @@ function Get-ePORecoveryKey {
}

process {
foreach ($Comp in $Computer) {
if ($Comp -is [ePOComputer]) {
Write-Verbose 'Computer is an ePOComputer object'
$CompResponse = $Comp
} else {
Write-Verbose 'Computer is not an ePOComputer object. Searching for...'
$CompResponse = Get-ePOComputer -Computer $Comp -ErrorAction Stop
}
try {
switch ($PSCmdlet.ParameterSetName) {
'Computer' {
foreach ($Comp in $Computer) {
if ($Comp -is [ePOComputer]) {
Write-Verbose 'Computer is an ePOComputer object'
$CompResponse = $Comp
} else {
Write-Verbose 'Computer is not an ePOComputer object. Searching for...'
$CompResponse = Get-ePOComputer -Computer $Comp -ErrorAction Stop
}

foreach ($Item in $CompResponse) {
Write-Verbose ('Detecting mount points for {0}' -f $Item.ComputerName)
$QueryRequest = @{
Table = 'MneVolumes'
Select = @(
'MneFvRecoveryKeys.DisplayName',
'MneVolumes.MountPoint'
)
Where = @{
eq = @{
'MneVolumes.EPOLeafNodeId' = $Item.ParentID
foreach ($Item in $CompResponse) {
Write-Verbose ('Detecting mount points for {0}' -f $Item.ComputerName)
$QueryRequest = @{
Table = 'MneVolumes'
Select = @(
'MneFvRecoveryKeys.DisplayName',
'MneVolumes.MountPoint'
)
Where = @{
eq = @{
'MneVolumes.EPOLeafNodeId' = $Item.ParentID
}
}
ErrorAction = 'Stop'
}

Write-Debug ('Mount point query request: {0}' -f ($QueryRequest | ConvertTo-Json))

$MountPoints = Invoke-ePOQuery @QueryRequest

if ($MountPoints.Count -eq 0) {
Write-Warning ('Failed to find mount points for {0}, Parent ID {1}' -f $Item.ComputerName, $Item.ParentID)
continue
}

Write-Debug ('Mount points: {0}' -f ($MountPoints -join ', '))

foreach ($MountPoint in $MountPoints) {
Write-Verbose ('Getting recovery key for mount point: {0}' -f $MountPoint.'MneFvRecoveryKeys.DisplayName')
$Request.Query.serialNumber = $MountPoint.'MneFvRecoveryKeys.DisplayName'

$RecoveryKey = Invoke-ePORequest @Request
$RecoveryKeyObject = [ePORecoveryKey]::new($Item.ComputerName, $MountPoint.'MneVolumes.MountPoint', $RecoveryKey)
Write-Output $RecoveryKeyObject
}
}
}
ErrorAction = 'Stop'
}

Write-Debug ('Mount point query request: {0}' -f ($QueryRequest | ConvertTo-Json))
'AgentGuid' {
foreach ($Guid in $AgentGuid) {
Write-Verbose ('Checking for system via Agent Guid: {0}' -f $Guid)

$MountPoints = Invoke-ePOQuery @QueryRequest
if (-not ($CompResponse = Get-ePOComputer -AgentGuid $Guid)) {
Write-Error ('Failed to find system via Agent Guid')
continue
}

if ($MountPoints.Count -eq 0) {
Write-Warning ('Failed to find mount points for {0}, Parent ID {1}' -f $Item.ComputerName, $Item.ParentID)
continue
}
Write-Verbose ('Detecting mount points for {0}' -f $CompResponse.ComputerName)

$QueryRequest = @{
Table = 'MneVolumes'
Select = @(
'MneFvRecoveryKeys.DisplayName',
'MneVolumes.MountPoint'
)
Where = @{
eq = @{
'MneVolumes.EPOLeafNodeId' = $CompResponse.ParentID
}
}
ErrorAction = 'Stop'
}

Write-Debug ('Mount points: {0}' -f ($MountPoints -join ', '))
Write-Debug ('Mount point query request: {0}' -f ($QueryRequest | ConvertTo-Json))

foreach ($MountPoint in $MountPoints) {
Write-Verbose ('Getting recovery key for mount point: {0}' -f $MountPoint.'MneFvRecoveryKeys.DisplayName')
$Request.Query.serialNumber = $MountPoint.'MneFvRecoveryKeys.DisplayName'
$MountPoints = Invoke-ePOQuery @QueryRequest

$RecoveryKey = Invoke-ePORequest @Request
$RecoveryKeyObject = [ePORecoveryKey]::new($Item.ComputerName, $MountPoint.'MneVolumes.MountPoint', $RecoveryKey)
Write-Output $RecoveryKeyObject
if ($MountPoints.Count -eq 0) {
Write-Warning ('Failed to find mount points for {0}, Parent ID {1}' -f $CompResponse.ComputerName, $Item.ParentID)
continue
}

Write-Debug ('Mount points: {0}' -f ($MountPoints -join ', '))

foreach ($MountPoint in $MountPoints) {
Write-Verbose ('Getting recovery key for mount point: {0}' -f $MountPoint.'MneFvRecoveryKeys.DisplayName')
$Request.Query.serialNumber = $MountPoint.'MneFvRecoveryKeys.DisplayName'

$RecoveryKey = Invoke-ePORequest @Request
$RecoveryKeyObject = [ePORecoveryKey]::new($Item.ComputerName, $MountPoint.'MneVolumes.MountPoint', $RecoveryKey)
Write-Output $RecoveryKeyObject
}
}
}
}
} catch {
Write-Information $_ -Tags Exception
}
}

Expand Down
Loading

0 comments on commit 1d3ede3

Please sign in to comment.