Thursday, 6 July 2017

Setting Windows PowerShell Path

Hi!

Here we discuss some ways to set the PowerShell Path variable to meet our requirements.

We would look at the following scenarios.

Check the current PATH
Change the Current PATH
Append to the Current PATH
Make the PATH changes permanent.


Check the current PATH
To find out the current PowerShell Path, run the below cmd in PowerShell.

$env:Path
Change the Current PATH
This would completely change the existing PATH to the new PATH defined by you.


 $env:Path = "C:\newpath;c:\newpath64";





Append to the Current PATH
This could be the best way to consider if you would like to change the PATH, so that you may not end up breaking some dependencies that may be needed.

 $env:Path += ";C:\Program Files\NewApp\bin;C:\Program Files\NewApp\Lib"
Please pay attention to the semicolon ";" before the new PATH folders, if this is missed it could break the PATH variable syntax, the command may go through, but you may not get the desired output.



Make the PATH changes permanent. 
And finally make the PATH changes permanent for your profile when ever you open PowerShell.
By default the user profile loads this PATH,

[Environment]::SetEnvironmentVariable("Path", "C:\Program Files\NewApp\bin;C:\Program Files\NewApp\Lib" , [EnvironmentVariableTarget]::User)



The path would not change immediately, we need to re-open the session for this to work!

This would actually create a key at  HKEY_CURRENT_USER\Environment :: PATH with the Folders added. When the profile loads this would be appended to the existing PATH.

If you want to revert, delete the KEY PATH at HKEY_CURRENT_USER\Environment\

Happy Scripting!








Tuesday, 4 July 2017

Enable/Disable Proxy Settings via PowerShell.

Hello,


Here is a PowerShell function to SET the HTTP proxy server's address and port with optional parameter to set the Automatic Configuration Script.
The input of the CmdLet has two input parameters -proxy which would set the proxy server details and -acs for the Automatic Configuration Script value.
-proxy is the mandatory parameter and -acs is optinal while calling the function.

Set-InternetProxy

This function will edit the values at "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" as below.
It enables the Proxy if disabled by default.
  
SYNTAX
Enable proxy:
    Set-InternetProxy [-Proxy] <string[]> [[-acs] <string[]>]  [<CommonParameters>]

 Disable-proxy:
    Disable-InternetProxy






Here is the function's code :

Set-InternetProxy

<#
.Synopsis
This function will set the proxy settings provided as input to the cmdlet.
.Description
This function will set the proxy server and (optinal) Automatic configuration script.
.Parameter ProxyServer
This parameter is set as the proxy for the system.
Data from. This parameter is Mandatory
.Example
Setting proxy information
Set-InternetProxy -proxy "proxy:7890"
.Example
Setting proxy information and (optinal) Automatic Configuration Script
Set-InternetProxy -proxy "proxy:7890" -acs "http://proxy:7892"
#>


Function Set-InternetProxy
{
    [CmdletBinding()]
    Param(
       
        [Parameter(Mandatory=$True,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [String[]]$Proxy,

        [Parameter(Mandatory=$False,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [AllowEmptyString()]
        [String[]]$acs
               
    )

    Begin
    {

            $regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
       
    }
   
    Process
    {
       
        Set-ItemProperty -path $regKey ProxyEnable -value 1

        Set-ItemProperty -path $regKey ProxyServer -value $proxy
                           
        if($acs)
        {           
           
                 Set-ItemProperty -path $regKey AutoConfigURL -Value $acs         
        }

    }
   
    End
    {

        Write-Output "Proxy is now enabled"

        Write-Output "Proxy Server : $proxy"

        if ($acs)
        {
           
            Write-Output "Automatic Configuration Script : $acs"

        }
        else
        {
           
            Write-Output "Automatic Configuration Script : Not Defined"

        }
    }
}


Disable-InternetProxy 

Function Disable-InternetProxy
{
  Begin
    {

            $regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
       
    }
   
    Process
    {
       
        Set-ItemProperty -path $regKey ProxyEnable -value 0 -ErrorAction Stop

        Set-ItemProperty -path $regKey ProxyServer -value "" -ErrorAction Stop
                           
        Set-ItemProperty -path $regKey AutoConfigURL -Value "" -ErrorAction Stop         
      
    }
   
    End
    {

        Write-Output "Proxy is now Disabled"

             
    }

}


Note: For the changes to take effect the IE (Internet Explorer) should be opened and closed.

Happy Scripting!

Saturday, 1 July 2017

DSC for beginners Part 2

Folks,

If you are here on this page directly because of the search engine, you need to go here first -> DSC for beginners Part 1 and i mean it.

Managing nodes from a Configuration Management tool is healthy than logging into the servers.
This would simplify the change management and the risk of human errors.

We jump to the example :
The very common problem, a configuration (editable file) being changed causing issues in the environment. We want to keep the file as it is.

Once the LCM is set as per the earlier post,open ISE and paste the code below.

#I Have no app running on this VM.
#This is to provide demo.
#This would create the file with the desired contents if deleted.
#This would revert changes inside the file if altered.
Configuration ImportantFile
{

   File KeepTheFileandContent
   {
       Ensure = 'Present'
       Type = 'File'
       DestinationPath = 'C:\MyAppConfig\App.conf'
       Contents = "appPath = C:\MyAppConfig"
   }

}
ImportantFile -OutputPath C:\ImportantFile\



To initiate the configuration

Start-DscConfiguration -Path C:\ImportantFile\ -Verbose -Wait



Now you could do the

Test 1

Delete the file C:\MyAppConfig\App.conf

Test 2
Edit the content of the file C:\MyAppConfig\App.conf

Give it some time.

The server/node could be validated if its in the desired state by executing

Test-DscConfiguration -Verbose



Output: True indicates the server is in the desired state with the configuration defined.

PowerShell DSC is very rich in features, this initial post is just the scope what it could do.

Good Source for DSC Learning : 
https://channel9.msdn.com/Series/Getting-Started-with-PowerShell-Desired-State-Configuration-DSC

Happy Scripting.