Showing posts with label Write-Host. Show all posts
Showing posts with label Write-Host. Show all posts

Monday, March 18, 2013

Powershell Script : Create An Encrypted Folder using Powershell.

Hi,

Windows Encryption is one of the best inbuilt security tool in windows7. My and my friends use windows encrypted file systems (EFS) a lot and may be you are using EFS in your windows environment.

Rather that create a whole DISK, My Documents, Desktop encrypted, I always prefer to create an encrypted folder somewhere on the disk and then put the files which you want to be encrypted in that encrypted folder.

I have written a small Powershell Script which created a encryption enabled folder on your desktop with folder name “Encrypted-Folder”.

You can download the script from this link http://gallery.technet.microsoft.com/scriptcenter/Create-Encrypted-Folder-6f0fe0c9

Script Logic
I have created a small flow charts on how this script works.
Encryption Script Sample
How it works.
  1. This script check for folder name “Encrypted-Folder” on your Desktop.
  2. If the Folder Exists.
    1. then it check if the attributes of the “Encrypted-Folder” is contains encrypted.
      1. If the Folder Attributes on “Encrypted-Folder”  contains “Encrypted”
      2. Then it won’t do anything.
      3. but
      4. If the Folder attributes doesn’t contains “Encrypted-Folder” 
      5. then
      6. it runs “cipher /e” command to make folder encrypted
  3. If the folder Does not exists,
    1. then this create a new folder “Encrypted-Folder”  on your desktop
    2. and encrypt it.
In this script i am using “cipher /e” to enable encryption. I have tested this script on windows7 and windows8 and it is working fine for me.
Screenshots.
18-03-2013 15-55-02
18-03-2013 16-15-19
18-03-2013 16-16-04

You can download the script from this link http://gallery.technet.microsoft.com/scriptcenter/Create-Encrypted-Folder-6f0fe0c9

That’s all for now.
Thanks
Aman Dhally
clip_image001 clip_image002 clip_image003 clip_image005clip_image007

Friday, August 31, 2012

Setting Microsoft Office 2012 Trust Center's Trusted Location Paths using Powershell.

Hi Everyone.

If you are working in a Telecom Industry, and if your Analysts and Consultants are using in house built templates then i am sure that you are 98% facing the “Trust Centre warning”.

We are using custom in house built Office Templates for Word,Excel, and PowerPoint.  and the problem was whenever we open our in house templates is Office 2012 we were facing the below error.

We can click on it but it is a quite annoying.

31-08-2012 14-47-12

To resolved this error, the solution is to add our custom built templates directory to the  trusted location in trust center manually on every laptop and desktop.

31-08-2012 15-14-10

Did i mentioned MANUALLY???

Naaaaaa !!!!!!! I thought that we can script this.

And to resolve this is wrote a simple script. you can download the script from this link :
http://gallery.technet.microsoft.com/scriptcenter/Setting-office-2010-31d6a5f4

This script is very simple.,

You only need to provide the path of your templates or the location which you want to add as trusted to  $trustlocation variable.
$trustlocation = 'C:\Mylocation\Templates'
and that’s all , when you run the script , the script will create a location registry  named as “Location99” in to the registry.
and the Registry Keys will be look like this .
31-08-2012 15-33-48
and i have added comments in the script for easy understanding,
Functionality.
  1. First script test the path of the “Location99” registry key
  2. and if the Test path fails that will create a New Registry  Folder “Location99” and the the following registry keys
      • AllowSubfolders this is DWORD registry key and the value is 1 to allow subfolders as a trusted location too.
      • Date  = Date of creation
      • Description = Description for location etc.
      • Path  =  Path to the folder
  3. and if folder found it just write a confirmation message in the console that folder do exists.
  4. The script will do the same process for WORD,EXCEL and Powerpoint.
After running the script the location is added to the Word.
31-08-2012 15-15-24

Thanks
Aman Dhally
join aman on facebook Join aman on Linkedin follow aman on Twitter

Wednesday, April 11, 2012

