-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ChangePassword(Monitor/User/local): Add Set-FGTMonitorUserLocalChange…
…Password for change password for FortiOS 7.4.x (and after)
- Loading branch information
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
} |