ninja-one/old/Reset-ComputerMachinePassword.ps1

#Requires -Version 5.1

function Get-NinjaProperty {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $True, ValueFromPipeline = $True)]
        [String]$Name,
        [Parameter()]
        [String]$Type,
        [Parameter()]
        [String]$DocumentName
    )

    # If we're requested to get the field value from a Ninja document we'll specify it here.
    $DocumentationParams = @{ }
    if ($DocumentName) {
        $DocumentationParams["DocumentName"] = $DocumentName
    }

    # These two types require more information to parse.
    $NeedsOptions = "DropDown", "MultiSelect"

    # Grabbing document values requires a slightly different command.
    if ($DocumentName) {
        # Secure fields are only readable when they're a device custom field
        if ($Type -Like "Secure") {
            throw [System.ArgumentOutOfRangeException]::New("$Type is an invalid type! Please check here for valid types. https://ninjarmm.zendesk.com/hc/en-us/articles/16973443979789-Command-Line-Interface-CLI-Supported-Fields-and-Functionality")
        }

        # We'll redirect the error output to the success stream to make it easier to error out if nothing was found or something else went wrong.
        Write-Host "Retrieving value from Ninja Document..."
        $NinjaPropertyValue = Ninja-Property-Docs-Get -AttributeName $Name @DocumentationParams 2>&1

        # Certain fields require more information to parse.
        if ($NeedsOptions -contains $Type) {
            $NinjaPropertyOptions = Ninja-Property-Docs-Options -AttributeName $Name @DocumentationParams 2>&1
        }
    }
    else {
        # We'll redirect error output to the success stream to make it easier to error out if nothing was found or something else went wrong.
        $NinjaPropertyValue = Ninja-Property-Get -Name $Name 2>&1

        # Certain fields require more information to parse.
        if ($NeedsOptions -contains $Type) {
            $NinjaPropertyOptions = Ninja-Property-Options -Name $Name 2>&1
        }
    }

    # If we received some sort of error it should have an exception property and we'll exit the function with that error information.
    if ($NinjaPropertyValue.Exception) {
        throw $NinjaPropertyValue
    }
    if ($NinjaPropertyOptions.Exception) {
        throw $NinjaPropertyOptions
    }

    if (-not $NinjaPropertyValue) {
        throw [System.NullReferenceException]::New("The Custom Field '$Name' is empty!")
    }

    # This switch will compare the type given with the quoted string. If it matches, it'll parse it further; otherwise, the default option will be selected.
    switch ($Type) {
        "Attachment" {
            # Attachments come in a JSON format this will convert it into a PowerShell Object.
            $NinjaPropertyValue | ConvertFrom-Json
        }
        "Checkbox" {
            # Checkbox's come in as a string representing an integer. We'll need to cast that string into an integer and then convert it to a more traditional boolean.
            [System.Convert]::ToBoolean([int]$NinjaPropertyValue)
        }
        "Date or Date Time" {
            # In Ninja Date and Date/Time fields are in Unix Epoch time in the UTC timezone the below should convert it into local time as a datetime object.
            $UnixTimeStamp = $NinjaPropertyValue
            $UTC = (Get-Date "1970-01-01 00:00:00").AddSeconds($UnixTimeStamp)
            $TimeZone = [TimeZoneInfo]::Local
            [TimeZoneInfo]::ConvertTimeFromUtc($UTC, $TimeZone)
        }
        "Decimal" {
            # In ninja decimals are strings that represent a decimal this will cast it into a double data type.
            [double]$NinjaPropertyValue
        }
        "Device Dropdown" {
            # Device Drop-Downs Fields come in a JSON format this will convert it into a PowerShell Object.
            $NinjaPropertyValue | ConvertFrom-Json
        }
        "Device MultiSelect" {
            # Device Multi-Select Fields come in a JSON format this will convert it into a PowerShell Object.
            $NinjaPropertyValue | ConvertFrom-Json
        }
        "Dropdown" {
            # Drop-Down custom fields come in as a comma-separated list of GUIDs; we'll compare these with all the options and return just the option values selected instead of a GUID.
            $Options = $NinjaPropertyOptions -replace '=', ',' | ConvertFrom-Csv -Header "GUID", "Name"
            $Options | Where-Object { $_.GUID -eq $NinjaPropertyValue } | Select-Object -ExpandProperty Name
        }
        "Integer" {
            # Cast's the Ninja provided string into an integer.
            [int]$NinjaPropertyValue
        }
        "MultiSelect" {
            # Multi-Select custom fields come in as a comma-separated list of GUID's we'll compare these with all the options and return just the option values selected instead of a guid.
            $Options = $NinjaPropertyOptions -replace '=', ',' | ConvertFrom-Csv -Header "GUID", "Name"
            $Selection = ($NinjaPropertyValue -split ',').trim()

            foreach ($Item in $Selection) {
                $Options | Where-Object { $_.GUID -eq $Item } | Select-Object -ExpandProperty Name
            }
        }
        "Organization Dropdown" {
            # Turns the Ninja provided JSON into a PowerShell Object.
            $NinjaPropertyValue | ConvertFrom-Json
        }
        "Organization Location Dropdown" {
            # Turns the Ninja provided JSON into a PowerShell Object.
            $NinjaPropertyValue | ConvertFrom-Json
        }
        "Organization Location MultiSelect" {
            # Turns the Ninja provided JSON into a PowerShell Object.
            $NinjaPropertyValue | ConvertFrom-Json
        }
        "Organization MultiSelect" {
            # Turns the Ninja provided JSON into a PowerShell Object.
            $NinjaPropertyValue | ConvertFrom-Json
        }
        "Time" {
            # Time fields are given as a number of seconds starting from midnight. This will convert it into a datetime object.
            $Seconds = $NinjaPropertyValue
            $UTC = ([timespan]::fromseconds($Seconds)).ToString("hh\:mm\:ss")
            $TimeZone = [TimeZoneInfo]::Local
            $ConvertedTime = [TimeZoneInfo]::ConvertTimeFromUtc($UTC, $TimeZone)

            Get-Date $ConvertedTime -DisplayHint Time
        }
        default {
            # If no type was given or not one that matches the above types just output what we retrieved.
            $NinjaPropertyValue
        }
    }
}

# Get password from secure custom field
if ($env:passwordToResetWith -and $env:passwordToResetWith -notlike "null") {
    try {
        $Password = Get-NinjaProperty -Name $env:passwordToResetWith
    }
    catch {
        Write-Host "[Error] Failed to get password from secure custom field."
        exit 1
    }
}

try {
    Write-Output "[Info] Starting to Reset Computer Machine Password"

    $username = "JRE\jreadmin"
    $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
    $Credential = New-Object System.Management.Automation.PSCredential ($username, $SecurePassword)

    Reset-ComputerMachinePassword -Credential $Credential 

    Write-Output "[Info] Successfully Reset Computer Machine Password"

    # Force a group policy update
    Write-Output "[Info] Forcing Group Policy Update"
    Start-Process -FilePath "gpupdate" -ArgumentList "/force" -Wait -NoNewWindow -ErrorAction Stop
    Write-Output "[Info] Successfully Forced Group Policy Update"
}
catch {
    Write-Output "[Error] Failed to Reset Computer Machine Password"
    Write-Output "Exception: $( $_.Exception.Message )"
    exit 1
}

# Clear sensitive data from memory
$Password = $null
$SecurePassword = $null
$Credential = $null

exit 0