PowerShell Basics Part-1: Single Quotes (') and Double Quotes ("") in PowerShell.

2012-01_SingleVsDoubleQuotes 

Hi,

When new users start working on PowerShell the the main problem which they have faced is basic of PowerShell or we can say basic of any scripting language.

In this "PowerShell basics" tutorial series we are planning to cover as much as basics of PowerShell we can. These basics are not in any particular manner. The things which come in my mind and start covering them.

In the Part-1 we are covering "Single Quotes (') and Double Quotes ("").

In PowerShell we can use Single quotes and double quotes to  access the folder and files which have space in there names. Without "single or Double quotes" PowerShell consider blank space as separator.

Have a Look.

Try to Get-Childitem of "Program files" with out space, and you will get an error that it not able to find "C:\Program". As i mentioned before that without quotes PowerShell treat "blank space " as separator.

11-04-2012 14-44-25

Now lets wrap the the "Program Files" in Double or Single Quote. And this time it works Perfectly.

11-04-2012 14-44-50

we can also use the single quotes to achieve the same. Let see another example.

I have a text file named as " Space test.txt", lets try to open it without quotes first.

11-04-2012 14-57-24

OOps !! same error..

11-04-2012 14-58-49

Wrap it Single or Double Quotes,  this time lets wrap it in a Single quote and see what will happen.  Working :)

11-04-2012 15-00-28

But..the main difference in Single and Double quotes are described below.

"Double Quotes"

The Main Difference between "single and Double quotes" are that Variable are expands in "Double Quotes" but they don't expand in single quotes.

$name = "Aman"

11-04-2012 15-36-47

In variable $name i am assigning a value "Aman" let see if variable expand in $name.

You can see that in  Write-Host "My Name is $name" , $name is changed with the value "Aman"

11-04-2012 15-38-04

'Single Quotes'

The Single Quotes mainly used to mention the path to files and folders. I already mentioned the main difference above between "single and double" quotes that variables don't get expand in Single Quotes.

Let's look at same above example but with single quotes.

$name = "Aman"

You can see that in Write-Host "My Name is $name" , $name is not changed, that remain constant.

11-04-2012 15-46-16

Thanks for Reading.

Aman Dhally

Wednesday, April 4, 2012

IP Address Release, Renew using PowerShell.


Hi,

This is a very common task which is nearly performed by every End-user and IT Admin and Engineers. Normally we use "IPCONFIG /Release" to release the IP-address and "IPconfig / Renew" to renew or getting a new IP Address.

In some powershell script you may need to do this ip config /release and renew task. We can use native "IPconfig" command to achieve this. But there is an another way to do the same. Using WMI.
Download the script from here :

In variable $ethernet we are querying class Win32_NetworkAdapterConfiguration and using where-Object cmdlet we are choosing those adapter which have IP Enabled and have DHCP Enabled selected in there IP Configuration

than on each adapter we are using wmi method ReleaseDHCPLease()  first and then RenewDHCPLease() .

Download the script from here: http://gallery.technet.microsoft.com/scriptcenter/Renew-IP-Adresses-Using-365f6bfa

################################
$ethernet = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true}  
foreach ($lan in $ethernet) {
       Write-Host "Flushing IP addresses" -ForegroundColor Yellow
       Sleep 2
       $lan.ReleaseDHCPLease() | out-Null
       Write-Host "Renewing IP Addresses" -ForegroundColor Green
       $lan.RenewDHCPLease() | out-Null
       Write-Host "The New Ip Address is "$lan.IPAddress" with Subnet "$lan.IPSubnet"" -ForegroundColor Yellow
       }
#############################################
Download the script from here: http://gallery.technet.microsoft.com/scriptcenter/Renew-IP-Adresses-Using-365f6bfa

30-03-2012 15-41-48

I hope that it may help someone.

Thanks

Aman Dhally

Monday, March 19, 2012

Get an Average Ping Response time of multiple Servers using PowerShell.


Hi,

