- <#
- .SYNOPSIS
- This script restarts the DNS Service on a Remote System
- .DESCRIPTION
- This script uses WMI to reach out and restart the DNS
- DNS service on a remote machine. in my home environment,
- I find the DNS service on the home DC fails and needs
- to be restarted - this script works!
- .NOTES
- File Name : Restart-DNS.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2009/10/restart-dnsps1.html
- .EXAMPLE
- PSH [C:\foo]: .\Restart-DNS.ps1
- Restarting DNS Service on: Cookham1
- DNS Service stopped
- DNS Service started
- #>
- ##
- # Start Script
- ##
- # Set server to reset and display our intention
- $Server = "Cookham1"
- "Restarting DNS Service on: $Server"
- # Get DNS service
- $dns = gwmi win32_service -computer $Server | where {$_.name -eq "DNS"}
- # Now stop it
- $ret = $dns.stopservice()
- if ($ret.returnvalue -eq 0) {"DNS Service stopped"}
- else {"DNS Service not stopped"}
- # And now restart it
- $ret = $dns.startservice()
- if ($ret.returnvalue -eq 0) {"DNS Service started"}
- else {"DNS Service not started"}
- # End of Script
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Thursday, 29 October 2009
Restart-DNS.ps1
Labels:
dns,
powershell,
PowerShell scripts,
PowerShell V2,
wmi
Saturday, 24 October 2009
Get-CARolesOfCaller.ps1
- <#
- .SYNOPSIS
- This script displays the CA roles of the caller
- .DESCRIPTION
- This script instantiates the CA COM object, gets
- the allowed roles, and displays them. This script
- also shows use of Bitwise And operations, typical
- when using output from API calls. Based on an earlier
- script by Vadims Podans.
- .NOTES
- File Name : Get-CARolesofCaller.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2009/10/get-carolesofcallerps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/aa383243%28VS.85%29.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-CARolesOfCaller.ps1'
- You have the following rights on this CA: Cookham11\Cookham-CookhamCA
- CA administrator
- CA officer
- CA auditor
- CA backup
- Enrollment access
- #>
- # Instantiate the COM object
- $CertAdmin = New-Object -com "CertificateAuthority.Admin.1"
- # Now get the roles assigned to me
- $CA = "Cookham11\Cookham-CookhamCA"
- $MyRoles = $CertAdmin.GetMyRoles([string] $CA)
- #Display Granular Rights
- "You have the following rights on this CA: {0}" -f $CA
- switch ($MyRoles){
- {$MyRoles -band 1} {" CA administrator"}
- {$MyRoles -band 2} {" CA officer"}
- {$MyRoles -band 4} {" CA auditor"}
- {$MyRoles -band 8} {" CA backup"}
- {$MyRoles -band 256} {" CA Read access"}
- {$MyRoles -band 512} {" Enrollment access"}
- default {" No CA Access"}
- }
Labels:
COM,
powershell,
PowerShell scripts
Wednesday, 21 October 2009
Get-DNSInfo.ps1
- <#
- .SYNOPSIS
- This script re-implements a MSDN Sample in PowerShell that
- shows the use of System.Net.NetworkInformation.NetworkInterface
- .DESCRIPTION
- This script gets network information and formats DNS info. The
- script is effectively a one-liner!
- .NOTES
- File Name : Get-DNSInfo.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getallnetworkinterfaces.aspx
- .EXAMPLE
- PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.Net.NetworkInformation\Get-DNSInfo.ps1'
- DnsSuffix IsDnsEnabled IsDynamicDnsEnabled
- --------- ------------ -------------------
- cookham.net False True
- cookham.net False True
- False True
- cookham.net False True
- #>
- ##
- # Start of script
- ##
- #Get information and print - do this as a 1-liner!
- [System.Net.Networkinformation.NetworkInterface]::GetAllNetworkInterfaces() | %{$_.GetIpProperties()} | FT Dnssuffix,Isdnsenabled,IsDynamicDnsEnabled -a
- # End of script
Thursday, 8 October 2009
Get-OutlookFolders.ps1
- <#
- .SYNOPSIS
- This script displays the MAPI namespace in the current
- Outlook Profile.
- .DESCRIPTION
- This script creates and Outlook.application object,
- then gets and displays the top level folders in the store, and
- the folders one level below.
- .NOTES
- File Name : Get-OutlookFolders.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2009/10/get-outlookfoldersps1.html
- .EXAMPLE
- PSH [C:\foo]: .\Get-OutLookFolders.PS1
- 3 Top Level Folders:
- Mailbox - Thomas Lee
- Public Folders
- Archive Folders
- Folder 'Mailbox - Thomas Lee' contains the following subfolders:
- Deleted Items
- Grateful Dead
- Inbox
- Outbox
- Sent Items
- Calendar
- Contacts
- Drafts
- Journal
- Junk E-mail
- Notes
- Quarantine
- RSS Feeds
- Sync Issues
- Tasks
- Conversation Action Settings
- Quick Step Settings
- Suggested Contacts
- Folder 'Public Folders' contains the following subfolders:
- Favorites
- All Public Folders
- Folder 'Archive Folders' contains the following subfolders:
- Deleted Items
- Sent Items
- Calendar
- Journal
- Tasks
- #>
- ##
- # Start of Script
- ##
- # First create Outlook object and get the Mapi namespace.
- $Outlook = New-Object -com Outlook.Application
- $Namespace = $outlook.GetNamespace("MAPI")
- # Now display top level folders
- "{0} Top Level Folders: " -f $Namespace.folders.count
- foreach ($Fl in $namespace.Folders) {
- " {0}" -f $Fl.Name}
- ""
- # Now look inside
- foreach ($Folder in $Namespace.Folders) {
- "Folder `'{0}`' contains the following subfolders: " -f $Folder.Name
- foreach ($fl in $Folder.Folders){
- " {0}" -f $Fl.Name
- }
- }
- # end of script
Wednesday, 7 October 2009
New-Folder.ps1
- <#
- .SYNOPSIS
- This script Creates then removes a folder using methods in
- System.IO.Directory.
- .DESCRIPTION
- This script checks to see if a staticly named folder exists.
- if not, it creates then removes the folder. The creation/deletion
- logic in enclosed within a try/catch block to capture errors.
- .NOTES
- File Name : New-Folder.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/54a0at6s.aspx
- .EXAMPLE
- PSH [C:\foo]: .\New-Folder.ps1'
- The directory was created successfully at 10/7/2009 12:00:35 PM.
- The directory was deleted successfully.
- #>
- # Specify the directory to manipulate
- $path = "c:\fooxx"
- # try to see if the dirctory exists (It should not!)
- try {
- if ([System.Io.Directory]::Exists($path)) {
- "That path exists already."
- return
- }
- # Try to create the directory
- $di = [System.Io.Directory]::CreateDirectory($path)
- # Get creattion time and display results
- $dit = [System.Io.Directory]::GetCreationTime($path)
- "The directory was created successfully at {0}." -f $dit
- # Delete the directory.
- $di.Delete()
- "The directory was deleted successfully."
- }
- catch {
- "The process failed"
- }
Tuesday, 6 October 2009
Create-TempFile.ps1
- <#
- .SYNOPSIS
- This script creates then copies a file using
- System.IO.Fileinfo.CopyTo()
- .DESCRIPTION
- This script first creates a temporary file, then
- copies it to another file then deletes the
- second file.
- .NOTES
- File Name : Create-TempFile.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2009/10/create-tempfile.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Create-TempFile.PS1'
- File C:\Users\tfl\AppData\Local\Temp\tmpF2C1.tmp exists
- Original file:
- -----------
- Hello
- And
- Welcome
- -----------
- C:\Users\tfl\AppData\Local\Temp\tmpF2C1.tmp was copied to C:\Users\tfl\AppData\Local\Temp\tmpF2E1.tmp
- C:\Users\tfl\AppData\Local\Temp\tmpF2E1.tmp was successfully deleted.
- #>
- ##
- # start of script
- ##
- # Create temp file and FileInfo object
- $Path = [System.Io.Path]::GetTempFileName()
- $fi1 = New-Object System.Io.FileInfo $Path
- # See if file exists
- if ((Ls $fi1)) {
- "File {0} exists" -f $fi1
- # Create a file to write to and write to it
- $sw = $fi1.CreateText()
- $result=$sw.WriteLine("Hello")
- $result=$sw.WriteLine("And")
- $result=$sw.WriteLine("Welcome")
- $sw.Close()
- }
- # Open the file to read from.
- $sr = $fi1.OpenText()
- $s = "";
- "Original file:";"-----------"
- while (($s = $sr.ReadLine()) -ne $null) {
- $s
- }
- "-----------"
- try {
- $path2 = [System.IO.Path]::GetTempFileName()
- $fi2 = New-Object system.IO.FileInfo $path2
- # Ensure that the target does not exist.
- $fi2.Delete()
- # Copy the file
- $result = $fi1.CopyTo($path2)
- "{0} was copied to {1}" -f $path, $path2
- # Delete the newly created file.
- $fi2.Delete()
- "{0} was successfully deleted." -f $path2
- }
- catch {
- "The process failed:";$Error[0]
- }
- # End of Script
- "-----------"
Labels:
powershell,
PowerShell scripts,
PowerShell V2
Monday, 5 October 2009
Get-FolderSize.ps1
- <#
- .SYNOPSIS
- This script displays the size of a folder
- .DESCRIPTION
- This script is a rewrite of an MSDN sample. It
- uses System.IO namespace to determine the size
- of a folder and its subfolders, then displays it.
- Note that the Get-DirSize function is recursive!
- .NOTES
- File Name : Get-FolderSize.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to: http://pshscripts.blogspot.com/2009/10/get-foldersizeps1.html
- MSDN Sample posted at: http://msdn.microsoft.com/en-us/library/system.io.directory.aspx
-
- .EXAMPLE
- PSH [C:\foo]: .Get-FolderSize c:\foo
- The size of C:\Foo and its subdirectories is 17,577,318 bytes
- #>
-
- param (
- $folder = "C:\Foo"
- )
-
- ##
- # Helper Function Get-DirSize
- ##
- function Get-DirSize {
- param ([system.IO.DirectoryInfo] $d)
- [long] $Size = 0;
-
- # Add file sizes.
- $fis = $d.GetFiles();
- foreach ($fi in $fis){
- $size += $fi.Length;
- }
- # Add subdirectory sizes recursively.
- $dis = $d.GetDirectories()
- foreach ($di in $dis) {
- $Size += Get-DirSize($di)
- }
- return $Size
- }
-
- ## End of Get-DirSize helper function
-
- ##
- # Start of Script
- $d = New-Object system.IO.DirectoryInfo $folder
- $size= Get-Dirsize $d
- "The size of {0} and its subdirectories is {1} bytes" -f $d,$size.ToString("###,###")
Subscribe to:
Posts (Atom)