< .SYNOPSIS This script encrpyts then decrypts a byte string .DESCRIPTION This script uses System.Security to encrpyt a byte string, then decrypts it. .NOTES File Name : Protect-ByteArray.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.security.cryptography.protectedmemory.aspx .EXAMPLE PSH [C:\foo]: .Protect-ByteArray.ps1' Unencrpyted byte string: 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 6 Encrpyted byte string: 199 52 177 169 162 117 118 127 180 16 230 70 19 89 85 168 Unencrpyted byte string: 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 6 #> ## # Start of script ## # Load System.Security [void] [Reflection.Assembly]::LoadWithPartialName("System.Security") # Create and display a byte string [byte[]] $Secret = 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3,6 "Unencrpyted byte string:" $Secret # now encrypt it and display the encrpyted string [System.Security.Cryptography.ProtectedMemory]::Protect($Secret,[System.Security.Cryptography.MemoryProtectionScope]::SameLogon) "Encrpyted byte string:" $Secret # Now decrypt it and re-display it - it' s the same byte array we started with [System.Security.Cryptography.ProtectedMemory]::UnProtect($Secret ,[System.Security.Cryptography.MemoryProtectionScope]::SameLogon) "Unencrpyted byte string:" $Secret
< .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 $Server = "Cookham1" "Restarting DNS Service on: $Server" $dns = gwmi win32_service -computer $Server | where { $_ .name -eq "DNS" } $ret = $dns .stopservice() if ( $ret .returnvalue -eq 0) { "DNS Service stopped" } else { "DNS Service not stopped" } $ret = $dns .startservice() if ( $ret .returnvalue -eq 0) { "DNS Service started" } else { "DNS Service not started" }
< .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 $CertAdmin = New-Object -com "CertificateAuthority.Admin.1" $CA = "Cookham11\Cookham-CookhamCA" $MyRoles = $CertAdmin .GetMyRoles([string] $CA ) "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" } }
< .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 [System.Net.Networkinformation.NetworkInterface]::GetAllNetworkInterfaces() | %{$_ .GetIpProperties()} | FT Dnssuffix,Isdnsenabled,IsDynamicDnsEnabled -a
< .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 } }
< .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. $path = "c:\fooxx" try { if ([System.Io.Directory]::Exists( $path )) { "That path exists already." return } $di = [System.Io.Directory]::CreateDirectory( $path ) $dit = [System.Io.Directory]::GetCreationTime( $path ) "The directory was created successfully at {0}." -f $dit $di .Delete() "The directory was deleted successfully." } catch { "The process failed" }
< .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. $Path = [System.Io.Path]::GetTempFileName() $fi1 = New-Object System.Io.FileInfo $Path if ((Ls $fi1 )) { "File {0} exists" -f $fi1 $sw = $fi1 .CreateText() $result = $sw .WriteLine( "Hello" ) $result = $sw .WriteLine( "And" ) $result = $sw .WriteLine( "Welcome" ) $sw .Close() } $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 $fi2 .Delete() $result = $fi1 .CopyTo( $path2 ) "{0} was copied to {1}" -f $path , $path2 $fi2 .Delete() "{0} was successfully deleted." -f $path2 } catch { "The process failed:" ; $Error [0] } "-----------"
< .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" )
function Get-DirSize { param ([system.IO.DirectoryInfo] $d ) [long] $Size = 0;
$fis = $d .GetFiles(); foreach ( $fi in $fis ){ $size += $fi .Length; } $dis = $d .GetDirectories() foreach ( $di in $dis ) { $Size += Get-DirSize( $di ) } return $Size }
$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( "###,###" )
< .SYNOPSIS This script displays the platform id of the system .DESCRIPTION This script is a rewrite of an MSDN sample. It gets System.Environment.Version's Platform field and displays it. The sample is simplified from the C# version! Not tested except on Server 2008. .NOTES File Name : Get-PlatformId.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/3a8hyw88.aspx .EXAMPLE PSH [C:\foo]: .Get-PlatformID.ps1' Platform ID: Win32NT $OS = [System.Environment]::OSVersion $Platformid = $OS .Platform; "Platform ID: {0}" -f $Platformid
< .SYNOPSIS This script creates and sends an SMTP email message. .DESCRIPTION This script first creates a System.Net.Mail.Mailmessage, and populates it. Next, it creates an system.Net.Mail.SmtpClient, which then sends the message to the SMTP Server, and onwards transmission. This script is effectively a re-write of the C .NOTES File Name : Send-EmailMessage.ps1 Author : Thomas Lee - tfl@psp .co.uk Requires : PowerShell V2 .LINK This script posted to: http://pshscripts.blogspot.com/2009/09/send-emailmessageps1.html MSDN Sample posted at: http://msdn.microsoft.com/en-us/library/67w4as51.aspx .EXAMPLE PSH [C:\foo]: .\Send-EmailMessage.ps1' Message sent successfully #> ## # Start of Script ## # Set contents of the Email message $To = "doctordns@gmail.com" $From = "jane@cookham.net" $Subject = "Using the .NET SMTP client." $Body = "Using this .NET feature, you can send an e-mail message from an application very easily." # Create meil message $Message = New-Object System.Net.Mail.MailMessage $From, $To, $Subject, $Body # Create SMTP client $Server = "cookham8" $Port = 25 $Client = New-Object System.Net.Mail.SmtpClient $Server, $Port # Credentials are necessary if the server requires the client # to authenticate before it will send e-mail on the client' s behalf. $Client .Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials try { $Client .Send( $Message ) "Message sent successfully" } catch { "Exception caught in Send-Emailmessage.ps1" }