If you are an IT admin, then I am sure you are using PING command everyday. In PowerShell we have an Test-Connection command which is a very good alternative of it. as we know PowerShell is an object based scripting language by using it we can do much more.

Whenever I/we have network or internet related issues we do "Ping google.com" to check the response time of website and then we can know how our internet is performing. Sometime rather then pinging one  website we do ping few more website.

So i was thinking that if i can ping multiple servers in one time and get there average as as result. I don't want an fancy script just a simple script which i can run from my PowerShell console and view the result there.

So i just create a simple PowerShell script which can do this for us.

You can download the script from here : http://gallery.technet.microsoft.com/scriptcenter/Get-the-Average-Ping-49fc4924

#######################################
$CompName = "dc-Local","google.com","rediff.com","yahoo.com"
foreach ($comp in $CompName) {
       $test = (Test-Connection -ComputerName $comp -Count 4  | measure-Object -Property ResponseTime -Average).average
       $response = ($test -as [int] )
       write-Host "The Average response time for" -ForegroundColor Green -NoNewline;write-Host " `"$comp`" is " -ForegroundColor Red -NoNewline;;Write-Host "$response ms" -ForegroundColor Black -BackgroundColor white
}
##########################################
19-03-2012 14-05-36 


Thanks
Aman Dhally

Friday, March 2, 2012

Remove Computer Account from All Domain controllers using PowerShell


Hi,

If you are using Active Directory environment in you infrastructure then you does the one thing for sure , remove / add computers to the domain.

Some times when you format user computer and add it again to the Domain it gives an error that "Computer Already exists in the domain". Or you can say that you have multiple remote offices and syncing / replication between those  all DC's are slow  and you need to delete that computer from All DC including the local and remote domain Controllers.

I wrote this small script which delete the provided "computer" in to all DC which is on you replicated Server List. Before running this script make sure you have "Active Directory" module installed.

This script works for me and hope this works for you too.

You can Download the script from here : http://gallery.technet.microsoft.com/scriptcenter/Remove-Computer-Account-326f1e22

#########################################################
## make sure you have Active Directory Moudle Installed ##
# Import Module
Import-Module ActiveDirectory
# Variables
       $computer = $env:Computername     # Computername which you want to use
       $localdc = "Dc-XXXX"               # Chnage with ur local DC
       $credentials = Get-Credential   # This should be Admin Credentials
# AD  
       $ADResult = (Get-ADComputer -Filter {cn -like $computer}  -Server "$localdc" -Credential $credentials  ).name -eq $computer
       $dclist = (Get-ADDomain -Server "$localdc" -Credential $credentials).ReplicaDirectoryServers   
       $arrDc = @()
       foreach ($obj in $dclist) {
       $nlist = $obj.Replace("`.XYZ.com","")  # Replace XYZ.com with your Domain Name
       $arrDc += $nlist
       }
      
# If you want to remove it from AD remove -wahtif and un-commnted -confirm:$false
      
       if ($ADResult -eq $true) {      
       Write-Host -ForegroundColor  Red "$computer exists in AD, I am going to remove it"
       foreach ( $dc in $arrdc) {
              Remove-ADComputer -Identity "$computer"  -Server $dc  -Credential $credentials  -whatif  #-confirm:$false
              write-host $([char]7)
              write-Host "$computer is deleted on $dc " -ForegroundColor Green
              }     
              }
                          
## ENd of Script##### a m a n   d h a l l y ________                
02-03-2012 15-35-04

You can Download the script from here : http://gallery.technet.microsoft.com/scriptcenter/Remove-Computer-Account-326f1e22

Thanks for Reading.
New Delhi PowerShell User Group
Aman Dhally

Thursday, March 1, 2012

Clean Disk Space using PowerShell.


Hi,

I wrote a simple PowerShell script which helps us to clean some of the disk space for us. This script wrote primarily for LENOVO laptops but can tweak it as per your requirements.

This Script does the following tasks:

  • ·         Clean users TEMP folder
  • ·         Empty Recycle Bin
  • ·         Empty SWTOOL Folder {mainly in Lenovo laptops}
  • ·         Empty Windows TEMP Folder
  • ·         Run Windows  Disk Cleanup Tool.


