Showing posts with label Environment Variables. Show all posts
Showing posts with label Environment Variables. Show all posts

Tuesday, May 27, 2014

Learn how to use Windows PowerShell Desired State Configuration Environment Resource.

 

 

1.  Introduction of Windows PowerShell “Desired State Configuration”.

2.  Installing Windows PowerShell 4.0 [Windows Management Framework 4.0].

3.  Getting Started With Desired State Configuration: DSC Syntax.

4.  Review of Desired State Configuration: The 3 easy steps.

5.  Write your First Desired State Configuration Script using ROLE Resource.

6.  Run your first "DSC" PowerShell Script.

7. Configuring Dependencies in "Desired State Configuration" script in PowerShell.

8. PowerShell and DSC : Using File Resource

9. Using Registry Resource

10. Using Archive Resource.

11. Using Group Resource.

12. Using User Resource

13. Using Service Resource.

14. Configure the Service to run under different account name in Service Resource.

 

I am hoping that you are already knows about "Windows Environment" variables, they are very helpful, I do use them heavily, they are very important piece in PowerShell scripts.

Today we are going to use the "Environment Resource" of the Windows PowerShell "Desired State Configuration" in our script.

The Syntax of the Windows Environment Resource is :

27-05-2014 13-14-19

Let's get started.

This is the simplest "Desired State Configuration" script.  In this script, we are setting a new "Windows Environment Variable" , named as "ServerRack", in this variable, I am manually storing the location of my server rack as a variable value.

I am sure , that in this script, I don't need to explain anything, this is very simple and straight forwarded script.

Configuration setEnvpath
{
Node 'Posh-Demo'
{
Environment serveRackInfo
{
Ensure = 'Present'
Name = "ServerRack"
Value = "Rack2 U9"

}
}
}
setEnvpath


27-05-2014 13-08-27


Before running the script, lets' check the server and see if we have "ServerRack" Windows Environment variable set.


you can see that, currently there are no variable set yet.


27-05-2014 13-08-00


The name of our script is "DSC_EnvironmentDemo.ps1"  and the name of our "Desired State Configuration" Configuration is "setEnvpath".


Run the script, and it will export the MOF file.


27-05-2014 13-08-52


Now, deploy the MOF file by using Start-DscConfiguration cmdlet.


Start-DscConfiguration -Path .\setEnvpath -Wait -Verbose


27-05-2014 13-09-32


Our DSC script has run successfully.


Let's cross check it.


Note : Sometime, we may need to log-off or restart the system to check the Windows Environment after setup.


I am rebooting my server for a precaution sake.


27-05-2014 13-10-20


My server is rebooted and after rebooting you can see in the below screenshot, that our Windows Environment variable ServerRack is set-up correctly and it is showing us the proper value.


27-05-2014 13-14-00


That's all for now, will see you in my next blog post.


Thanks


Thank-you (1)


Aman Dhally


If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.

Monday, August 5, 2013

Powershell and MS Office : Setting User Initials in MS Office using Powershell.

 
Hi,

I hope you all are doing fine and enjoying my blog post.

I think we covered lots of things from setting wallpapers using Powershell to the generating reports from Active Directory, and this shows how powerful Powershell is and how we can use it in our various task to automate.

I simply love Powershell, not because it is Powerful but because it is one of (I want to say only) scripting language available, which is easy to learn and easy to use.

Ok, now lets discuss about my today’s task.

If in your company users are heavily using Word, Excel, PowerPoint, then you are aware from how, valuable are comments in.

To add a comment, you have to click on “Review ” tab and need to click on “New Comment” icon.
So now you may be ask, what is the big deal?

Nice question, when few users, or any team , works on a single file, and when they add comments in to the file, the comments are shown as “User Initials”.

So, what if your “User Initials” in Ms Office are wrong, then no one knows who commented an who added the notes.

Download link : http://gallery.technet.microsoft.com/scriptcenter/Fix-Users-Initial-in-35c2801b

05-08-2013 15-59-16

you can see my user initials are set wrong. and when i add comments they are coming as “SAM” rather then “AD”.

05-08-2013 16-31-06

This is a problem, for example, if you have 100+ user or so and incase few one’s user initials are wrong, how you are going to fix it?

Manually? Yes, you can, but, that is not a good option for scripters.

Powershell Script? Awesome, let see how we are do this.
This user initials for Ms Office application are stored in, 
HKCU:\Software\Microsoft\Office\Common\UserInfo” in key “UserInitials”.
In this script.We are going to set this key using “$env:USERNAME” environment variable.
Here is the script code.

# stroing $env:Username in to  a Username variable
$userName =  $env:USERNAME

# Checking the current  Initials in the registry and storing in a variable
$oldInitials = (Get-ItemProperty -Path HKCU:\Software\Microsoft\Office\Common\UserInfo).UserInitials

# spliting username
# if your username had space you can use => $split = $username.Split()
$split = $username.Split(".")

# joining spiting username and getting a first words only
switch ($split.Length)
              {
                     0      { Write-Warning "Not able to find username." ; break         }
                     1      { $newUserInitials =  $split[0].Remove(1)   ; break    }
                     2      { $newUserInitials =  $split[0].Remove(1) + $split[1].Remove(1)   ; break     }
                     3      { $newUserInitials =  $split[0].Remove(1) + $split[1].Remove(1)  + $split[3].Remove(1)    ; break  }
                     4      { $newUserInitials =  $split[0].Remove(1) + $split[1].Remove(1)  + $split[3].Remove(1)  +  $split[4].Remove(1)   ; break  }
                     5      { $newUserInitials =  $split[0].Remove(1) + $split[1].Remove(1)  + $split[3].Remove(1)  +  $split[4].Remove(1)  + $split[5].Remove(1) ; break  }
              }



# if old user intitails not matched with Our one
if ( $oldInitials -ne $newUserInitials )
       {
       set-ItemProperty -Path HKCU:\Software\Microsoft\Office\Common\UserInfo -Name UserInitials -Value $newUserInitials -Force
       Write-Host "User Initials Fixed" -ForegroundColor 'Green'
       }
else
       {
       Write-Host "User Initials matched."
       }
In above screenshot, you have seen that my user initials are set wrong in MS Office.

Now lets run the script and see and if this fix it. , Make sure that you all Office app are closed .

I run the script and and it is showing me that it fixed it.

05-08-2013 16-09-15

Now open any office app and see if this really fixed it.

Seems good.

05-08-2013 16-09-30 

now add some comments.

Yaay.!!!!!. it fixed.

05-08-2013 16-10-56

Download link : http://gallery.technet.microsoft.com/scriptcenter/Fix-Users-Initial-in-35c2801b

Thanks
Aman Dhally
clip_image001 clip_image002 clip_image003 clip_image005clip_image007