Skip to content

Commit

Permalink
ChangePassword(Monitor/User/local): Add Set-FGTMonitorUserLocalChange…
Browse files Browse the repository at this point in the history
…Password for change password for FortiOS 7.4.x (and after)
  • Loading branch information
alagoutte committed Sep 16, 2024
1 parent 9b7bdbf commit e7c2838
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions PowerFGT/Public/monitor/user/local/changepassword.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#
# Copyright 2022, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#
function Set-FGTMonitorUserLocalChangePassword {

<#
.SYNOPSIS
Set User Local Change Password
.DESCRIPTION
Set User Local Change Password (For > FortiOS 7.4.X)
.EXAMPLE
$mynewpassword = ConvertTo-SecureString mypassword -AsPlainText -Force
PS > Get-FGTUserLocal MyFGTUserLocal | Set-FGTMonitorUserLocalChangePassword -new_password $mynewpassword
Change password for MyFGTUserLocal
#>

[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'medium')]
Param(
[Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1)]
[ValidateScript( { Confirm-FGTUserLocal $_ })]
[psobject]$userlocal,
[Parameter (Mandatory = $true)]
[SecureString]$new_password,
[Parameter(Mandatory = $false)]
[String[]]$vdom,
[Parameter(Mandatory = $false)]
[psobject]$connection = $DefaultFGTConnection
)

Begin {
}

Process {

$invokeParams = @{ }
if ( $PsBoundParameters.ContainsKey('vdom') ) {
$invokeParams.add( 'vdom', $vdom )
}

$uri = 'api/v2/monitor/user/local/change-password'

#before 7.4.x, you need to use Set-FGTLocalUser -passwd cmdlet
if ($connection.version -lt "7.4.0") {
Throw "You need to use Set-FGTLocalUser -passwd..."
}
else {
if (("Desktop" -eq $PSVersionTable.PsEdition) -or ($null -eq $PSVersionTable.PsEdition)) {
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($new_password);
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr);
}
else {
$password = ConvertFrom-SecureString -SecureString $new_password -AsPlainText
}

$body = @{
"username" = $userlocal.name
"new_password" = $password
}
}

if ($PSCmdlet.ShouldProcess($userlocal.name, 'Configure User Local Password')) {

Invoke-FGTRestMethod -uri $uri -method "POST" -body $body -connection $connection @invokeParams | Out-Null

Get-FGTUserLocal -connection $connection @invokeParams -name $userlocal.name
}

}

End {
}
}

0 comments on commit e7c2838

Please sign in to comment.