Skip to content

Commit

Permalink
Updates to get-foghost options and speed and added get-lastimagetime …
Browse files Browse the repository at this point in the history
…function
  • Loading branch information
darksidemilk committed Aug 23, 2022
1 parent 1d4e77d commit c9bb997
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 9 deletions.
40 changes: 31 additions & 9 deletions FogApi/Public/Get-FogHost.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ function Get-FogHost {
.DESCRIPTION
Searches a new or existing object of hosts for a specific host (or hosts) with search options of uuid, hostname, or mac address
if no search terms are specified then it gets the search terms from your host that is making the request and tries to find your
computer in fog
computer in fog. IF you specify the id of the host, then only that host is queried for in the api, otherwise it gets all hosts and searches
that object with the given parameters.
.PARAMETER uuid
the uuid of the host
Expand All @@ -21,7 +22,7 @@ function Get-FogHost {
defaults to calling Get-FogHosts but if you already have that in an object you can pass it here to speed up processing
.EXAMPLE
Get-FogHost -hostName MewoMachine
Get-FogHost -hostName MeowMachine
This would return the fog details of a host named MeowMachine in your fog instance
Expand All @@ -30,26 +31,42 @@ function Get-FogHost {
If you specify no param it will return your current host from fog
.EXAMPLE
Get-FogHost -hostID 1234
Will get the host of id 1234 directly, this is the fastest way to call the function
.EXAMPLE
Get-FogHost -serialNumber
#>

[CmdletBinding()]
[CmdletBinding(DefaultParameterSetName='searchTerm')]
param (
[parameter(ParameterSetName='searchTerm')]
[string]$uuid,
[parameter(ParameterSetName='searchTerm')]
[string]$hostName,
[parameter(ParameterSetName='searchTerm')]
[string]$macAddr,
[parameter(ParameterSetName='byID',Mandatory=$true)]
[string]$hostID,
$hosts = (Get-FogHosts)
[parameter(ParameterSetName='serialNumber',Mandatory=$true)]
[string]$serialNumber
)

begin {
[bool]$found = $false;
Write-Verbose 'Checking for passed variables'
if (!$uuid -and !$hostName -and !$macAddr -and !$hostID) {
if ($serialNumber) {
$inventorys = (Get-FogObject -type object -coreObject inventory).inventorys
$hostID = $inventorys | Where-Object { $_.sysserial -eq $serialNumber -OR $_.mbserial -eq $serialNumber -OR $_.caseserial -eq $serialNumber } | Select-Object -ExpandProperty HostID #find the inventory where the serial number matches one of the serial numbers in a hosts inventory and select the host id from that
} elseif (!$uuid -and !$hostName -and !$macAddr -and !$hostID) {
Write-Verbose 'no params given, getting current computer variables';
try {
$compSys = (Get-WmiObject Win32_ComputerSystemProduct);
} catch {
$compSys = Get-CimInstance -ClassName win32_computersystemproduct
} catch {
$compSys = (Get-WmiObject Win32_ComputerSystemProduct);
}
if ($compSys.UUID -notmatch "12345678-9012-3456-7890-abcdefabcdef" ) {
$uuid = $compSys.UUID;
Expand All @@ -72,6 +89,10 @@ function Get-FogHost {
).Replace("-",":");
}
$hostName = $(hostname);
} else {
if ($hostID) {
Write-Verbose "getting host from ID $hostID directly..."
}
}
Write-Verbose 'getting all hosts to search...';
Write-Verbose "search terms: uuid is $uuid, macAddr is $macAddr, hostname is $hostName";
Expand All @@ -81,18 +102,19 @@ function Get-FogHost {
Write-Verbose 'finding host in hosts';
[bool]$found = $false;
if ($hostID) {
$hostObj = $hosts | Where-Object id -eq $hostID;
$hostObj = get-fogobject -type object -coreObject host -IDofObject "$hostID";
if ($null -ne $hostObj) {
$found = $true;
}
} else {
$hosts = (Get-FogHosts)
$hostObj = $hosts | Where-Object {
($uuid -ne "" -AND $_.inventory.sysuuid -eq $uuid) -OR `
($hostName -ne "" -AND $_.name -eq $hostName) -OR `
($macAddr -ne "" -AND $_.macs -contains $macAddr);
if ($uuid -ne "" -AND $_.inventory.sysuuid -eq $uuid) {
Write-Verbose "$($_.inventory.sysuuid) matches the uuid $uuid`! host found";
$found = $true;
Write-Verbose "$($_.inventory.sysuuid) matches the uuid $uuid`! host found is $found";
}
if ($macAddr -ne "" -AND $_.macs -contains $macAddr) {
Write-Verbose "$($_.macs) matches the macaddress $macAddr`! host found";
Expand Down
67 changes: 67 additions & 0 deletions FogApi/Public/Get-LastImageTime.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
function Get-LastImageTime {
<#
.SYNOPSIS
Prompts for a serial number, finds the host by that serial number, and returns a string showing the last image time of that host
.DESCRIPTION
Searches the imaging log for the hostid and returns the last entries start time and image used in a descriptive string
.PARAMETER serialNumber
The serialnumber to search for, if not specified, it will prompt for input with readhost
.EXAMPLE
Get-LastImageTime
Will prompt you to scan/type in a serialnumber (i.e. via barcode).
Lets say you scan/input 12345678, if that serialnumber belong to a host named "test" it would display a string like this
"Serial number 12345678 belongs to host test, it was last imaged at 2022-08-18 12:19:38 with the image Win-10-21H2"
And return the full object of the host's imaging log
.EXAMPLE
Get-LastImageTime
.NOTES
Implemented as part of a feature request found in the forums here https://forums.fogproject.org/post/146276
#>
[CmdletBinding(DefaultParameterSetName="bySN")]
param (
[parameter(ParameterSetName='bySN')]
$serialNumber, #scan the barcode input into powershell
[parameter(ParameterSetName='byHostId')]
$hostId,
[parameter(ParameterSetName='byHost')]
$fogHost,
[parameter(ParameterSetName='byHost')]
[switch]$currentHost
)
process {
switch ($PSCmdlet.ParameterSetName) {
bySN {
Write-Verbose "Getting host by serial number"
if (!$serialNumber) {
$serialNumber = (Read-Host -Prompt "Scan Serial Number barcode")
}
$fogHost = Get-FogHost -serialNumber $serialNumber;
$HostID = $fogHost.id;
}
byHostID {
Write-Verbose "Getting host by id"
$fogHost = Get-FogHost -hostID $hostId;
}
byHost {
if (!$fogHost) {
Write-Verbose "getting host of current machine"
$fogHost = (Get-FogHost)
}
$HostID = $fogHost.id
}
}

$imageLog = (get-fogobject -type object -coreObject imaginglog).imagingLogs # get the image history log
$hostLogs = $imageLog | where-object hostid -eq $HostID # get the image history logs for the given host
$hostLog = $hostLogs[-1] # select the last/most recent log
#return a string of the information about the serial number
"Serial number $serialNumber belongs to host $($fogHost.name), it was last imaged at $($hostLog.start) with the image $($hostLog.image)" | Out-Host
return $hostLog;
}
}

0 comments on commit c9bb997

Please sign in to comment.