Friday, December 2, 2011

Change Service Start Mode using PowerShell

 

PowerTip of the Day, from PowerShell.com:

 

You can use WMI like this if you want to change a service start mode:

([wmi]'Win32_Service.Name="Spooler"').ChangeStartMode('Automatic').ReturnValue
([wmi]'Win32_Service.Name="Spooler"').ChangeStartMode('Manual').ReturnValue
 
Note that a return value of 0 indicates success. 
You will need Administrator privileges to change the start mode.
Thanks to www.powershell.com

“Enable or Disable” Network adapters using Powershell.

 

Hi,

In our IT environment we don’t give  “admin” rights to the normal users and they can’t enable/disable, install/un-install anything.

Today one of our users was having some problem with Wi-FI and to troubleshoot it i need to “enable” and “disable” Wi-Fi network adapter few times, but to enable or disable the network adapter i required to insert the “Administrator” username and password every time {remember my user don’t have admin privileges}.

network_1

This make me mad, i Inserted the username and password approx 10 times {somehow the problem was not resolved and with every change i need to disable and re-enable the wi-fi}, and I also don't want to login as administrator  to the system. Then i thought there must be a PowerShell way to do this and after few minutes of R&D i found the solution of my problem.

Problem:

user don’t have admin privileges to enable/disable any network adapters, so while troubleshooting if you need to enable or disable the network adapter it always ask for Administrator account to do the task.

 

network_1  network_2

Solution :

Run PowerShell as Administrator First

Search for PowerShell and then Right click and choose “Run as administrator

network_3

When PowerShell runs as Administrator, the PS path changed to “C:\widows\System32” and in title bar you can see the “Administrator” is written.

network_4

We will be using  WMI to enable and disable the network adapters.

I do remember that the WMI class for network adapter is start with win32_Network but i forget the full name of the class. lets search it first

  1: Get-WmiObject -List | where  { $_.Name -like "win32_network*"}

in above command we are listing all class in WMI and choosing those to display which starts with Win32_Network


It shows the all classes those are start with Win32_Network , the class which we need is Win32_NetworkAdapter


network_5


ok, lets query the Win32_NetworkAdapter class

  1: Get-WmiObject -Class win32_NetworkAdapter

and you see it shows the list of all network adapter installed in the system.


network_6 


lets filter it more and choose only Name of Adapter to display

  1: Get-WmiObject -Class win32_NetworkAdapter | select name

we piped the command further to Select-Object cmdlet and choose only to display name of the Network adpaters


network_7


Currently I am interested in enable/disable my Wi-Fi network Card. the Name of the  my Wi-Fi network adapter is “Intel(R) Centrino(R) Ultimate-N 6300 AGN


Now I will select only my “Intel(R) Centrino(R) Ultimate-N 6300 AGN” network adapter using where-object cmdlet and in $_.Name i am searching for that Name is Like something “ultimate-n”

  1: get-WmiObject win32_networkadapter |where { $_.name -like "*ultimate-n*"}

Now it showing only one Network Adapter and its showing the network adapter which i want to disable/enable. Our command is working fine till now.


network_8


Now I am going to put the above command in to a variable $wifi.

  1: $wifi = get-WmiObject win32_networkadapter |where { $_.name -like "*ultimate-n*"}

and lets also test the variable too.


All working fine ….


network_9


now pipe the $wifi to get-member cmdlet and check what the methods do we have

  1: $wifi | Get-Member -MemberType Method

NetworkAdapter has 4 methods but we are interested in only 2 for now, the enable and disable method.


To disable a Network adapter we need to use disable()


To enable a Network adapter we need to use enable()


network_10


lets try :)


first Disable it

$wifi.disable()

working


network_11


not enable it

  1: $wifi.enable()

it enabled :)


network_12


Now we can enable/disable network adapters number of times without inserting “Administrator” username and password again and again.


I hope it save someone's time :)


Thanks


Aman Dhally

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

Storing Pictures in Active Directory

 

PowerTip of the Day, from PowerShell.com:

When you need to store a picture into an AD account, the picture will have  to be converted to byte values before it can be stored. Just make sure you adjust the path to the picture you want to store and the LDAP path of the AD object you want the picture to be stored in:

 

  1: # adjust picture path:
  2: $file = "C:\pic.jpg"
  3: $bild = New-Object system.Drawing.Bitmap($file)
  4:  
  5: $ms = New-Object IO.MemoryStream
  6: $bild.Save($MS, 'jpeg')
  7: $MS.Flush()
  8: $byte = $MS.ToArray()
  9: # adjust user LDAP path:
 10: $user = New-Object System.DirectoryServices.DirectoryEntry`
 11: 
 12: ("LDAP://10.17.141.219/CN=Tobias,CN=Users,DC=powershell,DC=local")
 13: 
 14: $User.Properties["jpegPhoto"].Value = $byte
 

$user.SetInfo()


 


Thanks to www.PowerShell.com

Monday, November 28, 2011

Finding IP and MAC address

 

PowerTip of the Day, from PowerShell.com:

 

Finding IP and MAC address

When you query network adapters with WMI, it is not easy to find the active network card. To find the network card(s) that are currently connected to the network, you can filter based on NetConnectionStatus which needs to be "2" for connected cards. Then you can take the MAC information from the Win32_NetworkAdapter class and the IP address from the Win32_NetworkAdapterConfiguration class and combine both into one custom return object:

PS> Get-WmiObject Win32_NetworkAdapter -Filter 'NetConnectionStatus=2'

ServiceName : NETw5s64

MACAddress : 00:22:FA:D9:E1:50

AdapterType : Ethernet 802.3

DeviceID : 11

Name : Intel(R) WiFi Link 5100 AGN

NetworkAddresses :

Speed : 54000000

This gets you the network hardware but not the network configuration. To get the configuration data for this network card (like its IP address), get the related Win32_NetworkAdapterConfiguration instance:

########### Script Start Here ###########################

function Get-NetworkConfig {

Get-WmiObject Win32_NetworkAdapter -Filter 'NetConnectionStatus=2' |

ForEach-Object {

$result = 1 | Select-Object Name, IP, MAC

$result.Name = $_.Name

$result.MAC = $_.MacAddress

$config = $_.GetRelated('Win32_NetworkAdapterConfiguration')

$result.IP = $config | Select-Object -expand IPAddress

$result

}

}

###################### Script end Here #####################

 

PS> Get-NetworkConfig

Name IP Mac

---- -- ---

Intel(R) WiFi Link 5100... {78.64.118.150, fe80::a... 00:22:FA:D9:E1:50

 

Thanks to www.PowerShell.com

“Script to list all folder of Outlook and total number of emails store in them” using PowerShell

 

Hi,

from past few days I tried to writing a script which shows me that how many folders I do have in my Outlook and total number of emails stored on them.

I get only 50% success so far, I am able to see the folders but somehow not able to see the total number of items store in them

Then I tried searching the Internet and found a script which does it. This script is written by well knows PowerShell MVP “Shay levy”

This script solve my problem and then i thought i should share it with you.

you can download the script from here:  http://dl.dropbox.com/u/17858935/ListOutlook_Folders_Total_Emails.zip

Screenshot of the output when you run script.

Before: When first time I run the Script

After: I removed 2 email from my “Junk-Junk” folder and test it again and precise result

Thanks “Shay”

screenshot

 

Thanks

Aman Dhally

Friday, November 25, 2011

“Find how many types of files you have in you laptop” using Powershell.

 

Hi

Today i was searching for one PDF file on my laptop and then I just thinks “How Many PDF files do i have?”, i have no idea, one way to find is go to windows search and find all .PDF files? but “how can i find this with powershell”

we can

let try it ..

Cmdlet Used: Get-ChildItem,Group-Object, Sort-Object

Command: $files =  Get-ChildItem -Path C:\Users\aman.dhally\Documents –Recurse

In $files variable i am saving the result of Get-ChildItem -Path C:\Users\aman.dhally\Documents –Recurse  command , Get-ChildItem in equal as DIR command and we are using –Path switch parameter to give the path of the folder in which we want to search for files, –Recurse means including sub folders.

File

lets run $files variable and see what it has stored

It showing the files and folders located in my “Document” folder

File_2

let filer the results

$files | Group-Object -Property Extension | Sort-Object Count –Descending

now pipe the $files variable to Group-Object cmdlet and the grouping Property is Extensions because we want to group files by there extensions, now piped the command to Sort-Object and we are sorting the results by Count,

OoPs 751 .torrent files,, going to delete them . )

File_3 

Thanks

aman dhally