- <#
- .SYNOPSIS
- Defines a function to remove 'invalid' characters
- from a file name.
- .DESCRIPTION
- Some programs do not like certain 'invalid' characters
- in a file name used by that application. The function
- takes a look at each the file name and replaces some invalid
- characters with '-'.
- This function takes a file name and 'fixes' it and returns
- the 'fixed' file name. Needless to say the characters to match
- and what to replace them with is an application specific decision!
- .NOTES
- File Name : Fix-FileName.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Psh> .\Fix-FileName.ps1
- File name was: 123{}{{{|[\]
- Fixed name is: 123--------
- #>
- Function Fix-FileName {
- [CMdletbinding()]
- Param (
- $fn = $(throw 'no file name specified - returning')
- )
- Switch -Regex ($fn) {
- "}" { $fn = $fn -replace '{','-' }
- "}" { $fn = $fn -replace '}','-' }
- "\]" { $fn = $fn -replace ']','-' }
- "\[" { $fn = $fn -replace '\[','-' }
- "\\" { $fn = $fn -replace '\\','-' }
- "\|" { $fn = $fn -replace '\|','-' }
- }
- $fn
- }
- $fn = "123{}{{{|[\]"
- $fnf = Fix-FileName $fn
- "File name was: $fn"
- "Fixed name is: $fnf"
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Thursday, 14 November 2013
Fix-FileName.ps1
Wednesday, 16 October 2013
Get-SQLServer2.ps1
- #Requires -Version 3.0
- <#
- .SYNOPSIS
- This script Gets a list of SQL Severs on the Subnet
- .DESCRIPTION
- This script uses SMO to Find all the local SQL Servers
- and displays them
- .NOTES
- File Name : Get-SQLServer2.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- PS> # On a Lync Server looking at Lync Implementation
- PS> Get-SQLServer2
- There are 7 SQL Server(s) on the Local Subnet
- ServerName InstanceName Version
- ---------- ------------ -------
- 2013-LYNC-MGT MON 10.50.2500.0
- 2013-LYNC-MGT SCOM 10.50.2500.0
- 2013-TS RTCLOCAL 11.0.2100.60
- 2013-SHAREPOINT SPSDB 11.0.3000.0
- 2013-LYNC-FE RTC 11.0.2100.60
- 2013-LYNC-FE RTCLOCAL 11.0.2100.60
- 2013-LYNC-FE LYNCLOCAL 11.0.2100.60
- #>
- Import-Module SQLPS
- # Now get all the database servers on the local subnet
- $SQLservers = [System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources()
- $Srvs= @()
- # Convert collection to an array
- Foreach ($srv in $SQLservers) {
- $srvs += $srv
- }
- # Now display results
- If ($Srvs.count -LE 0) {
- "There are no SQL Servers on the Local Subnet"
- return
- }
- # Now print server details
- "There are {0} SQL Server(s) on the Local Subnet" -f $Srvs.count
- $Srvs | Select ServerName, InstanceName, Version | Format-Table -AutoSize
Technorati Tags: SQL Server 2012,SQL Server 2012 SMOs
Labels:
PowerShell Script,
SMO,
SQL,
SQL Server 2012
Saturday, 12 October 2013
Get-OLCalendarItem
- Function Get-OLCalendarItem {
- <#
- .SYNOPSIS
- A function to retreive Outlook Calender items between two dates.
- Returns PSobjects containing each item.
- .DESCRIPTION
- The function opens one's outlook calender, then retrieves the items.
- The function takes 2 parameter: start, end - items are returned that
- start betweween these two dates.
- .NOTES
- File Name : Get-OLCalendarItem
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Left as an exercise for the reader
- #>
- [CmdletBinding()]
- Param (
- $start = $(Get-Date) ,
- $end = $((Get-date).AddMonths(1))
- )
- Write-Verbose "Returning objects between: $($start.tostring()) and $($end.tostring())"
- # Load Outlook interop and Outlook iteslf
- [Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Outlook") | out-null
- $Outlook = new-object -comobject outlook.application
- # Get OL default folders
- $OlFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
- $Namespace = $Outlook.GetNameSpace("MAPI")
- $Calendar = $namespace.GetDefaultFolder($olFolders::olFolderCalendar)
- Write-Verbose "There are $($calendar.items.count) items in the calender in total"
- # Now return psobjects for all items between 2 dates
- ForEach ($citem in ($Calendar.Items | sort start)) {
- #Write-Verbose "Processing [$($citem.Subject)] Starting: [$($Citem.Start)]"
- If ($citem.start -ge $start -and $citem.start -LE $end) {
- $CalHT =[ordered] @{
- Subject = $($Citem.Subject)
- Location = $($Citem.Location);
- Start = $(Get-Date $Citem.Start);
- StartUTC = $(Get-Date $Citem.StartUTC);
- End = $(Get-Date $Citem.End);
- EndUTC = $(Get-Date $Citem.EndUTC);
- Duration = $($Citem.Duration);
- Importance = $($Citem.Importance);
- IsRecurring = $($Citem.IsRecurring);
- AllDayEvent = $($citem.AllDayEvent);
- Sensitivity = $($Citem.Sensitivity);
- ReminderSet = $($Citem.ReminderSet);
- CreationTime = $($Citem.CreationTime);
- LastModificationTime = $($Citem.LastModificationTime);
- Body = $($Citem.Body);
- }
- # Write the item out as a custom item
- $o=New-Object PSobject -Property $CalHT
- Write-Output $o
- }
- } #End of foreach
- } # End of function
- Set-Alias GCALI Get-OLCalendarItem
Wednesday, 28 August 2013
Get-DHCPPerf.Ps1
- <#
- .SYNOPSIS
- This script gets and displays perf counters for DHCP
- .DESCRIPTION
- This script uses the Get-Counter cmdlet to get all
- the counters for DHCP. The function does this for one
- DHCP server at a time.
- .NOTES
- File Name : Show-.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- C:\Foo> Get-DHCPPerformanceCounters -comp dhcp2
- Performance counters for: dhcp2
- \\dhcp1\dhcp server\failover: bndupd dropped. 0
- \\dhcp1\dhcp server\failover: transitions to recover state. 8
- \\dhcp1\dhcp server\failover: transitions to partner-down state. 0
- \\dhcp1\dhcp server\failover: transitions to communication-interrupted state. 9
- \\dhcp1\dhcp server\failover: bndupd pending in outbound queue. 0
- \\dhcp1\dhcp server\failover: bndack received/sec. 0
- \\dhcp1\dhcp server\failover: bndack sent/sec. 0
- \\dhcp1\dhcp server\failover: bndupd received/sec. 0
- \\dhcp1\dhcp server\failover: bndupd sent/sec. 0
- \\dhcp1\dhcp server\denied due to nonmatch. 0
- \\dhcp1\dhcp server\denied due to match. 0
- \\dhcp1\dhcp server\offer queue length 0
- \\dhcp1\dhcp server\releases/sec 0
- \\dhcp1\dhcp server\declines/sec 0
- \\dhcp1\dhcp server\nacks/sec 0
- \\dhcp1\dhcp server\acks/sec 0
- \\dhcp1\dhcp server\informs/sec 0
- \\dhcp1\dhcp server\requests/sec 0
- \\dhcp1\dhcp server\offers/sec 0
- \\dhcp1\dhcp server\discovers/sec 0
- \\dhcp1\dhcp server\conflict check queue length 0
- \\dhcp1\dhcp server\active queue length 0
- \\dhcp1\dhcp server\milliseconds per packet (avg). 28
- \\dhcp1\dhcp server\packets expired/sec 0
- \\dhcp1\dhcp server\duplicates dropped/sec 0
- #>
- # Get-DHCPPerformanceCounters function
- Function Get-DHCPPerformanceCounters {
- Param (
- $comp = 'localhost')
- # Get DHCP Counters
- $set = get-counter -listset "DHCP Server"
- $ctrs = $set.counter
- " Performance counters for: {0}" -f $comp
- # For each counter, get the sample
- foreach ($ctr in $ctrs) {
- $sample = (get-counter $ctr).countersamples
- "{0,-78} {1,10}" -f $sample.path, $sample.cookedvalue
- }
- }
- # Now test it
- Get-DHCPPerformanceCounters -comp dhcp1
- Get-DHCPPerformanceCounters -comp dhcp2
Technorati Tags: DHCP
Monday, 29 July 2013
Get-Zip.ps1
- <#
- .SYNOPSIS
- This script demonstrates the use of the Zip lib in .NET
- .DESCRIPTION
- This script is a re-write of an MSDN sample, using PowerShell
- .NOTES
- File Name : Show-ZIP.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx
- .EXAMPLE
- PSH> .\Show-Zip
- Directory: C:\example
- Mode LastWriteTime Length Name
- ---- ------------- ------ ----
- d---- 7/29/2013 4:35 PM extract
- d---- 7/29/2013 4:29 PM start
- -a--- 7/29/2013 4:35 PM 1668 result.zip
- Directory: C:\example\extract
- Mode LastWriteTime Length Name
- ---- ------------- ------ ----
- -a--- 7/29/2013 4:28 PM 5609 d1.txt
- -a--- 7/29/2013 4:29 PM 67308 d2.txt
- -a--- 7/29/2013 4:29 PM 67308 d3.txt
- -a--- 7/29/2013 4:29 PM 67308 d4.txt
- -a--- 7/29/2013 4:29 PM 67308 d5.txt
- Directory: C:\example\start
- Mode LastWriteTime Length Name
- ---- ------------ ------ ----
- -a--- 7/29/2013 4:28 PM 5609 d1.txt
- -a--- 7/29/2013 4:29 PM 67308 d2.txt
- -a--- 7/29/2013 4:29 PM 67308 d3.txt
- -a--- 7/29/2013 4:29 PM 67308 d4.txt
- -a--- 7/29/2013 4:29 PM 67308 d5.txt
- #>
- # Load the compression namespace
- # And yes, I know this usage is obsolete - but it works.
- # Ignore the output
- [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | out-null
- # Set locations
- $startPath = "c:\example\start"
- $zipPath = "c:\example\result.zip"
- $extractPath = "c:\example\extract"
- Remove-Item $zipPath -ea SilentlyContinue
- Remove-Item -Path $extractPath -inc * -Recurse -ea SilentlyContinue
- # Create the zip file
- [System.IO.Compression.ZipFile]::CreateFromDirectory($startPath, $zipPath)
- # Extract from zip and show what's all there
- [System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath,$extractPath);
- ls c:\example -Recurse
Technorati Tags: System.Io.Compression,ZipFile
Labels:
System.Io.Compression.FileSystem,
Zipfile
Sunday, 28 July 2013
Show-NumberGroupSizes.ps1
- <#
- .SYNOPSIS
- This script demonstrates the use of the NumberGroupSizes
- .DESCRIPTION
- This script is a re-write of an MSDN sample, using PowerShell
- .NOTES
- File Name : Show-NumberGroupSizes.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numbergroupsizes%28v=vs.100%29.aspx
- .EXAMPLE
- PSH> .\Show-NumberGroupSizes.ps1
- 123,456,789,012,345.00
- 12,3456,7890,123,45.00
- 1234567890,123,45.00
- #>
- # Get Number Format
- $nf = New-Object System.Globalization.CultureInfo "en-US", $False
- $nfi = $nf.NumberFormat
- [Int64] $myInt = 123456789012345
- $myInt.ToString( "N", $nfi )
- # Display the same value with different groupings
- [int[]] $MySizes1 = 2,3,4
- [int[]] $MySizes2 = 2,3,0
- $nfi.NumberGroupSizes = $mySizes1
- $myInt.ToString( "N",$nfi )
- $nfi.NumberGroupSizes = $mySizes2
- $myInt.ToString( "N", $nfi )
Friday, 28 June 2013
Show-CurrencyFormatting.ps1
- <#
- .SYNOPSIS
- This script re-implements an MSDN Sample showing the
- use of the NumberFormat class to nicely format things
- in this case, currency.
- .DESCRIPTION
- This script iterates through the Windows cultures and
- displays those whose 2-letter ISO code is 'en' and
- displays how Windows formats currency in that culture.
- .NOTES
- File Name : Show-CurrencyFormatting.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 to:
- http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx
- .EXAMPLE
- Psh > .\Show-CurrencyFormatting.ps1
- The currency symbol for 'English (United States)' is '$'
- The currency symbol for 'English (United Kingdom)' is '£'
- The currency symbol for 'English (Australia)' is '$'
- The currency symbol for 'English (Canada)' is '$'
- The currency symbol for 'English (New Zealand)' is '$'
- The currency symbol for 'English (Ireland)' is '€'
- The currency symbol for 'English (South Africa)' is 'R'
- The currency symbol for 'English (Jamaica)' is 'J$'
- The currency symbol for 'English (Caribbean)' is '$'
- The currency symbol for 'English (Belize)' is 'BZ$'
- The currency symbol for 'English (Trinidad and Tobago)' is 'TT$'
- The currency symbol for 'English (Zimbabwe)' is 'Z$'
- The currency symbol for 'English (Republic of the Philippines)' is 'Php'
- The currency symbol for 'English (Singapore)' is '$'
- The currency symbol for 'English (Malaysia)' is 'RM'
- The currency symbol for 'English (India)' is 'Rs.'
- #>
- # Loop through all the specific cultures known to the CLR.
- foreach ($ci in [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures))
- {
- # Only show the currency symbols for cultures that speak English.
- if ($ci.TwoLetterISOLanguageName -eq "en") {
- # Display the culture name and currency symbol.
- $nfi = $ci.NumberFormat
- "The currency symbol for '{0}' is '{1}'" -f $ci.DisplayName, $nfi.CurrencySymbol
- }
- }
Thursday, 20 June 2013
Show-CurrencyGroupSize.ps1
- <#
- .SYNOPSIS
- This script reimplements a code sample from MSDN in PowerShell.
- This sample formats and display currency using standard and
- different currency groupings.
- .DESCRIPTION
- This script displays a currency using standard, then two
- custom CurrencyGroupSizes.
- .NOTES
- File Name : Show-CurrencyGroupSize.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 to:
- http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencygroupsizes.aspx
- .EXAMPLE
- PSH:> .\Show-CurrencyGroupSize.ps1
- Default numeric format string "C"
- $123,456,789,012,345.00
- Display with array = 2,3,4
- $12,3456,7890,123,45.00
- Display with array = 2,3,0
- $1234567890,123,45.00
- #>
- # Get a NumberFormatInfo associated with the en-US culture.
- $fi = new-object System.Globalization.CultureInfo "en-US", false
- $nfi = $fi.NumberFormat
- # Display a value with the default separator (".")
- "Default numeric format string `"C`""
- [Int64] $myInt = 123456789012345
- $myInt.ToString( "C", $nfi )
- # Display the same value with different groupings.
- [int[]] $mySizes1 = (2,3,4)
- $mySizes = 2,3,0
- "";"Display with array = 2,3,4"
- $nfi.CurrencyGroupSizes = $mySizes1
- $myInt.ToString( "C", $nfi )
- "";"Display with array = 2,3,0"
- $nfi.CurrencyGroupSizes = $mySizes2
- $myInt.ToString( "C", $nfi )
Thursday, 16 May 2013
Show-CurrencyDecimalDigits.ps1
- <#
- .SYNOPSIS
- This script demonstrates the use of the CurrencyDecimalDigits
- .DESCRIPTION
- This script is a re-write of an MSDN sample, using PowerShell
- .NOTES
- File Name : Show-CurrencyDecimalDigits.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencydecimaldigits%28v=vs.100%29.aspx
- .EXAMPLE
- PSH> .\Show-CurrencyDecimalDigits.ps1
- ($1,234.00)
- ($1,234.0000)
- #>
- # Get Number Format
- $nf = New-Object System.Globalization.CultureInfo "en-US", $False
- $nfi = $nf.NumberFormat
- # Display a negative value with the default number of decimal digits (2).
- [Int64] $myInt = -1234
- $myInt.ToString( "C", $nfi )
- # Displays the same value with four decimal digits.
- $nfi.CurrencyDecimalDigits = 4
- $myInt.ToString( "C", $nfi )
Wednesday, 17 April 2013
Show-NumberDecimalSeparator.ps1
- <#
- .SYNOPSIS
- This script demonstrates the use of the NumberDecimalSeparator
- .DESCRIPTION
- This script is a re-write of an MSDN sample, using PowerShell
- .NOTES
- File Name : Show-NumberDecimaleparatort.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/b74zyt45%28v=vs.100%29.aspx
- .EXAMPLE
- PSH> .\Show-NumberDecimalSeparator.ps1
- 123,456,789.00
- 123,456,789 00
- #>
- # Get Number Format
- $nf = New-Object System.Globalization.CultureInfo "en-US", $False
- $nfi = $nf.NumberFormat
- [Int64] $myInt = 123456789
- $myInt.ToString( "N", $nfi )
- $nfi.NumberDecimalSeparator = " "
- $myInt.ToString( "N", $nfi )
Wednesday, 13 March 2013
Show-CurrencyGroupSeparator.ps1
- <#
- .SYNOPSIS
- This script demonstrates the use of the CurrencyGroupSeparator
- .DESCRIPTION
- This script is a re-write of an MSDN sample, using PowerShell
- .NOTES
- File Name : Show-CurrencyGroupSeparator.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencygroupseparator%28v=vs.100%29.aspx
- .EXAMPLE
- PSH> .\Show-NumberGroupSizes.ps1
- $123,456,789,012,345.00
- $123 456 789 012 345.00
- #>
- # Get Number Format
- $nf = New-Object System.Globalization.CultureInfo "en-US", $False
- $nfi = $nf.NumberFormat
- # Display a value with the default separator (",").
- [Int64] $myInt = 123456789012345
- $myInt.ToString( "C", $nfi )
- # Displays the same value with a blank as the separator.
- $nfi.CurrencyGroupSeparator = " "
- $myInt.ToString( "C", $nfi )
Sunday, 24 February 2013
New-ZipFromDirectory.ps1
- <#
- .SYNOPSIS
- Creates a new zip file from an existing folder
- .DESCRIPTION
- This script uses the .NET 4.5 zipfile class
- to create a zip file, getting contents from
- a folder.
- .NOTES
- File Name : New-ZipfromDirectory
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0 and .NET 4.5
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Psh> C:\foo\new-zip.ps1
- Zip file created:
- Directory: C:\foo
- Mode LastWriteTime Length Name
- ---- ------------- ------ ----
- -a--- 2/24/2013 3:00 PM 291182 ScriptLib.ZIP
- #>
- # Load the compression namespace
- # and yes, I know this usage is obsolete - but it works.
- # Ignore the output
- [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null
- # Create a type accellerator for the zipfile class
- [System.Type] $TypeAcceleratorsType=[System.Management.Automation.PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators',$True,$True)
- $TypeAcceleratorsType::Add('Zipfile','System.IO.Compression.Zipfile')
- # Now create a zip file
- # Set target zip flie and source folder
- $Folder = 'E:\PowerShellScriptLib'
- $Zipfile = 'C:\foo\ScriptLib.ZIP'
- # Ensure file does NOT exist and fodler DOES exist
- If (Test-Path $zipfile -EA -0) {
- Throw "$Zipfile exists - not safe to continue"}
- If (!(Test-Path $folder)) {
- Throw "$Folder does not seem to exist"}
- # Now create the Zip file
- Try {
- [Zipfile]::CreateFromDirectory( $folder, $zipfile)
- "Zip file created:";ls $zipfile}
- Catch {
- "Zip File NOT created"
- $Error[0]}
Labels:
.net 4.5,
System.Io.Compression.FileSystem,
zip,
Zipfile
Monday, 4 February 2013
Configure-DC1-2.ps1
- ####
- # Configure-DC1-2
- # Configures DC1 after dcpromo is completed
- #
- # Version Date What Changed
- # ------- ----------- -------------------------------------------
- # 1.0.0 14 Jan 2013 Initial release
- # 1.1.0 24 Jan 2013 Added code to count how long it all took,
- # Added checkpoint at the end of this script
- # 1.1.1 25 Jan 2013 Added auto admin logon
- # 1.1.2 5 Feb 2013 Added forced reboot of DC1-1 at script end
- ####
- # Configuration block
- $Conf = {
- $StartTime = Get-Date
- Write-Host "Starting at: $StartTime"
- # Set Credentials for use in this configuration block
- $User = "Reskit\Administrator"
- $Password = 'Pa$$w0rd'
- $PasswordSS = ConvertTo-SecureString -String $Password –AsPlainText `
- -Force
- $Dom = 'Reskit'
- $CredRK = New-Object `
- -Typename System.Management.Automation.PSCredential `
- -Argumentlist $User,$PasswordSS
- # Define registry path for autologon, then set admin logon
- $RegPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
- Set-ItemProperty -Path $RegPath -Name AutoAdminLogon `
- -Value 1 -EA 0
- Set-ItemProperty -Path $RegPath -Name DefaultUserName `
- -Value $User -EA 0
- Set-ItemProperty -Path $RegPath -Name DefaultPassword `
- -Value $Password -EA 0
- Set-ItemProperty -Path $RegPath -Name DefaultDomainName `
- -Value $Dom -EA 0
- # Install key Windows features for labs
- Write-Verbose 'Installing Windows fetures needed for DC1'
- $Features = @('PowerShell-ISE',
- 'Hyper-V-PowerShell',
- 'Rsat-AD-PowerShell',
- 'Web-Server','Web-Mgmt-Tools',
- 'Web-Mgmt-Console',
- 'Web-Scripting-Tools',
- 'Telnet-Client')
- Install-WindowsFeature @Features -IncludeManagementTools -Verbose
- # Install and configure DHCP
- Write-Verbose -Message 'Adding and then configuring DHCP'
- Install-WindowsFeature DHCP -IncludeManagementTools
- Add-DhcpServerV4Scope -Name "ReskitNet0" `
- -StartRange 10.0.0.100 `
- -EndRange 10.0.0.119 `
- -SubnetMask 255.255.255.0
- Set-DhcpServerV4OptionValue -DnsDomain Reskit.Org `
- -DnsServer 10.0.0.10
- Add-DhcpServerInDC -DnsName Dc1.reskit.org
- # Add users to the AD and then add them to some groups
- # Hash table for common new user paraemters
- Write-Verbose -Message
- $NewUserHT = @{AccountPassword = $PasswordSS;
- Enabled = $true;
- PasswordNeverExpires = $true;
- ChangePasswordAtLogon = $false
- }
- # Create one new user (me!) and add to enterprise and domain admins security groups
- New-ADUser @NewUserHT -SamAccountName tfl `
- -UserPrincipalName 'tfl@reskit.org' `
- -Name "tfl" `
- -DisplayName 'Thomas Lee'
- Add-ADPrincipalGroupMembership `
- -Identity "CN=tfl,CN=Users,DC=reskit,DC=org" `
- -MemberOf "CN=Enterprise Admins,CN=Users,DC=reskit,DC=org" ,
- "CN=Domain Admins,CN=Users,DC=reskit,DC=org"
- # Say nice things and finish
- $FinishTime = Get-Date
- Write-Verbose "Finished at: $FinishTime"
- Write-Verbose "DC1 Configuration took $(($FinishTime - $StartTime).TotalSeconds.ToString('n2')) seconds"
- } # End Conf configuration script block
- # Start of script proper
- # Set Credentials
- $Username = "Reskit\administrator"
- $Password = 'Pa$$w0rd'
- $PasswordSS = ConvertTo-SecureString -String $Password -AsPlainText
- -Force
- $CredRK = New-Object -Typename System.Management.Automation.PSCredential `
- -Argumentlist $Username,$PasswordSS
- # Following code used to test the credentials. Remove the comments on next two lines the first time you
- # run this script
- Invoke-Command -ComputerName DC1 -ScriptBlock {hostname} -Credential $Credrk -verbose
- Pause
- Invoke-Command -ComputerName DC1 -Scriptblock $conf -Credential $CredRK `
- -Verbose
- # OK - script block has completed - reboot the system and wait till it comes up
- Restart-Computer -ComputerName DC1 -Wait -For PowerShell –Force
- -Credential $CredRK
- # Finally, run a post-DCPromo snapshot
- Checkpoint-VM -VM $(Get-VM DC1) `
- -SnapshotName "DC1 - post configuration by ConfigureDC1-2.ps1"
Labels:
powershell,
PowerShell scripts,
PowerShell v3
Subscribe to:
Posts (Atom)