Thursday, 29 October 2009

Restart-DNS.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script restarts the DNS Service on a Remote System 
  4. .DESCRIPTION 
  5.     This script uses WMI to reach out and restart the DNS 
  6.     DNS service on a remote machine. in my home environment, 
  7.     I find the DNS service on the home DC fails and needs  
  8.     to be restarted - this script works! 
  9. .NOTES 
  10.     File Name  : Restart-DNS.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscripts.blogspot.com/2009/10/restart-dnsps1.html
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Restart-DNS.ps1 
  18.     Restarting DNS Service on: Cookham1 
  19.     DNS Service stopped 
  20.     DNS Service started 
  21. #> 
  22. ## 
  23. # Start Script 
  24. ## 
  25.  
  26. # Set server to reset and display our intention 
  27. $Server = "Cookham1" 
  28. "Restarting DNS Service on: $Server" 
  29.   
  30. # Get DNS service 
  31. $dns = gwmi win32_service -computer $Server  | where {$_.name -eq "DNS"
  32.   
  33. # Now stop it 
  34. $ret = $dns.stopservice() 
  35. if ($ret.returnvalue -eq 0) {"DNS Service stopped"
  36. else {"DNS Service not stopped"
  37.   
  38. # And now restart it 
  39. $ret = $dns.startservice() 
  40. if ($ret.returnvalue -eq 0) {"DNS Service started"
  41. else {"DNS Service not started"
  42.   
  43. # End of Script 
Technorati Tags: ,,,,

Saturday, 24 October 2009

Get-CARolesOfCaller.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the CA roles of the caller 
  4. .DESCRIPTION 
  5.     This script instantiates the CA COM object, gets 
  6.     the allowed roles, and displays them. This script 
  7.     also shows use of Bitwise And operations, typical  
  8.     when using output from API calls.  Based on an earlier
  9.     script by Vadims Podans.
  10. .NOTES 
  11.     File Name  : Get-CARolesofCaller.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell V2 
  14. .LINK 
  15.     This script posted to: 
  16.         http://pshscripts.blogspot.com/2009/10/get-carolesofcallerps1.html
  17.     MSDN Sample posted at: 
  18.         http://msdn.microsoft.com/en-us/library/aa383243%28VS.85%29.aspx 
  19. .EXAMPLE 
  20.     PSH [C:\foo]: .\Get-CARolesOfCaller.ps1' 
  21.     You have the following rights on this CA: Cookham11\Cookham-CookhamCA 
  22.         CA administrator 
  23.         CA officer 
  24.         CA auditor 
  25.         CA backup 
  26.         Enrollment access 
  27. #> 
  28. # Instantiate the COM object 
  29. $CertAdmin = New-Object -com "CertificateAuthority.Admin.1" 
  30.   
  31. # Now get the roles assigned to me 
  32. $CA = "Cookham11\Cookham-CookhamCA" 
  33. $MyRoles = $CertAdmin.GetMyRoles([string] $CA
  34.  
  35. #Display Granular Rights 
  36. "You have the following rights on this CA: {0}" -f $CA 
  37.  switch ($MyRoles){ 
  38. {$MyRoles -band 1}     {"    CA administrator"
  39. {$MyRoles -band 2}     {"    CA officer"
  40. {$MyRoles -band 4}     {"    CA auditor"
  41. {$MyRoles -band 8}     {"    CA backup"
  42. {$MyRoles -band 256}   {"    CA Read access"
  43. {$MyRoles -band 512}   {"    Enrollment access"
  44. default                {"    No CA Access"

Wednesday, 21 October 2009

Get-DNSInfo.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script re-implements a MSDN Sample in PowerShell that 
  4.    shows the use of System.Net.NetworkInformation.NetworkInterface 
  5. .DESCRIPTION 
  6.     This script gets network information and formats DNS info. The 
  7.     script is effectively a one-liner! 
  8. .NOTES 
  9.     File Name  : Get-DNSInfo.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN Sample posted at: 
  16.         http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getallnetworkinterfaces.aspx 
  17. .EXAMPLE 
  18.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.Net.NetworkInformation\Get-DNSInfo.ps1' 
  19.  
  20.     DnsSuffix   IsDnsEnabled IsDynamicDnsEnabled 
  21.     ---------   ------------ ------------------- 
  22.     cookham.net        False                True 
  23.     cookham.net        False                True 
  24.                        False                True 
  25.     cookham.net        False                True 
  26. #> 
  27.   
  28. ## 
  29. # Start of script 
  30. ## 
  31.  
  32. #Get information and print - do this as a 1-liner! 
  33. [System.Net.Networkinformation.NetworkInterface]::GetAllNetworkInterfaces() | %{$_.GetIpProperties()} | FT Dnssuffix,Isdnsenabled,IsDynamicDnsEnabled -a 
  34. # End of script 

Thursday, 8 October 2009

Get-OutlookFolders.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the MAPI namespace in the current  
  4.     Outlook Profile. 
  5. .DESCRIPTION 
  6.     This script creates and Outlook.application object, 
  7.     then gets and displays the top level folders in the store, and 
  8.     the folders one level below.      
  9. .NOTES 
  10.     File Name  : Get-OutlookFolders.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscripts.blogspot.com/2009/10/get-outlookfoldersps1.html 
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-OutLookFolders.PS1
  18.     3 Top Level Folders: 
  19.        Mailbox - Thomas Lee 
  20.        Public Folders 
  21.        Archive Folders 
  22.  
  23.     Folder 'Mailbox - Thomas Lee' contains the following subfolders: 
  24.        Deleted Items 
  25.        Grateful Dead 
  26.        Inbox 
  27.        Outbox 
  28.        Sent Items 
  29.        Calendar 
  30.        Contacts 
  31.        Drafts 
  32.        Journal 
  33.        Junk E-mail 
  34.        Notes 
  35.        Quarantine 
  36.        RSS Feeds 
  37.        Sync Issues 
  38.        Tasks 
  39.        Conversation Action Settings 
  40.        Quick Step Settings 
  41.        Suggested Contacts 
  42.     Folder 'Public Folders' contains the following subfolders: 
  43.        Favorites 
  44.        All Public Folders 
  45.     Folder 'Archive Folders' contains the following subfolders: 
  46.        Deleted Items 
  47.        Sent Items 
  48.        Calendar 
  49.        Journal 
  50.        Tasks 
  51. #> 
  52.  
  53. ##  
  54. # Start of Script 
  55. ## 
  56.  
  57. # First create Outlook object and get the Mapi namespace. 
  58. $Outlook = New-Object -com Outlook.Application 
  59. $Namespace = $outlook.GetNamespace("MAPI") 
  60.  
  61. # Now display top level folders 
  62. "{0} Top Level Folders:      " -f $Namespace.folders.count 
  63. foreach ($Fl in $namespace.Folders) { 
  64.   "   {0}" -f $Fl.Name} 
  65. "" 
  66. # Now look inside 
  67. foreach ($Folder in $Namespace.Folders) { 
  68. "Folder `'{0}`' contains the following subfolders: " -f $Folder.Name 
  69.   foreach ($fl in $Folder.Folders){ 
  70.   "   {0}" -f $Fl.Name 
  71.   } 
  72. }  
  73. # end of script 

Wednesday, 7 October 2009

New-Folder.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script Creates then removes a folder using methods in 
  4.     System.IO.Directory. 
  5. .DESCRIPTION 
  6.     This script checks to see if a staticly named folder exists. 
  7.     if not, it creates then removes the folder. The creation/deletion 
  8.     logic in enclosed within a try/catch block to capture errors. 
  9. .NOTES 
  10.     File Name  : New-Folder.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/54a0at6s.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\New-Folder.ps1' 
  20.     The directory was created successfully at 10/7/2009 12:00:35 PM. 
  21.     The directory was deleted successfully. 
  22. #> 
  23. # Specify the directory to manipulate 
  24. $path = "c:\fooxx" 
  25.   
  26. # try to see if the dirctory exists (It should not!) 
  27. try { 
  28.    if ([System.Io.Directory]::Exists($path)) {   
  29.                 "That path exists already." 
  30.                 return 
  31.     } 
  32.  
  33. # Try to create the directory 
  34. $di  = [System.Io.Directory]::CreateDirectory($path
  35.   
  36. # Get creattion time and display results 
  37. $dit = [System.Io.Directory]::GetCreationTime($path
  38. "The directory was created successfully at {0}." -f  $dit
  39.   
  40. # Delete the directory. 
  41.   $di.Delete() 
  42.   "The directory was deleted successfully." 
  43. }  
  44. catch { 
  45.             "The process failed" 
  46. }  

Tuesday, 6 October 2009

Create-TempFile.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates then copies a file using 
  4.     System.IO.Fileinfo.CopyTo() 
  5. .DESCRIPTION 
  6.     This script first creates a temporary file, then 
  7.     copies it to another file then deletes the  
  8.     second file. 
  9. .NOTES 
  10.     File Name : Create-TempFile.ps1 
  11.     Author : Thomas Lee - tfl@psp.co.uk 
  12.     Requires : PowerShell V2 
  13. .LINK 
  14.     This script posted to: 
  15.     http://pshscripts.blogspot.com/2009/10/create-tempfile.html
  16.     MSDN Sample posted at: 
  17.     http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Create-TempFile.PS1' 
  20.     File C:\Users\tfl\AppData\Local\Temp\tmpF2C1.tmp exists 
  21.     Original file: 
  22.     ----------- 
  23.     Hello 
  24.     And 
  25.     Welcome 
  26.     ----------- 
  27.     C:\Users\tfl\AppData\Local\Temp\tmpF2C1.tmp was copied to C:\Users\tfl\AppData\Local\Temp\tmpF2E1.tmp 
  28.     C:\Users\tfl\AppData\Local\Temp\tmpF2E1.tmp was successfully deleted.    
  29. #> 
  30.   
  31. ## 
  32. # start of script 
  33. ## 
  34.    
  35. # Create temp file and FileInfo object  
  36. $Path = [System.Io.Path]::GetTempFileName() 
  37. $fi1 = New-Object System.Io.FileInfo $Path 
  38.  
  39. # See if file exists 
  40. if ((Ls $fi1)) { 
  41.     "File {0} exists" -f $fi1  
  42.     # Create a file to write to and write to it 
  43.     $sw = $fi1.CreateText() 
  44.     $result=$sw.WriteLine("Hello"
  45.     $result=$sw.WriteLine("And"
  46.     $result=$sw.WriteLine("Welcome"
  47.     $sw.Close() 
  48.   
  49. # Open the file to read from. 
  50. $sr = $fi1.OpenText() 
  51. $s = ""
  52. "Original file:";"-----------" 
  53. while (($s = $sr.ReadLine()) -ne $null) { 
  54.     $s 
  55. "-----------"
  56. try { 
  57.     $path2 = [System.IO.Path]::GetTempFileName() 
  58.     $fi2 = New-Object system.IO.FileInfo $path2 
  59.  
  60.     # Ensure that the target does not exist. 
  61.     $fi2.Delete() 
  62.  
  63.     # Copy the file 
  64.     $result = $fi1.CopyTo($path2
  65.  
  66.     "{0} was copied to {1}" -f $path, $path2 
  67.  
  68.     # Delete the newly created file. 
  69.     $fi2.Delete() 
  70.     "{0} was successfully deleted." -f $path2 
  71. }  
  72. catch { 
  73.     "The process failed:";$Error[0] 
  74. # End of Script 
  75. "-----------" 

Monday, 5 October 2009

Get-FolderSize.ps1

  1. <#
  2. .SYNOPSIS
  3. This script displays the size of a folder
  4. .DESCRIPTION
  5. This script is a rewrite of an MSDN sample. It
  6. uses System.IO namespace to determine the size
  7. of a folder and its subfolders, then displays it.
  8. Note that the Get-DirSize function is recursive!
  9. .NOTES
  10. File Name : Get-FolderSize.ps1
  11. Author : Thomas Lee - tfl@psp.co.uk
  12. Requires : PowerShell V2
  13. .LINK
  14. This script posted to: http://pshscripts.blogspot.com/2009/10/get-foldersizeps1.html
  15. MSDN Sample posted at: http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

  16. .EXAMPLE
  17. PSH [C:\foo]: .Get-FolderSize c:\foo
  18. The size of C:\Foo and its subdirectories is 17,577,318 bytes
  19. #>

  20. param (
  21. $folder = "C:\Foo"
  22. )

  23. ##
  24. # Helper Function Get-DirSize
  25. ##
  26. function Get-DirSize {
  27. param ([system.IO.DirectoryInfo] $d)
  28. [long] $Size = 0;

  29. # Add file sizes.
  30. $fis = $d.GetFiles();
  31. foreach ($fi in $fis){
  32. $size += $fi.Length;
  33. }
  34. # Add subdirectory sizes recursively.
  35. $dis = $d.GetDirectories()
  36. foreach ($di in $dis) {
  37. $Size += Get-DirSize($di)
  38. }
  39. return $Size
  40. }

  41. ## End of Get-DirSize helper function

  42. ##
  43. # Start of Script
  44. $d = New-Object system.IO.DirectoryInfo $folder
  45. $size= Get-Dirsize $d
  46. "The size of {0} and its subdirectories is {1} bytes" -f $d,$size.ToString("###,###")