- <#
- .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
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Thursday, 6 August 2009
New-Ou.PS1
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
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
Thursday, 9 July 2009
Copy-File.ps1
- <#
- .SYNOPSIS
- This script displays the usage of the Exists and the
- copy methods of System.IP.File
- .DESCRIPTION
- This script sets up two file names, then checks to see if
- the sorucefile exists. if so, it's copied to a new file.
- .NOTES
- File Name : copy-file.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.io.file.exists.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Copy-File.PS1'
- Source File (c:\foo\Test.txt) copied to (c:\foo\Test2.txt)
- #>
- ##
- # start of script
- ##
- # Setup source and destination files
- $SourceFile = "c:\foo\Test.txt";
- $NewFile = "c:\foo\Test2.txt";
- # Now - check to see if $Sourcefile exists, and if so,
- # copy it to $newfile
- if ([System.IO.File]::Exists($SourceFile)) {
- [System.IO.File]::Copy($SourceFile, $NewFile)
- "Source File ($SourceFile) copied to ($newFile)"
- }
- else {
- "Source file ($Sourcefile) does not exist."
- }
- # End of script
Monday, 6 July 2009
Happy 1st Birthday To HTTP://PshScripts.Blogspot.Com!!!
I’ve been posting scripts here for just over a year – the first script was posted on July 5th 2008. There have been 127 posts, and the downloadable script library now has 141 scripts.
Here are some monthly stats for the past year:
The readership of the blog was not great till I started posting in ernest in November. Since then, there’s been a slow rise each month as more folks find the blog. When I first started this blog, the idea was to post just scripts – on the assumption that folks would find them using Google, et al. And that’s pretty much what has happened. Looking at the last 20 blog hits as an example: 3 of the 20 were direct access, with 17 coming from a referral. Of the 17 referrals, 12 came from Google (google.com, google.it, google.co.uk, google.bg, google.pl), 2 from Bing and one each from Hal Rotenbergs blog, Technet and PowerShell.com. It’s also clear that the readership is pretty international:
I hope this blog and the scripts posted here have been of use. Please feel free to comment on other scripts you’d like to see here!
Compare-Int32.ps1
- <#
- .SYNOPSIS
- This script illustrates the CompareExchange Method
- .DESCRIPTION
- This script creates three values, and calls CompareExchange method,
- and displays the results. The first time, we compare two non-equal values
- so no exchange is done. The second time, the comparison succeeds and the
- value us updated.
- .NOTES
- File Name : Compare-Int32.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/801kt583.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Compare-int32.PS1
- Before 1st call:
- 42
- 69
- 104
- After 1st call, before 2nd
- 42
- 69
- 104
- After 2nd call
- 69
- 69
- 104
- #>
- ##
- # Start of Script
- ##
- # Create 3 int32 values
- [int32] $a=42
- [int32] $b=69
- [int32] $c=104
- # Display values before
- "Before 1st call:";$a,$b,$c
- ""
- # Call CompareExchange and print results
- # This call compares $a with $c, so there are not equal
- $result = [System.Threading.Interlocked]::CompareExchange([ref]$a, $b, $c)
- "After 1st call, before 2nd"
- $a,$b,$c
- ""
- # Call CompareExchange and print results
- # This call compares $a with $A, so this call
- # returns $a updated to $b
- $result = [System.Threading.Interlocked]::CompareExchange([ref]$a, $b, $a)
- "After 2nd call"
- $a,$b,$c
Wednesday, 1 July 2009
Compare-Double
- <#
- .SYNOPSIS
- This script illustrates the CompareExchange Method
- .DESCRIPTION
- This script creates three values, and calls CompareExchange method,
- and displays the results. The first time, we compare to non-equal values
- so no exchange is done. The second time, the comparison succeeds and the
- value us updated.
- .NOTES
- File Name : Compare-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/cd0811yf.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Compare-Double.PS1
- Before 1st call:
- 42.42
- 69.69
- 104.4
- After 1st call, before 2nd
- 42.42
- 69.69
- 104.4
- After 2nd call
- 69.69
- 69.69
- 104.4
- #>
- ##
- # Start of Script
- ##
- # Create 3 double values
- [double] $a=42.42
- [double] $b=69.69
- [double] $c=104.4
- # Display values before
- "Before 1st call:";$a,$b,$c
- ""
- # Call CompareExchange and print results
- # This call compares $a with $c, so there are not equal
- $result = [system.Threading.Interlocked]::CompareExchange([ref]$a, $b, $c)
- "After 1st call, before 2nd"
- $a,$b,$c
- ""
- # Call CompareExchange and print results
- # This call compares $a with $A, so this call
- # returns $a updated to $b
- $result = [system.Threading.Interlocked]::CompareExchange([ref]$a, $b, $a)
- "After 2nd call"
- $a,$b,$c