- <#
- .Synopsis
- Creates a function to set PowerShell as GUI in Server 2012
- .DESCRIPTION
- The function in this script sets PowerShell as the
- default shell in Server 2012. When the server is rebooted,
- it runs PowerShell.exe by default. When PowerShell starts, it
- displays the $PSVersionTable variable.
- .NOTES
- File Name : Set-PowerShellAsGui.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : Server 2012
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Left as an exercise to the reader
- #>
- Function Set-PowerShellAsShell {
- [CmdletBinding()]
- Param (
- [switch] $Reboot = $false
- )
- # Create Registry Path variable
- $RegPath = "Microsoft.PowerShell.Core\Registry::"
- $RegPath += "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\"
- $RegPath += "Windows NT\CurrentVersion\winlogon"
- # Create splatted parameter hash table
- $parm = @{Path = $regpath} # key
- $parm += @{Name = 'Shell'} # value name
- $parm += @{Value = 'PowerShell.exe –NoExit
-Command "$psversiontable"'} # value’s value - # Set Registry value entry
- Set-ItemProperty @parm
- # And restart to see PowerShell
- if ($Reboot) {Restart-Computer -confirm}
- }
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Showing posts with label PowerShell V2. Show all posts
Showing posts with label PowerShell V2. Show all posts
Wednesday, 3 October 2012
Set-PowerShellAsShell
Labels:
powershell,
PowerShell scripts,
PowerShell V2,
PowerShell v3,
Server,
Server2012,
ServerCore
Tuesday, 29 March 2011
New-Credential.ps1
- <#
- .SYNOPSIS
- A function to create a credential object from script.
- .DESCRIPTION
- Enables you to create a credential objects from stored details.
- .NOTES
- File Name : New-Credential.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2011/03/new-credentialps1.html
- .PARAMETER UserId
- The userid in the form of "domain\user"
- .PARAMETER Password
- The password for this user
- .EXAMPLE
- New-Credential contoso\administrator Pa$$w0rd
- #>
- function New-Credential {
- param (
- [string] $Userid,
- [string] $Pwd
- )
- # Create the credential
- $spwd = ConvertTo-SecureString -AsPlainText $pwd -Force
- $cred = New-Object System.Management.Automation.PSCredential $userid,$spwd
- # Now return it to the caller
- return $cred
- }
- # Call the function to demostrate example
- New-Credential "contoso\administrator" "Pa$$w0rd"
Labels:
credential,
powershell,
PowerShell V2,
security
Thursday, 23 December 2010
Set-IseThemeVim.ps1
- <#
- .SYNOPSIS
- This script sets an ISE Theme to similar to the old VIM editor.
- .DESCRIPTION
- This script sets the key values in $PsIse.Options to values consistent
- with the VIM editor, beloved by many, particularly on the Powershell
- product team. This script is based on Davis Mohundro's blog post
- (http://bit.ly/iib5IM), updated for RTM of PowerShell V2.0. See also
- .NOTES
- File Name : Set-ISEThemeVIM.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0 (ISE only)
- .LINK
- This script posted to:
- http://pshscriptsbog.blogspot.com
- http://bit.ly/iaA2iX
- .EXAMPLE
- This script when run resets colours on key panes, including
- colourising tokens in the script pane. Try it and see it...
- #>
- # PowerShell ISE version of the VIM blackboard theme at
- # http://www.vim.org/scripts/script.php?script_id=2280
- # Set font name and size
- $psISE.Options.FontName = 'Courier New'
- $psISE.Options.FontSize = 16
- # Set colours for output pane
- $psISE.Options.OutputPaneBackgroundColor = '#FF000000'
- $psISE.Options.OutputPaneTextBackgroundColor = '#FF000000'
- $psISE.Options.OutputPaneForegroundColor = '#FFFFFFFF'
- # Set colours for command pane
- $psISE.Options.CommandPaneBackgroundColor = '#FF000000'
- # Set colours for script pane
- $psise.options.ScriptPaneBackgroundColor ='#FF000000'
- # Set colours for tokens in Script Pane
- $psISE.Options.TokenColors['Command'] = '#FFFFFF60'
- $psISE.Options.TokenColors['Unknown'] = '#FFFFFFFF'
- $psISE.Options.TokenColors['Member'] = '#FFFFFFFF'
- $psISE.Options.TokenColors['Position'] = '#FFFFFFFF'
- $psISE.Options.TokenColors['GroupEnd'] = '#FFFFFFFF'
- $psISE.Options.TokenColors['GroupStart'] = '#FFFFFFFF'
- $psISE.Options.TokenColors['LineContinuation'] = '#FFFFFFFF'
- $psISE.Options.TokenColors['NewLine'] = '#FFFFFFFF'
- $psISE.Options.TokenColors['StatementSeparator'] = '#FFFFFFFF'
- $psISE.Options.TokenColors['Comment'] = '#FFAEAEAE'
- $psISE.Options.TokenColors['String'] = '#FF00D42D'
- $psISE.Options.TokenColors['Keyword'] = '#FFFFDE00'
- $psISE.Options.TokenColors['Attribute'] = '#FF84A7C1'
- $psISE.Options.TokenColors['Type'] = '#FF84A7C1'
- $psISE.Options.TokenColors['Variable'] = '#FF00D42D'
- $psISE.Options.TokenColors['CommandParameter'] = '#FFFFDE00'
- $psISE.Options.TokenColors['CommandArgument'] = '#FFFFFFFF'
- $psISE.Options.TokenColors['Number'] = '#FF98FE1E'
Labels:
ISE,
Powershell ISE,
PowerShell V2
Set-ISEThemeDefault.ps1
- <#
- .SYNOPSIS
- This script resets the ISE to default ‘theme’.
- .DESCRIPTION
- This script sets the key values in $PsIse.Options to their default
- options in $Psise.Options.DefaultOptions. This script is useful if you
- are playing with ISE options and don't quite get it right - just run this
- script to set things back to default.
- .NOTES
- File Name : Set-ISEThemeDefault.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0 (ISE only)
- .LINK
- This script posted to:
- http://bit.ly/gJpu2W
- .EXAMPLE
- This script when run just resets the theme to the default. To view, run from
- the ISE after changing options!
- #>
- # Set Basic options
- $psise.options.SelectedScriptPaneState = $psise.options.DefaultOptions.SelectedScriptPaneState
- $psise.options.ShowToolBar = $psise.options.DefaultOptions.ShowToolBar
- $psise.options.FontSize = $psise.options.DefaultOptions.FontSize
- $psise.options.Fontname = $psise.options.DefaultOptions.Fontname
- # set colouring
- $psise.options.ErrorForegroundColor = $psise.options.DefaultOptions.ErrorForegroundColor
- $psise.options.ErrorBackgroundColor = $psise.options.DefaultOptions.ErrorBackgroundColor
- $psise.options.WarningForegroundColor = $psise.options.DefaultOptions.WarningForegroundColor
- $psise.options.WarningBackgroundColor = $psise.options.DefaultOptions.WarningBackgroundColor
- $psise.options.VerboseForegroundColor = $psise.options.DefaultOptions.VerboseForegroundColor
- $psise.options.VerboseBackgroundColor = $psise.options.DefaultOptions.VerboseBackgroundColor
- $psise.options.DebugBackgroundColor = $psise.options.DefaultOptions.DebugBackgroundColor
- $psise.options.DebugForegroundColor = $psise.options.DefaultOptions.DebugForegroundColor
- $psise.options.OutputPaneBackgroundColor = $psise.options.DefaultOptions.OutputPaneBackgroundColor
- $psise.options.OutputPaneTextBackgroundColor = $psise.options.DefaultOptions.OutputPaneTextBackgroundColor
- $psise.options.OutputPaneForegroundColor = $psise.options.DefaultOptions.OutPutPaneForegroundColor
- $psise.options.CommandPaneBackgroundColor = $psise.options.DefaultOptions.CommandPaneBackgroundColor
- $psise.options.ScriptPaneBackgroundColor = $psise.options.DefaultOptions.ScriptPaneBackgroundColor
- $psise.options.ScriptPaneForegroundColor = $psise.options.DefaultOptions.ScriptPaneForegroundColor
- # More options
- $psise.options.ShowWarningForDuplicateFiles = $psise.options.DefaultOptions.ShowWarningForDuplicateFiles
- $psise.options.ShowWarningBeforeSavingOnRun = $psise.options.DefaultOptions.ShowWarningBeforeSavingOnRun
- $psise.options.UseLocalHelp = $psise.options.DefaultOpitons.UseLocalHelp
- $psise.options.CommandPaneUp = $psise.options.DefaultOptions.CommandPaneUp
- # Reset Tokens Colors
- $psISE.Options.TokenColors['Attribute'] = $psISE.Options.DefaultOptions.TokenColors['Attribute']
- $psISE.Options.TokenColors['Command'] = $psISE.Options.DefaultOptions.TokenColors['Command']
- $psISE.Options.TokenColors['CommandArgument'] = $psISE.Options.DefaultOPtions.TokenColors['CommandArgument']
- $psISE.Options.TokenColors['CommandParameter'] = $psISE.Options.DefaultOptions.TokenColors['CommandParameter']
- $psISE.Options.TokenColors['Comment'] = $psISE.Options.DefaultOptions.TokenColors['Comment']
- $psISE.Options.TokenColors['GroupEnd'] = $psISE.Options.DefaultOptions.TokenColors['GroupEnd']
- $psISE.Options.TokenColors['GroupStart'] = $psISE.Options.DefaultOptions.TokenColors['GroupStart']
- $psISE.Options.TokenColors['Keyword'] = $psISE.Options.DefaultOptions.TokenColors['Keyword']
- $psISE.Options.TokenColors['LineContinuation'] = $psISE.Options.DefaultOptions.TokenColors['LineContinuation']
- $psISE.Options.TokenColors['LoopLabel'] = $psISE.Options.DefaultOptions.TokenColors['LoopLabel']
- $psISE.Options.TokenColors['Member'] = $psISE.Options.DefaultOptions.TokenColors['Member']
- $psISE.Options.TokenColors['NewLine'] = $psISE.Options.DefaultOptions.TokenColors['NewLine']
- $psISE.Options.TokenColors['Number'] = $psISE.Options.DefaultOPtions.TokenColors['Number']
- $psISE.Options.TokenColors['Position'] = $psISE.Options.DefaultOptions.TokenColors['Position']
- $psISE.Options.TokenColors['StatementSeparator'] = $psISE.Options.DefaultOptions.TokenColors['StatementSeparator']
- $psISE.Options.TokenColors['String'] = $psISE.Options.DefaultOptions.TokenColors['String']
- $psISE.Options.TokenColors['Type'] = $psISE.Options.DefaultOptions.TokenColors['Type']
- $psISE.Options.TokenColors['Unknown'] = $psISE.Options.DefaultOptions.TokenColors['Unknown']
- $psISE.Options.TokenColors['Variable'] = $psISE.Options.DefaultOptions.TokenColors['Variable']
- # Done
Labels:
ISE,
Powershell ISE,
PowerShell V2
Thursday, 7 October 2010
Get-WMINameSpace.ps1
- <#
- .SYNOPSIS
- This script displays all the WMI namespaces within a Windows system
- .DESCRIPTION
- This script uses Get-WMIObject to retrieve the names of all the namespaces
- within a system.
- .NOTES
- File Name : Get-WMINameSpace.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/10/get-wminamespaceps1.html
- .EXAMPLE
- PSH [C:\foo]: .\Get-WMINameSpace.ps1
- 37 Namespaces on: Cookham8
- Namespace
- ---------
- ROOT
- ROOT\aspnet
- ROOT\CIMV2
- ROOT\CIMV2\Security
- ROOT\CIMV2\Security\MicrosoftTpm
- ... {Remainder of list snipped to save space on this page}
- #>
- # Set computer name
- $comp = "."
- # Get the name spaces on the local computer, and the local computer name
- $Namespace = get-wmiobject __namespace -namespace 'root' -list -recurse -computer $comp
- $hostname = hostname
- # Display number of and names of the namespaces
- "{0} Namespaces on: {1}" -f $namespace.count, $hostname
- $NameSpace| sort __namespace | Format-Table @{Expression = "__Namespace"; Label = "Namespace"}
Labels:
namespace,
powershell,
PowerShell scripts,
PowerShell V2,
wmi
Friday, 24 September 2010
New-Task.ps1
- <#
- .SYNOPSIS
- This script creates a scheduled task object.
- .DESCRIPTION
- This script re-implements an MSDN sample using PowerShell
- .NOTES
- File Name : New-Task.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 tot:
- http://msdn.microsoft.com/en-us/library/aa383665%28VS.85%29.aspx
- .EXAMPLE
- PSH [C:\foo]: .\New-Task.ps1
- Time Now : 9/24/2010 12:43:47 PM
- Task startTime : 2010-09-24T12:44:17
- Task endTime : 2010-09-24T12:48:47
- Task definition created. About to submit the task...
- Name : Test TimeTrigger
- Path : Test TimeTrigger
- State : 3
- Enabled : True
- LastRunTime : 12/30/1899 12:00:00 AM
- LastTaskResult : 1
- NumberOfMissedRuns : 0
- NextRunTime : 9/24/2010 12:44:17 PM
- Definition : System.__ComObject
- Xml : <?xml version="1.0" encoding="UTF-16"?>
- <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
- <RegistrationInfo>
- <Author>Thomas Lee</Author>
- <Description>Start notepad at a certain time</Description>
- </RegistrationInfo>
- <Triggers>
- <TimeTrigger id="TimeTriggerId">
- <StartBoundary>2010-09-24T12:44:17</StartBoundary>
- <EndBoundary>2010-09-24T12:48:47</EndBoundary>
- <ExecutionTimeLimit>PT5M</ExecutionTimeLimit>
- <Enabled>true</Enabled>
- </TimeTrigger>
- </Triggers>
- <Settings>
- <IdleSettings>
- <Duration>PT10M</Duration>
- <WaitTimeout>PT1H</WaitTimeout>
- <StopOnIdleEnd>true</StopOnIdleEnd>
- <RestartOnIdle>false</RestartOnIdle>
- </IdleSettings>
- <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
- <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
- <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
- <AllowHardTerminate>true</AllowHardTerminate>
- <StartWhenAvailable>true</StartWhenAvailable>
- <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
- <AllowStartOnDemand>true</AllowStartOnDemand>
- <Enabled>true</Enabled>
- <Hidden>false</Hidden>
- <RunOnlyIfIdle>false</RunOnlyIfIdle>
- <WakeToRun>false</WakeToRun>
- <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
- <Priority>7</Priority>
- </Settings>
- <Actions Context="Author">
- <Exec>
- <Command>C:\Windows\System32\notepad.exe</Command>
- </Exec>
- </Actions>
- <Principals>
- <Principal id="Author">
- <UserId>COOKHAM\tfl</UserId>
- <LogonType>InteractiveToken</LogonType>
- </Principal>
- </Principals>
- </Task>
- Task submitted.
- #>
- # Helper Function
- function XMLTIME{
- Param ( $T)
- $csecond = $t.Second.ToString()
- $cminute = $t.minute.ToString()
- $chour = $t.hour.ToString()
- $cday = $t.day.ToString()
- $cmonth = $t.month.ToString()
- $cyear = $t.year.ToString()
- $date = $cyear + "-"
- if ($cmonth.Length -eq 1) { $date += "0" + $cmonth + "-"}
- else { $date += $cmonth + "-"}
- if ($cday.length -eq 1) { $date += "0" + $cday + "T"}
- else { $date += $cday + "T"}
- if ($chour.length -eq 1) { $date += "0" + $chour + ":"}
- else { $date += $chour + ":"}
- if ($cminute.length -eq 1){ $date += "0" + $cminute + ":"}
- else { $date += $cminute + ":"}
- if ($csecond.length -eq 1){ $date += "0" + $csecond}
- else { $date += $csecond}
- # return
- $date
- }
- ## Script starts here
- # A constant that specifies a time-based trigger.
- $TriggerTypeTime = 1
- # A constant that specifies an executable action.
- $ActionTypeExec = 0
- # Create and connect to the service
- $service = New-Object -com schedule.service
- $service.Connect()
- # Get a folder to create a task definition in.
- $rootFolder = $service.GetFolder("\")
- # The taskDefinition variable is the TaskDefinition object.
- # The flags parameter is 0 because it is not supported.
- $taskDefinition = $service.NewTask(0)
- # Define information about the task.
- # Set the registration info for the task by
- # creating the RegistrationInfo object.
- $regInfo = $taskDefinition.RegistrationInfo
- $regInfo.Description = "Start notepad at a certain time"
- $regInfo.Author = "Thomas Lee"
- # Set the principal for the task
- $principal = $taskDefinition.Principal
- # Set the logon type to interactive logon
- $principal.LogonType = 3
- # Set the task setting info for the Task Scheduler by
- # creating a TaskSettings object.
- $settings = $taskDefinition.Settings
- $settings.Enabled = $True
- $settings.StartWhenAvailable = $True
- $settings.Hidden = $False
- # Create a time-based trigger.
- $triggers = $taskDefinition.Triggers
- $trigger = $triggers.Create($TriggerTypeTime)
- # Trigger variables that define when the trigger is active.
- $time = ([system.datetime]::now).addseconds(30)
- $startTime = XmlTime($time)
- $time = ([system.datetime]::now).addminutes(5)
- $endTime = XmlTime($time)
- "Time Now : {0}" -f (Get-Date -display time)
- "Task startTime : {0}" -f $startTime
- "Task endTime : {0}" -f $endTime
- $trigger.StartBoundary = $startTime
- $trigger.EndBoundary = $endTime
- $trigger.ExecutionTimeLimit = "PT5M" #Five minutes
- $trigger.Id = "TimeTriggerId"
- $trigger.Enabled = $True
- # Create the action for the task to execute.
- # Add an action to the task to run notepad.exe.
- $Action = $taskDefinition.Actions.Create( $ActionTypeExec )
- $Action.Path = "C:\Windows\System32\notepad.exe"
- "Task definition created. About to submit the task..."
- # Register (create) the task.
- $rootFolder.RegisterTaskDefinition("Test TimeTrigger", $taskDefinition, 6,"" ,"" , 3)
- # all done!
- "Task submitted."
Labels:
COM,
powershell,
PowerShell V2,
Schedule.Service
Thursday, 2 September 2010
Get-UmAlQuraCalendar.ps1
- <#
- .SYNOPSIS
- This script displays details of a UmAlQura Calendar in PowerShell
- .DESCRIPTION
- This script shows the various aspects of this calendar including key properties,
- fields and selected methods.
- .NOTES
- File Name : Get-UmAlQuraCalendar.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.globalization.umalquracalendar.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-UmAlQuraCalendar.ps1'
- Um Al Qura Calendar
- Algorithm Type : LunarCalendar
- Eras in Calendar : 1
- Is read only? : False
- Max Supported Date/Time : 5/13/2029 11:59:59 PM
- Min Supported Date/Time : 4/30/1900 12:00:00 AM
- Two Digit Year Max : 1451
- April 3, 2002 of the Gregorian calendar equals the following in the UmAlQura calendar:
- Era: 1
- Year: 1423
- Is Leap Year? False
- Days In Year: 354
- Month: 1
- Months in Year: 12
- Days in Month: 29
- Leap Month: 0
- DayOfYear: 20
- DayOfMonth: 20
- DayOfWeek: Wednesday
- After adding two years and ten months and one day:
- Era: 1
- Year: 1425
- Is Leap Year? True
- Days In Year: 355
- Month: 11
- Months in Year: 12
- Days in Month: 30
- Leap Month: 0
- DayOfYear: 317
- DayOfMonth: 21
- DayOfWeek: Sunday
- #>
- # Helper Function
- Function DisplayValues {
- param ($MyCal, $MyDT )
- " Era: {0}" -f $MyCal.GetEra($MyDT)
- " Year: {0}" -f $MyCal.GetYear($MyDT)
- " Is Leap Year? {0}" -f $MyCal.IsLeapYear($MyCal.GetYear($MyDT))
- " Days In Year: {0}" -f $MyCal.GetDaysInYear($MyCal.GetYear($MyDT))
- " Month: {0}" -f $MyCal.GetMonth($MyDT)
- " Months in Year: {0}" -f $MyCal.GetMonthsInYear($MyCal.GetYear($MyDT))
- " Days in Month: {0}" -f $MyCal.GetDaysInMonth($MyCal.GetYear($MyDT), $MyDT.Month)
- " Leap Month: {0}" -f $MyCal.GetLeapMonth($MyCal.GetYear($MyDT))
- " DayOfYear: {0}" -f $MyCal.GetDayOfYear($MyDT)
- " DayOfMonth: {0}" -f $MyCal.GetDayOfMonth($MyDT)
- " DayOfWeek: {0}" -f $MyCal.GetDayOfWeek($MyDT)
- ""
- }
- # Sets a DateTime to April 3, 2002 of the Gregorian calendar.
- $MyDT = New-Object System.DateTime 2002, 4, 3, (New-Object System.Globalization.GregorianCalendar)
- # Creates an instance of the UmAlQuraCalendar.
- $MyCal = New-Object System.Globalization.UmAlQuraCalendar
- # Display properties of the calendar
- "Um Al Qura Calendar"
- "Algorithm Type : {0}" -f $MyCal.AlgorithmType
- "Eras in Calendar : {0}" -f $MyCal.Eras.count
- "Is read only? : {0}" -f $MyCal.IsReadOnly
- "Max Supported Date/Time : {0}" -f $MyCal.MaxSupportedDateTime
- "Min Supported Date/Time : {0}" -f $MyCal.MinSupportedDateTime
- "Two Digit Year Max : {0}" -f $MyCal.TwoDigitYearMax
- ""
- # Display the values of the DateTime.
- "April 3, 2002 of the Gregorian calendar equals the following in the UmAlQura calendar:"
- DisplayValues $MyCal $MyDT
- # Adds two years and ten months and one Day.
- $MyDT = $MyCal.AddYears( $MyDT, 2 )
- $MyDT = $MyCal.AddMonths($MyDT, 10 )
- $MyDT = $MyCal.AddDays($MyDT, 1 )
- # Display the values of the DateTime.
- "After adding two years and ten months and one day:"
- DisplayValues $MyCal $MyDT
Wednesday, 4 August 2010
Get-MultiplyBigInteger.ps1
- <#
- .SYNOPSIS
- This script re-implements this MSDN Sample of
- multiplying a big integer.
- .DESCRIPTION
- This script first tries and fails to multiple a pair of large integers. The
- script catches the error and then used BigInteger.Multiply to multiply
- the two big itegers.
- .NOTES
- File Name : Get-MultiplyBigInteger.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.numerics.biginteger.multiply.aspx
- .EXAMPLE
- PSH [C:\foo]: .Get-MultiplyBigInteger.ps1
- Too big for long, try biginteger
- 12,193,263,111,263,526,900
- #>
- # Add System.Numerics namespace
- $r=[system.Reflection.Assembly]::LoadWithPartialName("System.Numerics")
- # Two big numbers
- $number1 = 1234567890
- $number2 = 9876543210
- # Try normal [long] then catch error and do biginteger
- try
- {
- [long] $product = $number1 * $number2
- }
- catch
- { "Too big for long, try biginteger"
- $product = New-Object System.Numerics.BigInteger
- $product = [System.Numerics.BigInteger]::Multiply($number1, $number2)
- $product.ToString("N0")
- }
Saturday, 31 July 2010
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
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
Saturday, 22 May 2010
Get-ServiceDetails.ps1
- <#
- .SYNOPSIS
- This script displays service details
- .DESCRIPTION
- This script first enumerates all the service controllers (ie services)
- running on the local system. For each service, we look into WMI and
- get info about that running service.
- .NOTES
- File Name : Get-ServiceDetails.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/05/get-servicedetailsps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/hde9d63a.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-ServiceDetails.ps1'
- Services running on the local computer:
- Service : AeLookupSvc
- Display name: Application Experience
- Start name: localSystem
- Description: Processes application compatibility cache requests for applications as they are launched
- Service : AppHostSvc
- Display name: Application Host Helper Service
- Start name: LocalSystem
- Description: Provides administrative services for IIS, for example configuration history and Applicati
- on Pool account mapping. If this service is stopped, configuration history and locking down files or directori
- es with Application Pool specific Access Control Entries will not work.
- <rest snipped to save space!>
- #>
- ##
- # Start of script
- ##
- # Load assembly with ServiceProcess class
- $result = [reflection.Assembly]::LoadWithPartialName("System.ServiceProcess")
- $Services = [System.ServiceProcess.ServiceController]::GetServices()
- # Get WMI Services
- $WMIServices = gwmi win32_service
- # Display the list of services currently running on this computer.
- "Services running on the local computer:"
- foreach ($Service in $Services) {
- if ($Service.Status -eq [system.ServiceProcess.ServiceControllerStatus]::Running) {
- # Write the service name and the display name
- # for each running service.
- ""
- " Service : {0}" -f $Service.ServiceName
- " Display name: {0}" -f $Service.DisplayName
- # query WMI for more info on service
- $svc = $wmiServices | where {$_.name -eq $service.servicename}
- " Start name: {0}" -f $Svc.StartName
- " Description: {0}" -f $Svc.Description
- }
- }
- # End
Subscribe to:
Posts (Atom)