Skip to content

Commit

Permalink
Merge pull request #54 from greenlighttec/master
Browse files Browse the repository at this point in the history
Updates and new functions
  • Loading branch information
greenlighttec authored Jun 30, 2024
2 parents 19dcab4 + b15f6bd commit e0889a0
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 5 deletions.
49 changes: 49 additions & 0 deletions HuduAPI/Public/Get-HuduPasswordFolders.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function Get-HuduPasswordFolders {
<#
.SYNOPSIS
Get a list of Password Folders
.DESCRIPTION
Calls Hudu API to retrieve folders
.PARAMETER Id
Id of the folder
.PARAMETER Name
Filter by name
.PARAMETER CompanyId
Filter by company_id
.EXAMPLE
Get-HuduFolders
#>
[CmdletBinding()]
Param (
[Int]$Id = '',
[String]$Name = '',
[String]$Search = '',
[Alias('company_id')]
[Int]$CompanyId = '',
[Int]$page = '',
[Int]$page_size = ''
)

if ($id) {
$Folder = Invoke-HuduRequest -Method get -Resource "/api/v1/password_folders/$id"
return $Folder.password_folder
} else {
$Params = @{}

if ($CompanyId) { $Params.company_id = $CompanyId }
if ($Name) { $Params.name = $Name }

$HuduRequest = @{
Method = 'GET'
Resource = '/api/v1/password_folders'
Params = $Params
}
Invoke-HuduRequestPaginated -HuduRequest $HuduRequest -Property password_folders
}
}
86 changes: 86 additions & 0 deletions HuduAPI/Public/Move-HuduAssetsToNewLayout.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
function Move-HuduAssetsToNewLayout {
<#
.SYNOPSIS
Helper function that uses the Set-HuduAsset function to move an asset between asset layouts. This will leave behind orphan data in the database.
Review the article https://portal.risingtidegroup.net/kb?id=29 for more details.
.DESCRIPTION
Calls the Hudu API to update an asset by switching its asset_layout_id property to a different asset layout.
This function migrates the asset to the specified new layout while maintaining its fields. Note that this
operation may leave behind orphaned data in the Hudu database, so use it with caution.
.PARAMETER AssetsToMove
An array of assets to be moved to a new asset layout. Each asset must contain both 'id' and 'fields' properties.
.PARAMETER NewAssetLayoutID
The ID of the new asset layout to which the assets will be moved.
.EXAMPLE
$AssetLayout = Get-HuduAssetLayouts -Name "Servers"
$AssetsToUpdate = Get-HuduAssets -AssetLayoutId 9
Move-HuduAssetsToNewLayout -AssetsToMove $AssetsToUpdate -NewAssetLayoutID $AssetLayout.id
This example retrieves the asset layout with the name "Servers" and the assets with the layout ID 9, then moves those assets to the new layout.
.NOTES
Ensure that the new asset layout ID is valid and that the assets to be moved contain the required properties.
Using this function may result in orphaned data in your Hudu database. Review the provided article for more details.
#>
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({
if ($BadAssets = ($_ | where {(-not $_.id)})) {
$BadAssets
throw "Assets must be an object with an ID"
}
return $true
})]
[array]
$AssetsToMove,

[Parameter(Mandatory = $true)]
[int]
$NewAssetLayoutID
)

Write-Warning "Performing this function will leave behind orphaned data in your Hudu database. Please review https://portal.risingtidegroup.net/kb?id=29"
Read-Host "Press Enter to continue or (CTRL+C) to cancel..."

$assets = foreach ($AssetToMove in $AssetsToMove) {
if (-not ($AssetToMove.PSObject.Properties.Match('id')) -or -not ($AssetToMove.PSObject.Properties.Match('fields'))) {
Write-Error "Asset does not contain both 'id' and 'fields' properties. Skipping this asset."
continue
}

if (-not $AssetToMove.fields) {
Write-Warning "Asset ID: $($AssetToMove.id) has no fields. Proceeding with moving the asset."
}

$assetId = $AssetToMove.id

if ($PSCmdlet.ShouldProcess("Asset ID: $assetId", "Move to new layout with ID $NewAssetLayoutID")) {
try {
Write-Verbose "Processing Asset ID: $assetId"

$fields = New-Object -TypeName psobject
foreach ($field in $AssetToMove.fields) {
$fieldName = $field.label.replace(' ', '_').tolower()
$fields | Add-Member -MemberType NoteProperty -Name $fieldName -Value $field.value -Force
}

(Set-HuduAsset -Id $assetId -AssetLayoutId $NewAssetLayoutID -Fields $fields).asset

Write-Verbose "Successfully moved Asset ID: $assetId"
}
catch {
Write-Error "Failed to move Asset ID: $assetId. Error: $_"
}
finally {
Remove-Variable -Name fields -ErrorAction SilentlyContinue
}
}
}
return $assets
}
14 changes: 9 additions & 5 deletions HuduAPI/Public/New-HuduAssetLayout.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,15 @@ function New-HuduAssetLayout {
'dropdown' { $field.'field_type' = 'Dropdown' }
'embed' { $field.'field_type' = 'Embed' }
'phone' { $field.'field_type' = 'Phone' }
('email' -or 'copyabletext') { $field.'field_type' = 'Email' }
('assettag' -or 'assetlink') { $field.'field_type' = 'AssetTag' }
('website' -or 'link') { $field.'field_type' = 'Website' }
('password' -or 'confidentialtext') { $field.'field_type' = 'Password' }
Default { Write-Error "Invalid field type: $($field.'field_type') found in field $($field.name)"; break }
'email' { $field.'field_type' = 'Email' }
'copyabletext' { $field.'field_type' = 'Email' }
'assettag' { $field.'field_type' = 'AssetTag' }
'assetlink' { $field.'field_type' = 'AssetTag' }
'website' { $field.'field_type' = 'Website' }
'link' { $field.'field_type' = 'Website' }
'password' { $field.'field_type' = 'Password' }
'confidentialtext' { $field.'field_type' = 'Password' }
Default { throw "Invalid field type: $($field.'field_type') found in field $($field.name)" }
}
}

Expand Down

0 comments on commit e0889a0

Please sign in to comment.