I hope that you like this tiny script J


Thanks
Aman Dhally

Thursday, February 23, 2012

Using BEEP sound in PowerShell Scripts.


Hi,

After watching the valentine post of “Jeffery Hicks” I started playing with $([char]) characters. And while working with these characters I found a great character that is character 7.

23-02-2012 12-07-13

So what is interesting in character number 7?, it is a Beep Sound. For example if  in your script you are rebooting the server or laptop we can provide 3 beeps for rebooting. Or  we can  set a beep after every task or command finished his task.
Cool isn’t..

try this script.

Download Script from Here: http://gallery.technet.microsoft.com/scriptcenter/Using-BEEP-sound-in-f637bb48
#### save it as beep.ps1 or so....
Write-Host "I am going to reboot the Laptop after 3 Beep" -ForegroundColor Red
$([char]7)
write-Host "Beep 1 " -ForegroundColor Green
sleep 1
$([char]7)
write-Host "Beep 2 " -ForegroundColor Green
sleep 1
$([char]7)
write-Host "Beep 3 " -ForegroundColor Green
sleep 1
Write-Host "`n"
Restart-Computer -Force -WhatIf
#################################

and the output is like below. Our computer is going to reboot after 3 beeps...

23-02-2012 12-09-26

Download Script from Here: http://gallery.technet.microsoft.com/scriptcenter/Using-BEEP-sound-in-f637bb48

Thanks for reading,
Aman Dhally
messenger_freak_adp1 (30)

Tuesday, November 29, 2011

“Smart Backup” using PowerShell


Hi,
As a Network Admin I always worry about my backups. I was planning to write a simple script which create a new folder by date and copy the desired files and folder in to it recursively.
so i successfully  able to write a script for it and sharing it with you and I hope it is useful to you too. {or at least for tally administrators they always worried about there data, or we can say they this article contain “How to Automate Tally Backup”
I am running this script on my Tally server:
Script Logic:
  1. Map a Network Drive where you want to save the backup
  2. Create a Folder by using Date as Name
  3. Copy the backup
  4. Create a log file name as “backup_log.txt”
  5. When all some send a email to admin ($to) with backup_log.txt as attachment.
  6. Remove the Map Drive
you can download the script from here : http://dl.dropbox.com/u/17858935/Smart_Backup_Create%20folders_by_Date.zip

Note : change \\T_server\Tally with your folder where you want to take backup.
          Change $source with your source folder which you want to backedup

Script:
1: #System Variable for backup Procedure
2:  $date = Get-Date -Format d.MMMM.yyyy
3:  New-PSDrive -Name "Backup" -PSProvider Filesystem -Root "\\T_Server\Tally"
4:  $source = "D:\Tally\Data\"
5:  $destination = "backup:\$date"
6:  $path = test-Path $destination
7: #Email Variables
8:  $smtp = "Exchange-server"
9:  $from = "Tally Backup <tally.backup@xyz.com>"
10:  $to = "Aman Dhally <amandhally@gmail.com>"
11:  $body = "Log File of TALLY bacupk is attached, backup happens on of Date: $date"
12:  $subject = "Backup on $date" 
13: # Backup Process started
14:  if ($path -eq $true) {
15:     write-Host "Directory Already exists"
16:     Remove-PSDrive "Backup"  
17:     } elseif ($path -eq $false) {
18:             cd backup:\
19:             mkdir $date
20:             copy-Item  -Recurse $source -Destination $destination
21:             $backup_log = Dir -Recurse $destination | out-File "$destination\backup_log.txt"
22:             $attachment = "$destination\backup_log.txt"
23: #Send an Email to User 
24:             send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject $subject -Attachments $attachment -Body $body -BodyAsHtml
25:             write-host "Backup Sucessfull"
26:             cd c:\ 
27:  Remove-PSDrive "Backup"  
28:  }
I hope it may be useful to someone :) 
Thanks 
Aman Dhally