- <#
- .SYNOPSIS
- This script displays the versions of SQL Server running on a system
- .DESCRIPTION
- This script uses WMI to get the SQLServiceAdvancedProperty class from
- the ComputerManagement namespace to print out the versions. This
- script is an adaptation of the VBS script on MSDN.
- .NOTES
- File Name : Get-SQLServerVersion.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/ms186353.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-SQLVersion.ps1'
- You are running the following versions of SQL:
- Service Name Version
- ------------ -------
- MSSQL$MICROSOFT##SSEE 9.3.4035.00
- MSSQL$SQLEXPRESS 9.3.4035.00
- #>
- ##
- # Start of script
- ##
- # Get the versions of SQL from WMI
- $Versions = Get-WmiObject -Namespace root\Microsoft\SQLServer\computerManagement -Class SqlServiceAdvancedProperty | where {$_.SqlServiceType -eq 1 -and $_.PropertyName -eq "VERSION"}
- # Now display results
- "You are running the following versions of SQL:"
- "Service Name Version"
- "------------ -------"
- foreach ($version in $versions) {
- "{0} `t{1}" -f $version.servicename,$version.propertystrvalue
- }
- #End of script
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Friday, 7 August 2009
Get-SQLServerVersion.ps1
Labels:
powershell,
PowerShell scripts,
SMO,
SQL
Thursday, 6 August 2009
New-Ou.PS1
- <#
- .SYNOPSIS
- This script uses ADSI to add a new OU to a domain.
- .DESCRIPTION
- This script creates a pointer to the domain, then
- uses the Create method to create a new OU under
- the root of the domain.
- .NOTES
- File Name : New-Ou.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- PSH [C:\foo]: .\New-OU.PS1'
- Created OU: PowerShell
- distinguishedName : {OU=PowerShell,DC=cookham,DC=net}
- Path : LDAP://ou=PowerShell,dc=Cookham,dc=Net
- #>
- ##
- # Start of Script
- ##
- #Set variables
- $OuName = “PowerShell”
- $Domain = [ADSI]“LDAP://dc=Cookham,dc=Net”
- # Create the OU
- $Ou = $Domain.Create(”OrganizationalUnit”, “ou=” + $OuName)
- $Ou.SetInfo()
- # Display results
- "Created OU: {0}" -f $OUName
- $OU
Labels:
ADSI,
powershell,
PowerShell scripts
Wednesday, 5 August 2009
Get-SpecialFolders.ps1
- <#
- .SYNOPSIS
- This script lists the special folders enumerated in System.Environment.SpecialFolder
- .DESCRIPTION
- This script first enumerates the SpecialFolder Enum. for each member, the script
- then looks up, and displays, the value of that folder.
- .NOTES
- File Name : Get-SpecialFolders.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
- .EXAMPLE
- PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.Environment\Get-SpecialFolders.PS1'
- Folder Name Path
- ----------- -----------------------------------------------
- Desktop C:\Users\tfl\Desktop
- Programs C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
- Personal C:\Users\tfl\Documents
- Personal C:\Users\tfl\Documents
- Favorites C:\Users\tfl\NetHood\Favorites
- Startup C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
- Recent C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Recent
- SendTo C:\Users\tfl\AppData\Roaming\Microsoft\Windows\SendTo
- StartMenu C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Start Menu
- MyMusic C:\Users\tfl\Music
- DesktopDirectory C:\Users\tfl\Desktop
- MyComputer
- Templates C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Templates
- ApplicationData C:\Users\tfl\AppData\Roaming
- LocalApplicationData C:\Users\tfl\AppData\Local
- InternetCache C:\Users\tfl\AppData\Local\Microsoft\Windows\Temporary Internet Files
- Cookies C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Cookies
- History C:\Users\tfl\AppData\Local\Microsoft\Windows\History
- CommonApplicationData C:\ProgramData
- System C:\Windows\system32
- ProgramFiles C:\Program Files (x86)
- MyPictures C:\Users\tfl\Pictures
- CommonProgramFiles C:\Program Files (x86)\Common Files
- #>
- ##
- # Start of Script
- ##
- # Get the list of special folders
- $folders = [system.Enum]::GetValues([System.Environment+SpecialFolder])
- # Display these folders
- "Folder Name Path"
- "----------- -----------------------------------------------"
- foreach ($folder in $folders) {
- "{0,-22} {1,-15}" -f $folder,[System.Environment]::GetFolderPath($folder)
- }
- #End of Script
Get-Thursday
- <#
- .SYNOPSIS
- This script checks of a particular date in the past was a Thurday.
- .DESCRIPTION
- This script creates a DateTime object set for 1st May, 2003. The
- script then check to see if that day is a Thursday then displays
- the day of week for that date (which is a Thursday). This script
- is a copy of the MSDN sample, written in PowerShell.
- .NOTES
- File Name : Get-Thursday.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2009/08/get-thursday.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.dayofweek(VS.71).aspx
- .EXAMPLE
- PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.DateTime\get-thursday.ps1'
- Is Thursday the day of the week for 5/1/2003?: True
- The day of the week for 5/1/2003 is Thursday.
- #>
- ##
- # Start of script
- ##
- # Create a DateTime for the first of May, 2003.
- $dt = New-Object System.DateTime 2003, 5, 1
- # Now - is it Thursday?
- "Is Thursday the day of the week for {0:d}?: {1}" -f $dt,($dt.DayOfWeek -eq [system.DayOfWeek]::Thursday)
- "The day of the week for {0:d} is {1}." -f $dt, $dt.DayOfWeek
- # End of Script
Labels:
system.datetime,
system.dayofweek
Tuesday, 4 August 2009
Get-DNSAddress.ps1
- <#
- .SYNOPSIS
- This script uses the System.Net.Networkinginformation.Networknterface class
- to get all network interface detais and displays the DNS addresses configured.
- .DESCRIPTION
- This script first gets all the interface objects, then iterates throught them to
- get the DNS address(es) configured for each one.
- .NOTES
- File Name : Get-DNSAddress.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipaddressinformation(VS.85).aspx
- .EXAMPLE
- PSH [C:\foo]: . 'C:\Users\tfl\AppData\Local\Temp\Untitled11.ps1'
- NETGEAR FA311v2 PCI Adapter - Virtual Network
- DNS Servers ............................. : 10.10.1.101
- Broadcom NetXtreme Gigabit Ethernet
- DNS Servers ............................. : 10.10.1.101
- Software Loopback Interface 1
- DNS Servers ............................. : fec0:0:0:ffff::1%1
- DNS Servers ............................. : fec0:0:0:ffff::2%1
- DNS Servers ............................. : fec0:0:0:ffff::3%1
- isatap.cookham.net
- DNS Servers ............................. : 10.10.1.101
- #>
- ##
- # Start of Script
- ##
- # Get set of adapters
- $adapters = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()
- # For each adapter, print out DNS Server Addresses configured
- foreach ($adapter in $adapters) {
- $AdapterProperties = $Adapter.GetIPProperties()
- $dnsServers = $AdapterProperties.DnsAddresses
- if ($dnsServers.Count -gt 0){
- $adapter.Description
- foreach ($IPAddress in $dnsServers) {
- " DNS Servers ............................. : {0}" -f $ipaddress.IPAddressToString
- }
- }
- }
- # End of Script
Monday, 3 August 2009
Get-System.Environment.ps1
- <#
- .SYNOPSIS
- This script demonstrates the use of System.Environment
- .DESCRIPTION
- This script Uses a variety of the members of System.Environment and is
- a re-write of a MSDN Sample Script
- .NOTES
- File Name : Get-System.Environment
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.environment.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-System.Environment.ps1' foo1 foo2 foo3
- My system drive is C: and my system root is C:\Windows
- -- Environment members --
- CommandLine : "C:\Users\tfl\Desktop\psp\PowerShellPlus.exe"
- GetCommandLineArgs: C:\Users\tfl\Desktop\psp\PowerShellPlus.exe
- CurrentDirectory : C:\Users\tfl\Desktop\psp
- ExitCode : 0
- HasShutdownStarted: False
- MachineName : COOKHAM8
- NewLine :
- first line
- second line
- third line
- OSVersion : Microsoft Windows NT 6.0.6001 Service Pack 1
- StackTrace : ' at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
- at System.Environment.get_StackTrace()
- at getter(Object )
- at System.Management.Automation.DotNetAdapter.PropertyGet(PSProperty property)
- at System.Management.Automation.Adapter.BasePropertyGet(PSProperty property)
- at System.Management.Automation.PSProperty.GetAdaptedValue()
- at System.Management.Automation.PSProperty.get_Value()
- at System.Management.Automation.PropertyReferenceNode.GetValue(PSObject obj, Object property, ExecutionContext context)
- at System.Management.Automation.PropertyReferenceNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
- at System.Management.Automation.ExpressionNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
- at System.Management.Automation.ParseTreeNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
- at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
- at System.Management.Automation.StatementListNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
- at System.Management.Automation.ParseTreeNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
- at System.Management.Automation.ScriptCommandProcessor.ExecuteWithCatch(ParseTreeNode ptn, Array inputToProcess)
- at System.Management.Automation.ScriptCommandProcessor.RunClause(ParseTreeNode clause, Object dollarUnderbar, Object inputToProcess)
- at System.Management.Automation.ScriptCommandProcessor.Complete()
- at System.Management.Automation.CommandProcessorBase.DoComplete()
- at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input, Hashtable errorResults, Boolean enumerate)
- at System.Management.Automation.PipelineNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
- at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
- at System.Management.Automation.StatementListNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
- at System.Management.Automation.ParseTreeNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
- at System.Management.Automation.ScriptCommandProcessor.ExecuteWithCatch(ParseTreeNode ptn, Array inputToProcess)
- at System.Management.Automation.ScriptCommandProcessor.RunClause(ParseTreeNode clause, Object dollarUnderbar, Object inputToProcess)
- at System.Management.Automation.ScriptCommandProcessor.Complete()
- at System.Management.Automation.CommandProcessorBase.DoComplete()
- at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input, Hashtable errorResults, Boolean enumerate)
- at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecute(Array input, Hashtable errorResults)
- at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
- at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()
- at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
- at System.Threading.ExecutionContext.runTryCode(Object userData)
- at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
- at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
- at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
- at System.Threading.ThreadHelper.ThreadStart()'
- SystemDirectory : C:\Windows\system32
- TickCount : 98385431
- UserDomainName : COOKHAM
- UserInteractive : True
- UserName : tfl
- Version : 2.0.50727.3074
- WorkingSet : 49385472
- ExpandEnvironmentVariables:
- My system drive is C: and my system root is C:\Windows
- GetEnvironmentVariable:
- My temporary directory is C:\Users\tfl\AppData\Local\Temp.
- GetEnvironmentVariables:
- Name Value
- ---- -----
- Path C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\b...
- TEMP C:\Users\tfl\AppData\Local\Temp
- SESSIONNAME Console
- PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PSC1
- USERDOMAIN COOKHAM
- PROCESSOR_ARCHITECTURE x86
- ProgramW6432 C:\Program Files
- APPDATA C:\Users\tfl\AppData\Roaming
- windir C:\Windows
- LOCALAPPDATA C:\Users\tfl\AppData\Local
- CommonProgramW6432 C:\Program Files\Common Files
- TMP C:\Users\tfl\AppData\Local\Temp
- USERDNSDOMAIN COOKHAM.NET
- USERPROFILE C:\Users\tfl
- ProgramFiles C:\Program Files (x86)
- CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
- FP_NO_HOST_CHECK NO
- HOMEPATH \Users\tfl
- COMPUTERNAME COOKHAM8
- CLASSPATH .;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip
- PROCESSOR_ARCHITEW6432 AMD64
- ProgramData C:\ProgramData
- NUMBER_OF_PROCESSORS 8
- PROCESSOR_IDENTIFIER Intel64 Family 6 Model 23 Stepping 6, GenuineIntel
- SystemDrive C:
- SystemRoot C:\Windows
- ComSpec C:\Windows\system32\cmd.exe
- LOGONSERVER \\COOKHAM1
- ProgramFiles(x86) C:\Program Files (x86)
- PSMODULEPATH C:\Users\tfl\Documents\WindowsPowerShell\Modules;C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules
- CommonProgramFiles C:\Program Files (x86)\Common Files
- PROCESSOR_LEVEL 6
- PROCESSOR_REVISION 1706
- QTJAVA C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip
- ALLUSERSPROFILE C:\ProgramData
- VS90COMNTOOLS C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\
- PUBLIC C:\Users\Public
- USERNAME tfl
- OS Windows_NT
- HOMEDRIVE C:
- GetFolderPath: C:\Windows\system32
- GetLogicalDrives: C:\, D:\, E:\, F:\, G:\, H:\, I:\, J:\, K:\, L:\, Q:\, X:\
- .PARAMETER
- None, but if any are provided, they are displayed above
- #>
- ##
- # Start of Script
- ##
- # Setup two variables for later use
- [String] $str;
- [string] $nl = [System.Environment]::NewLine
- # Now display members of the environment class
- "-- Environment members --"
- # Assume: You Invoke this sample with an arbitrary set of command line arguments.
- "CommandLine : {0}" -f [system.environment]::CommandLine
- $arguments = [System.Environment]::GetCommandLineArgs()
- "GetCommandLineArgs: {0}" -f [system.string]::join(", ", $arguments)
- # <-- Keep this information secure! -->
- "CurrentDirectory : {0}" -f [System.Environment]::CurrentDirectory
- "ExitCode : {0}" -f [System.Environment]::ExitCode
- "HasShutdownStarted: {0}" -f [System.Environment]::HasShutdownStarted
- # <-- Keep this information secure! -->
- "MachineName : {0}" -f [System.Environment]::MachineName
- "NewLine : {0} first line{0} second line{0} third line" -f [System.Environment]::NewLine
- "OSVersion : {0}" -f [System.Environment]::OSVersion.ToString()
- "StackTrace : '{0}'" -f [System.Environment]::StackTrace
- # <-- Keep this information secure! -->
- "SystemDirectory : {0}" -f [System.Environment]::SystemDirectory
- "TickCount : {0}" -f [System.Environment]::TickCount
- # <-- Keep this information secure! -->
- "UserDomainName : {0}" -f [System.Environment]::UserDomainName
- "UserInteractive : {0}" -f [System.Environment]::UserInteractive
- # <-- Keep this information secure! -->
- "UserName : {0}" -f [System.Environment]::UserName
- "Version : {0}" -f [System.Environment]::Version.ToString()
- "WorkingSet : {0}" -f [System.Environment]::WorkingSet
- # No example for exit(exitCode) because doing so would terminate this example.
- # <-- Keep this information secure! -->
- $query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";
- $str = [System.Environment]::ExpandEnvironmentVariables($query)
- "ExpandEnvironmentVariables: {0} {1}" -f $nl,$str
- "GetEnvironmentVariable: {0} My temporary directory is {1}." -f $nl,[System.Environment]::GetEnvironmentVariable("TEMP")
- ""
- "GetEnvironmentVariables: "
- [System.Environment]::GetEnvironmentVariables()
- "GetFolderPath: {0}" -f [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::System)
- [String[]] $drives = [System.Environment]::GetLogicalDrives()
- "GetLogicalDrives: {0}" -f [System.String]::Join(", ", $drives)
- #End of Script
Sunday, 2 August 2009
Convert-Double.ps1
- <#
- .SYNOPSIS
- This script is an updated MSDN sample showing converstion to int32, boolean, string
- and char. The sample is written using PowerShell
- .DESCRIPTION
- This script
- .NOTES
- File Name : convert-double.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.convert.aspx
- .EXAMPLE
- PSH [C:\foo]: .\convert-double.ps1'
- Converting 23.15 to an int32 = 23
- Converting 23.15 to an Boolean = True
- Converting 23.15 to a String = 23.15
- Converting 1st char of "23.15" to a char = 2
- #>
- ##
- # Start of sample
- ##
- # Create double to convert
- [system.double] $dNumber = 23.15
- # convert to int32
- [int32] $iNumber = [System.Convert]::ToInt32($dNumber)
- "Converting $dnumber to an int32 = $inumber"
- # convert to bool
- [bool] $bNumber = [System.Convert]::ToBoolean($dNumber);
- "Converting $dnumber to an Boolean = $bNumber"
- # convert to string
- [string] $strNumber = [System.Convert]::ToString($dNumber)
- "Converting $dnumber to a String = $strNumber"
- # convert a single char in the string to a char
- [char] $chrNumber = [System.Convert]::ToChar($strNumber[0]);
- "Converting 1st char of `"$strnumber`" to a char = $chrNumber"
- # End of script
Subscribe to:
Posts (Atom)