Saturday, October 12, 2019

Powershell Script to append environment variable PATH in windows

Powershell Script to append environment variable PATH in windows

#Requires -RunAsAdministrator
<#
.Synopsis
   Script to append environment variable with new path
.DESCRIPTION
   This script will add a required path to path in windows environment variable
   Requires admin powershell prompt
   This script modifies registery, running without understanding might corrupt your windows installation
.EXAMPLE
   .\script.ps1 -PathToAdd "C:\program files\programname\bin"
#>
param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
[ValidateNotNullOrEmpty()]
[string[]]$PathToAdd
)
         
process{
    foreach($path in $PathToAdd)
    {
        try
        {
           Write-Warning "Path : $path"
           Test-Path (Resolve-Path $path -ErrorAction Stop) | Out-Null
  
            $currentPathStrings = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path.split(';')
            $currentPathStrings = $currentPathStrings | ? {$_.trim()}
            $pathToSave = $currentPathStrings + $path
            $pathStringToWrite = $pathToSave -join ';'
            Write-Warning "Writing below paths"
            Write-Output $pathStringToWrite
            try
            {
                Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $pathStringToWrite -ErrorAction Stop
            }
            catch
            {
                Write-Warning "Error writing to registry"
                Write-Output $_
            }
        }
        catch
        {
            Write-Warning "Above path you are trying to add could not be resolved, please check if it is a plain path and accessible from file explorer"
        }
    }
}

Wednesday, October 9, 2019

Issue: Angular: Datatable data disappears from table on clicking sort or on search

Problem: Upon loading data from http call to a datatable, it disappers on clicking the table header or searching

Possible cause: Data table caches the data as null even before http call occurs and it is not aware that new data has been updated

Solution: Declare a boolean variable in the class as DataLoaded = false and reassign the variable to true inside the http subscribe block.

inside the <TABLE> directive use *ngIf = "DataLoaded" as <TABLE  *ngIf = "DataLoaded">

This will ensure that data table initializes only after the data is loaded.