- <#
- .SYNOPSIS
- This script creates and displays a BigInteger.
- .DESCRIPTION
- This script is a rewrite of an MSDN sample.
- .NOTES
- File Name : New-BigInteger.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .NET Framework 4
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/07/new-bigintegerps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx
- .EXAMPLE
- PSH [c:\foo]: .\Get-BigInteger.ps1
- Big Integer from 179032.6541:
- 179,032
- Big Integer from 1234934157136952:
- 1,234,934,157,136,952
- #>
- # Add the .NET Version 4 System.Numerics.DLL
- Add-Type -Path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Numerics.dll"
- # Create first big integer then display it nicely
- $BigIntFromDouble = New-Object System.Numerics.BigInteger 179032.6541
- "Big Integer from 179032.6541:"
- $BigIntFromDouble.ToString("N0")
- #Create second big integer then display it nicely
- $BigIntFromInt64 = New-object System.Numerics.BigInteger 1234934157136952
- "Big Integer from 1234934157136952:"
- $Bigintfromint64.tostring("N0")
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Saturday, 31 July 2010
New-BigInteger.ps1
Labels:
.Net 4.0,
BigInteger,
powershell,
PowerShell scripts,
System.Numerics
Get-WmiClassDescription.ps1
- <#
- .SYNOPSIS
- This script gets and displays the description of a WMI Class.
- .DESCRIPTION
- This script takes a WMI Class name as a parameter. The script
- then gets the class's description from the CIM repository and
- displays it. Based on a PowerShell.Com tip of the day!
- .NOTES
- File Name : Get-WmiClassDescription.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-WmiClassDescription.ps1
- Description for WMI Class Win32_ComputerSystem:
- The Win32_ComputerSystem class represents a computer system operating in a Win32 environment.
- .EXAMPLE
- PSH [C:\foo]: . \Get-WmiClassDescription.ps1 win32_bios
- Description for WMI Class win32_bios:
- The Win32_BIOS class represents the attributes of the computer system's basic input/output services
- (BIOS) that are installed on the computer.
- #>
- param (
- [string] $WMIClassName = "Win32_ComputerSystem"
- )
- # Get WMI class from class name
- $class = [wmiclass]$wmiclassname
- # Now get then print the class description
- # First use ammended qualifiers to get description
- $class.psbase.Options.UseAmendedQualifiers = $true
- "Description for WMI Class {0}:" -f $WmiClassName
- ($class.psbase.qualifiers["description"]).Value
Labels:
powershell,
PowerShell scripts,
PowerShell V2,
wmi
Sunday, 18 July 2010
Get-Performance.ps1
- <#
- .SYNOPSIS
- This script displays basic performance information for a computer
- .DESCRIPTION
- This script calls Get-Counter on a computer to obtain default
- performance counters. These are obtained and displayed. The script
- takes a parameter which is the host name to display.
- .NOTES
- File Name : Get-Performance.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-Performance.ps1
- Basic Performance for system: Cookham8
- Network Bytes Per Second (2 adapters)
- broadcom netxtreme gigabit etherne 0.00 kbps
- netgear fa311v2 pci adapter 8.15 kbps
- CPU Usage 1.93 %
- Committed Bytes 30.61 mb
- Cache Faults 0.00 p/s
- Percent Disk Time 34.15 %
- Disk Queue length 0.00
- .EXAMPLE
- PSH [C:\foo]: .\Get-Performance cookham2
- Basic Performance for system: cookham2
- Network Bytes Per Second (6 adapters)
- public 25.31 kbps
- broadcom netxtreme 57xx gigabit co 26.20 kbps
- internal 0.16 kbps
- isatap.cookham.net 0.00 kbps
- teredo tunneling pseudo-interface 0.00 kbps
- isatap.{0b3c0adc-3418-4842-8b4e-cf 0.00 kbps
- CPU Usage 0.39 %
- Committed Bytes 45.80 mb
- Cache Faults 0.00 p/s
- Percent Disk Time 0.00 %
- Disk Queue length 0.00
- .PARAMETER comp
- The name of the computer to display. Default is localhost.
- #>
- param (
- [string] $comp = "localhost"
- )
- # Get counters
- $counter = Get-Counter -computername $comp
- #Display results
- "Basic Performance for system: {0}" -f $comp
- # Network interface
- $c= $counter.countersamples | where {$_.path -match "network Interface"}
- " Network Bytes Per Second ({0} adapters)" -f $c.count
- foreach ($ni in $c){
- $nin = if ($ni.instancename.length -le 34) {$ni.InstanceName}
- else {$ni.instancename.substring(0,34)}
- " {0,-35} {1,12:n2} kbps" -f $nin,$($ni.cookedvalue/1kb)
- }
- # CPU Usage
- $c= $counter.countersamples | where {$_.path -match "processor time"}
- " CPU Usage {0,30:n2} %" -f $c.cookedvalue
- # Memory
- $cb = ($counter.countersamples | where {$_.Path -match "bytes in use"}).cookedvalue
- $cf = ($counter.countersamples | where {$_.Path -match "cache"}).cookedvalue
- " Committed Bytes {0,30:n2} mb" -f $cb
- " Cache Faults {0,30:n2} p/s" -f $cf
- # Disk
- $dt = ($counter.countersamples | where {$_.Path -match "disk time"}).cookedvalue
- $dql = ($counter.countersamples | where {$_.Path -match "queue"}).cookedvalue
- " Percent Disk Time {0,30:p2}" -f $dt
- " Disk Queue Length {0,30:n2}" -f $dql
Get-DriveInfo.ps1
- <#
- .SYNOPSIS
- This script gets and displays basic information about drives on a local system.
- .DESCRIPTION
- This script uses the System.Io.DriveInfo's GetDrives method to get drive
- info which is then displayed. This is a re-implementation of an MSDN
- sample.
- .NOTES
- File Name : Get-DriveInfo.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/07/get-driveinfops1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-Driveinfo.ps1
- Drive C:\
- File type: Fixed
- Volume label:
- File system: NTFS
- Available space to current user: 6.75 gb
- Total available space: 6.75 gb
- Total size of drive: 48.828 gb
- Drive D:\
- File type: Fixed
- Volume label:
- File system: FAT32
- Available space to current user: 1.81 gb
- Total available space: 1.81 gb
- Total size of drive: 2.003 gb
- <ETC...>
- #>
- # Get Drive Info
- $allDrives = [system.IO.DriveInfo]::GetDrives()
- # Now display details
- foreach ($d in $allDrives) {
- "Drive {0}" -f $d.Name
- " File type: {0}" -f $d.DriveType
- if ($d.IsReady) {
- " Volume label: {0}" -f $d.VolumeLabel
- " File system: {0}" -f $d.DriveFormat
- $fs = $d.AvailableFreeSpace/1gb
- $tfs = $d.TotalFreeSpace/1gb
- $TS = $d.TotalSize/1gb
- " Available space to current user:{0, 15:n2} gb" -f $fs
- " Total available space: {0, 15:n2} gb" -f $tfs
- " Total size of drive: {0, 15:n3} gb" -f $ts
- }
- }
Friday, 16 July 2010
Set-SQLServerOption.ps1
- <#
- .SYNOPSIS
- This script sets options on a SQL Server using SMO
- .DESCRIPTION
- This script first loads the SQL cmdlet and provider snapin and
- displays information about the SQL Server. The script then sets
- two server options and alters to database to persist the changes.
- .NOTES
- File Name : Set-SQLServerOption.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]: .\Set-SQLServerOption.ps1
- Network Name SQL1
- Instance Name
- OS Version 6.1 (7600)
- SQL Edition Enterprise Edition (64-bit)
- Settings State Existing
- #>
- # Load the SMO Objects
- $null = Add-PSSnapIn SqlServerCmdletSnapin100 -erroraction silentlycontinue
- $null = Add-PSSnapIn SqlServerProviderSnapin100 -erroraction silentlycontinue
- # Set the path context to the local, default instance of SQL Server.
- CD sqlserver:\sql\localhost\
- $srv = get-item default
- # Display information about the instance of SQL Server and settings state
- "Network Name {0}" -f $srv.NetName
- "Instance Name {0}" -f $srv.InstanceName
- "OS Version {0}" -f $srv.Information.OSVersion
- "SQL Edition {0}" -f $srv.Edition
- "Settings State {0}" -f $srv.Settings.State.ToString()
- # Modify LoginMode settings
- $srv.Settings.LoginMode = [Microsoft.SqlServer.Management.SMO.ServerLoginMode]::Integrated
- # Modify settings specific to the current connection in UserOptions
- $srv.UserOptions.AbortOnArithmeticErrors = $true
- # Run the Alter method to make the changes on the instance of SQL Server
- $srv.Alter()
Labels:
powershell,
PowerShell scripts,
SMO,
SQL
Thursday, 15 July 2010
Copy-File2.ps1
- <#
- .SYNOPSIS
- This script creates a file, then reads it and displays the output.
- .DESCRIPTION
- This script implements the MSDN sample for this page. It first creates a file (assuming the file
- does not already exist) and writes three lines to it. The script then opens the file, reads each
- line and displays it.
- .NOTES
- File Name : Copy-File2.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/07/copy-file2ps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.io.file.aspx
- http://msdn.microsoft.com/en-us/library/system.io.file.createtext.aspx
- http://msdn.microsoft.com/en-us/library/system.io.file.opentext.aspx
- .EXAMPLE
- PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.Io.File\Copy-File2.PS1'
- Hello
- And
- Welcome
- #>
- # Set name of file to use for this script
- $path = "c:\foo\MyTest.txt"
- # Create the file if it does not already exist
- if (![System.IO.File]::Exists($path)) {
- $sw = [System.Io.File]::CreateText($path)
- $sw.WriteLine("Hello")
- $sw.WriteLine("And")
- $sw.WriteLine("Welcome")
- }
- # Open the file to read from and set $s to an empty string
- $sr = [System.Io.File]::OpenText($path)
- $s = "";
- # Loop through the file, line at a time and display the output
- while (($s = $sr.ReadLine()) -ne $null) {
- $s
- }
- # And close the reader/writer
- $sw.Close()
- $sr.Close()
Labels:
powershell,
PowerShell scripts,
PowerShell V2,
System.Io.File
Wednesday, 14 July 2010
Get-AssemblyDetails.ps1
- #
- .SYNOPSIS
- This script demonstrates the Assembly.GetType method.
- .DESCRIPTION
- This script creates an int32, then calls into .NET to find
- the assembly that the int32 class comes from. The assembly
- details are then output. This script is a re-work of an MSDN
- code sample.
- .NOTES
- File Name : Get-AssemblyDetails.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/system.reflection.assembly.getassembly.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-AssemblyDetails.ps1'
- CodeBase=: file:///C:/Windows/Microsoft.NET/Framework64/v2.0.50727/mscorlib.dll
- #>
- # Set the Type instance to the target class type.
- $Integer1 = New-Object System.Int32
- $Type1 = $Integer1.GetType();
- # Instantiate an Assembly class to the assembly housing the Integer type.
- $SampleAssembly = [System.Reflection.Assembly]::GetAssembly($Integer1.GetType())
- # Gets the location of the assembly using file: protocol.
- "CodeBase=: {0}" -f $SampleAssembly.CodeBase
Labels:
code,
powershell,
PowerShell scripts,
Script,
system.reflection.assembly
Tuesday, 13 July 2010
Say-HelloWorld.ps1
- <#
- .SYNOPSIS
- This script uses the Speech API to say "Hello World"
- .DESCRIPTION
- This script first loads the Speech API, then creates a
- SpeechSynthesiser object. It uses this object's Speak method
- to speak the time honoured phrase "hello world."
- .NOTES
- File Name : Say-HelloWorld.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/ms586901.aspx
- .EXAMPLE
- Left as an exercise for the reader.
- #>
- # First load the dll
- [System.Reflection.Assembly]::LoadWithPartialName("System.Speech") | out-null
- $spk = New-Object system.Speech.Synthesis.SpeechSynthesizer
- # Now say "Hello world"
- $spk.Speak("Hello world")
Monday, 12 July 2010
Get-InstalledVoice.ps1
- <#
- .SYNOPSIS
- This script displays the installed Speech Synthesiser voices
- .DESCRIPTION
- This script first loads the System.Speech DLL, then
- creates a new SpeechSynthesizer object. It uses this
- object to enumerate the installed speech voices.
- .NOTES
- File Name : Get-InstalledVoice.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/system.speech.synthesis.installedvoice%28VS.90%29.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-InstalledVoice.ps1
- Gender : Female
- Age : Adult
- Name : Microsoft Anna
- Culture : en-US
- Id : MS-Anna-1033-20-DSK
- Description : Microsoft Anna - English (United States)
- SupportedAudioFormats : {System.Speech.AudioFormat.SpeechAudioFormatInfo}
- AdditionalInfo : {[Age, Adult], [AudioFormats, 18], [Gender, Female], [Language, 409]...}
- #>
- #
- # First load the dll
- $r = [system.Reflection.Assembly]::LoadWithPartialName("system.speech")
- # Create a speech synthesizer object and enumerate voices
- $spk = New-Object system.Speech.Synthesis.SpeechSynthesizer
- $spk.GetInstalledVoices() | %{$_.voiceinfo}
Subscribe to:
Posts (Atom)