- <#
- .SYNOPSIS
- This script adds a program to the firewall.
- .DESCRIPTION
- This script used the firewall com object to add
- a new application to the firewall.
- .NOTES
- File Name : Add-FirewallApplication.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/03/add-firewallapplicationps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/aa366421%28VS.85%29.aspx
- .EXAMPLE
- At start of script, authorised applications are:
- Name Enabled
- ---- -------
- Delivery Manager Service True
- BitTornado True
- driver True
- driver True
- BitTorrent True
- DNA True
- Microsoft Office OneNote True
- After adding Notepad - here are authorised applications
- Name Enabled
- ---- -------
- Notepad True
- Delivery Manager Service True
- BitTornado True
- driver True
- driver True
- BitTorrent True
- DNA True
- Microsoft Office OneNote True
- #>
- ##
- # Start of script
- ##
- # Set constants
- $NET_FW_PROFILE_DOMAIN = 0
- $NET_FW_PROFILE_STANDARD = 1
- # Scope
- $NET_FW_SCOPE_ALL = 0
- # IP Version - ANY is the only allowable setting for now
- $NET_FW_IP_VERSION_ANY = 2
- # Create the firewall manager object.
- $fwMgr = new-object -com HNetCfg.FwMgr
- # Get the current profile for the local firewall policy.
- $profile = $fwMgr.LocalPolicy.CurrentProfile
- # Display applications available
- "At start of script, authorised applications are:"
- $profile.AuthorizedApplications | ft name, enabled -AutoSize
- # Create application to add to firewall
- $app = New-Object -com HNetCfg.FwAuthorizedApplication
- $app.ProcessImageFileName = "C:\windows\notepad.exe"
- $app.Name = "Notepad"
- $app.Scope = $NET_FW_SCOPE_ALL
- # Use either Scope or RemoteAddresses, but not both
- # $app.RemoteAddresses = "*"
- $app.IpVersion = $NET_FW_IP_VERSION_ANY
- $app.Enabled = $TRUE
- # Use this line if you want to add the app, but disabled.
- # $app.Enabled = FALSE
- $profile.AuthorizedApplications.Add($app)
- # Show applications after addition
- "After adding Notepad - here are authorised applications"
- $profile.AuthorizedApplications | ft name, enabled -AutoSize
- # End of script
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Sunday, 21 March 2010
Add-FireWallApplication.ps1
Saturday, 20 March 2010
Get-OutlookStores.ps1
- <#
- .SYNOPSIS
- This script uses the Outlook COM object to
- display the data stores in the current profile
- .DESCRIPTION
- This script creates an Outlook object, displays
- user information, and the stores currently
- attached to the profile.
- .NOTES
- File Name : Get-OutlookStores.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- PSH [C:\foo]: .\Get-OutlookStores.ps1'
- Current profile has the following configured accounts:
- Account Type User Name SMTP Address
- ------------ --------- ------------
- Microsoft Exchange Thomas.Lee Thomas.Lee@cookham.net
- thomas_lee@hotmail.com Thomas Lee (MSN) thomas_lee@hotmail.com
- Exchange Offile Folder Store:
- C:\Users\tfl\AppData\Local\Microsoft\Outlook\outlook0.ost
- PST Files
- Display Name File Path
- ------------ ---------
- Archive Folders C:\Users\tfl\AppData\Local\Microsoft\Outlook\archive.pst
- #>
- ##
- # Begin Script
- ##
- # Create Outlook object
- $Outlook = New-Object -ComObject Outlook.Application
- $stores = $Outlook.Session.Stores
- $accounts = $outlook.session.accounts
- # Basic information
- "Current profile has the following configured accounts:"
- $dn = @{label = "Account Type"; expression={$_.displayname}}
- $un = @{label = "User Name"; expression = {$_.username}}
- $sm = @{label = "SMTP Address"; expression = {$_.smtpaddress}}
- $accounts | Format-Table -AutoSize $dn,$un,$sm
- # Check number of stores > 0
- if ($stores.Count -le 0) {"No stores found"; return}
- # Outlook Off-Line folder store
- $ost = $stores | where{$_.filepath -match ".ost$"}
- if (!$ost)
- {
- "No Outlook Offline Folder store found"
- }
- else
- {
- "Exchange Offile Folder Store:"
- $ost | ft filepath -HideTableHeaders
- }
- # PST Files
- $pst = $stores | where {$_.filepath -match ".pst$"}
- if (!$pst)
- {
- "No PST files found"
- }
- else
- {
- "PST Files"
- $dn = @{label = "Display Name"; expression={$_.displayname}}
- $fn = @{label = "File Path"; expression={$_.filepath}}
- $pst | ft $dn,$fn
- }
- # End Script
Labels:
Outlook,
Outlook.Application,
PowerShell scripts,
Script,
scripts
Thursday, 18 March 2010
Enable-ICMP.ps1
- <#
- .SYNOPSIS
- This script Enables ICMP on the Standard Firewall profile.
- .DESCRIPTION
- This script creates a Firewall object then configures it.
- .NOTES
- File Name : Enable-ICMP.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://
- .EXAMPLE
- PSH [C:\foo]: . 'E:\PowerShellScriptLib\COM\HNetCfg.FwMgr\Enable-ICMP.ps1'
- AllowOutboundDestinationUnreachable : False
- AllowRedirect : False
- AllowInboundEchoRequest : False
- AllowOutboundTimeExceeded : False
- AllowOutboundParameterProblem : False
- AllowOutboundSourceQuench : False
- AllowInboundRouterRequest : False
- AllowInboundTimestampRequest : False
- AllowInboundMaskRequest : False
- AllowOutboundPacketTooBig : True
- After Script ran:
- AllowOutboundDestinationUnreachable : False
- AllowRedirect : False
- AllowInboundEchoRequest : True
- AllowOutboundTimeExceeded : False
- AllowOutboundParameterProblem : False
- AllowOutboundSourceQuench : False
- AllowInboundRouterRequest : False
- AllowInboundTimestampRequest : False
- AllowInboundMaskRequest : False
- AllowOutboundPacketTooBig : True
- #>
- ##
- # Start of script
- ##
- # Set strict mode
- Set-StrictMode -Version 2.0
- # Set Constants
- $NET_FW_PROFILE_DOMAIN = 0
- $NET_FW_PROFILE_STANDARD = 1
- # Create the firewall manager object.
- $fwMgr = New-Object -com HNetCfg.FwMgr
- # Get the current profile for the local firewall policy.
- $profile = $fwMgr.LocalPolicy.GetProfileByType($NET_FW_PROFILE_STANDARD)
- # Display current ICMP settings
- $Profile.IcmpSettings
- # Now set it to True
- $profile.IcmpSettings.AllowInboundEchoRequest = $True
- # Use this line if you want to disable the setting.
- #profile.IcmpSettings.AllowInboundEchoRequest = $FALSE
- # Display it again
- "After Script ran: "
- $Profile.IcmpSettings
- # End Script
Labels:
COM,
HNetCfg.FwMgr,
powershell,
PowerShell scripts
New-ExcelWorkbook.ps1
- <#
- .SYNOPSIS
- This script creates an Excel workbook using PowerShell
- .DESCRIPTION
- This script demonstrates manipulating Excell with PowerShell
- and the Excel.Application COM object.
- .NOTES
- File Name : New-ExcelWorkbook.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/03/new-excelworkbookps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/bb211359.aspx
- .EXAMPLE
- Run it and see one!
- #>
- ##
- # Start of Script
- ##
- # Then we create and save a sample worksheet
- # Create Excel object
- $excel = new-object -comobject Excel.Application
- # Make Excel visible
- $excel.visible = $true
- # Create a new workbook
- $workbook = $excel.workbooks.add()
- # The default workbook has three sheets, remove 2
- $S2 = $workbook.sheets | where {$_.name -eq "Sheet2"}
- $s3 = $workbook.sheets | where {$_.name -eq "Sheet3"}
- $s2.delete()
- $s3.delete()
- # Get sheet and update sheet name
- $s1 = $workbook.sheets | where {$_.name -eq 'Sheet1'}
- $s1.name = "PowerShell Sample"
- # Update workook properties
- $workbook.author = "Thomas Lee - tfl@psp.co.uk"
- $workbook.title = "Excel and PowerShell rock!"
- $workbook.subject = "Demonstrating the Power of PowerShell with Excel"
- # Next update some cells in the worksheet 'PowerShell Sample'
- $s1.range("A1:A1").cells="Cell a1"
- $s1.range("A2:A2").cells="A2"
- $s1.range("b1:b1").cells="Cell B1"
- $s1.range("b2:b2").cells="b2"
- # now make a sum
- $s1.range("E1:E2").cells="Widgets"
- $s1.range("E2:E2").cells=2
- $s1.range("E3:E3").cells=2
- $s1.range("E4:E4").cells=38
- $s1.range("D5:D5").cells="Total"
- $s1.range("E5:E5").cells.formula = "=sum(e2,E4)"
- # And save it away:
- $s1.saveas("c:\foo\xlsx3.xlsx")
- # end of script
Labels:
Excel,
Excel.Application,
powershell,
PowerShell scripts
Wednesday, 17 March 2010
Enable-FirewallPort2.ps1
- <#
- .SYNOPSIS
- This script creates a rule in the Windows Host Firewall.
- .DESCRIPTION
- This script creates a new firewall rule for
- port 80 over tcp (i.e. 80).
- .NOTES
- File Name : Enable-FirewallPort.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/03/enable-firewallport2ps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/aa366423%28VS.85%29.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Enable-FirewallPort.ps1
- Before Script Runs:
- Name IpVersion Protocol Port Scope RemoteAddresses Enabled
- ---- --------- -------- ---- ----- --------------- -------
- HTTPS 2 6 443 0 * True
- driver 2 6 8085 0 * True
- driver 2 6 8085 0 * True
- After Script Runs:
- Name IpVersion Protocol Port Scope RemoteAddresses Enabled
- ---- --------- -------- ---- ----- --------------- -------
- HTTP 2 6 80 0 * True
- HTTPS 2 6 443 0 * True
- driver 2 6 8085 0 * True
- driver 2 6 8085 0 * True
- #>
- ##
- # Start Script
- ##
- # Set Strict Mode
- Set-Strictmode -Version 2.0
- # Set Constants
- $NET_FW_IP_PROTOCOL_UDP = 17
- $NET_FW_IP_PROTOCOL_TCP = 6
- # Create the firewall manager object.
- $fwMgr = New-Object -COM HNetCfg.FwMgr
- # Get the current profile for the local firewall policy.
- $profile = $fwMgr.LocalPolicy.CurrentProfile
- # Display it
- "Before Script Runs:"
- $profile.GloballyOpenPorts | `
- ft name, ipversion, protocol, port, scope, remoteaddresses, enabled -auto
- # Now add Port 80
- $port = New-Object -COM HNetCfg.FWOpenPort
- $port.Name = "HTTP"
- $port.Protocol = $NET_FW_IP_PROTOCOL_TCP
- $port.Port = 80
- # If using RemoteAddresses, don't use Scope
- # "*" means Scope of Any. Other entries are ignored if this is specified.
- # "LocalSubnet" means Scope of Local Subnet. Can be used with other addresses as well.
- $port.RemoteAddresses = "*"
- # Use this line to scope the port to Local Subnet only
- #$port.RemoteAddresses = "LocalSubnet"
- #Use this line to scope the port to the specific IP 10.1.1.1, the specific subnet 12.5.0.0, and Local Subnet. Don't put spaces.
- #port.RemoteAddresses = "LocalSubnet,10.1.1.1/255.255.255.255,12.5.0.0/255.255.0.0"
- $port.Enabled = $TRUE
- #Use this line instead if you want to add the port, but disabled
- #port.Enabled = FALSE
- # Now add the port
- $profile.GloballyOpenPorts.Add($port)
- # Print Results
- " After Script Runs:"
- $profile = $fwMgr.LocalPolicy.CurrentProfile
- $profile.GloballyOpenPorts | `
- ft name, ipversion, protocol, port, scope, remoteaddresses, enabled -auto
- # End of script
Labels:
COM,
HNetCfg.FwMgr,
HNetCfg.FwOpenPort,
powershell,
PowerShell scripts
Tuesday, 16 March 2010
Set-ProgrammerAlias.ps1
- <#
- .SYNOPSIS
- This script creates a function to set aliases for all Cmdlets which omit the '-'
- .DESCRIPTION
- This script defines a function which uses Get-Command to find all
- cmdlets. For each of them, it then creates an alias which omits
- the "-". This function was oritinally written by Jeffrey Snover for
- Monad back in the day, but I updated it slightly for PowerShell V2.
- Updated after a comment to be a tad shorter.
- .NOTES
- File Name : Set-ProgrammerAlias.ps1
- Author : Jeffrey Snover - jsnover@microsoft.com
- Updated by : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- #>
- ##
- # Start of Script
- function Set-ProgrammerAlias {
- get-command -Com Cmdlet | % {set-alias $($_.Verb + $_.Noun) $_.Name}
- }
Friday, 5 March 2010
Enable-FWPort.ps1
- <#
- .SYNOPSIS
- This script enables then disables the SMTP port on a local system
- .DESCRIPTION
- This script first creates a FW object, then creates a port. The
- script then addes that port to the firewall rules. The script
- finally removes the port. The script also prints before/after
- results.
- .NOTES
- File Name : Enable-FWPort.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/aa366425(VS.85).aspx
- #>
- ##
- # Start of Script
- ##
- # Define Constants
- $NET_FW_IP_PROTOCOL_UDP = 17
- $NET_FW_IP_PROTOCOL_TCP = 6
- $NET_FW_SCOPE_ALL = 0
- # Create FW objecct
- $fwMgr = new-object -com HNetCfg.FwMgr
- # Get current profile
- $profile = $fwMgr.LocalPolicy.CurrentProfile
- # Display ports open:
- $profile.GloballyOpenPorts | ft name,port,enabled -auto
- # Create Port object
- $port = New-Object -com HNetCfg.FWOpenPort
- $port.Name = "SMTP"
- $port.Port = 25
- $port.Protocol = $NET_FW_IP_PROTOCOL_TCP
- $port.Scope = $NET_FW_SCOPE_ALL
- # Enable the port
- $port.Enabled = $True
- $profile.GloballyOpenPorts.Add($port)
- # Display results
- $profile.GloballyOpenPorts | ft name,port,enabled -auto
- # now remove the port
- $profile.GloballyOpenPorts.remove($port.port,$NET_FW_IP_PROTOCOL_TCP)
- # Display results
- $profile.GloballyOpenPorts | ft name,port,enabled -auto
- # End of script
Labels:
COM,
HNetCfg.FwMgr,
powershell,
PowerShell scripts,
Script,
scripts
Sunday, 28 February 2010
Using-Culture.ps1
Technorati Tags: PowerShell,PowerShell Script,System.Globalization.CultureInfo,System.Threading.Thread,culture
- <#
- .SYNOPSIS
- This script creates a function to run culture sensitive operations
- in some other culture.
- .DESCRIPTION
- The Using-Culture function gets the current culture, saves it, then
- runs the script block in the requested culture. The function then
- restores the culture. The script then ends with calls to
- Using-Culture to demonstrate its use.
- The script is based on a blog note at: http://blogs.msdn.com/powershell/archive/2006/04/25/583235.aspx.
- .NOTES
- File Name : Using-Culture.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/02/using-cultureps1.html
- .EXAMPLE
- run the script and see!
- #>
- # Defind the Using-Culture function
- Function Using-Culture {
- Param (
- [System.Globalization.CultureInfo]$culture = (throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}"),
- [ScriptBlock]$script= (throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}")
- )
- $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
- trap
- {
- [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
- }
- [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture
- Invoke-Command $script
- [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
- }
- ##
- # Now use the function
- ##
- # Here is an example of Using-Culture
- "Get-Date using ar-IQ"
- using-culture ar-IQ {get-date}
- ""
- # Parse ar-IQ formatted date into local culture
- "Parse IQ date to English and display"
- $IQD = "30 تشرين الثاني, 2005 09:01:38 ص"
- using-culture ar-IQ {$global:d=[DateTIme]::Parse($IQD)}
- $IQD
- $d
- ""
- # Now do it in German
- "Get-Date using de-de"
- using-culture de-de {get-date}
- ""
- "Parsing a german date into local"
- $ded="Mittwoch, 30. November 2005 09:02:29"
- using-culture de-de {$global:d=[DateTIme]::Parse($DED)}
- $ded
- $d
Saturday, 27 February 2010
PowerShell Master Class in Stockholm – Filling Up Quickly!
In a recent blog post, I mentioned the PowerShell Master Class I am running in Stockholm on March 9-11 2010 (see Http://Www.PowerShellMasterClass.Com for more details). I’m pleased to say the class is nearly full! I am looking forward to three great days with PowerShell!
Labels:
powershell,
stockholm,
www.powershellmasterclass.com
Saturday, 9 January 2010
Get-VMMemory
- <#
- .SYNOPSIS
- This script displays the virtual Memory assigned to Hyper-V
- VMs on a given host.
- .DESCRIPTION
- This script uses The Hyper-V module from Codeplex to first
- get all the VMs on a given server, then display the memory
- allocated to each VM.
- .NOTES
- File Name : get-VMMemory.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- and HyperV module from Codeplex
- http://pshyperv.codeplex.com
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- PSH [C:\Foo]: c:\foo\get-VMMemory.ps1 Foo21
- On Server Foo21 there are 2 VMs as follows:
- Virtual Machine Memory
- SQL-2008-R2-CTP 768
- Server-2008-R2 768
- .EXAMPLE
- PSH [C:\Foo]: "Server2" | c:\foo\get-VMMemory.ps1 Foo21
- On Server Server2 there are 3 VMs as follows:
- Virtual Machine Memory
- Server-2008-R2-1 768
- Server-2008-R2-2 768
- Server-2008-R2-3 768
- #>
- # Parameter is the machine to look at
- param (
- [Parameter(Position=0, Mandatory=$false,ValueFromPipeLine=$true)]
- [string] $Computer = "MyServer"
- )
- ##
- # Start of script
- ##
- # Import the module
- Import-Module HyperV
- # Get all the VMs on the target system
- $Vms = Get-Vm -Server $Computer
- # Now loop through the VMs and display memory allocated
- "On Server {0} there are {1} VMs as follows:" -f $Computer,$Vms.count
- "{0,-30} {1,10}" -f "Virtual Machine", "Memory"
- Foreach ($Vm in $Vms) {
- $Mem = Get-Vmmemory $Vm
- "{0,-30} {1,10}" -f $Vm.Elementname,$Mem.Virtualquantity
- }
- # End of script
Labels:
Codeplex,
Hyper-V,
PowerShell scripts,
PowerShell V2
Subscribe to:
Posts (Atom)