Sunday 24 February 2013

New-ZipFromDirectory.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     Creates a new zip file from an existing folder 
  4. .DESCRIPTION 
  5.     This script uses the .NET 4.5 zipfile class  
  6.     to create a zip file, getting contents from  
  7.     a folder. 
  8. .NOTES 
  9.     File Name  : New-ZipfromDirectory 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 3.0 and .NET 4.5 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15. .EXAMPLE 
  16.     Psh> C:\foo\new-zip.ps1 
  17.     Zip file created: 
  18.      
  19.     Directory: C:\foo 
  20.          
  21.     Mode                LastWriteTime     Length Name 
  22.     ----                -------------     ------ ---- 
  23.     -a---         2/24/2013   3:00 PM     291182 ScriptLib.ZIP 
  24.  
  25. #> 
  26.  
  27. # Load the compression namespace 
  28. # and yes, I know this usage is obsolete - but it works. 
  29. # Ignore the output 
  30. [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null  
  31.  
  32. # Create a type accellerator for the zipfile class 
  33. [System.Type] $TypeAcceleratorsType=[System.Management.Automation.PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators',$True,$True) 
  34. $TypeAcceleratorsType::Add('Zipfile','System.IO.Compression.Zipfile'
  35.  
  36. # Now create a zip file 
  37. # Set target zip flie and source folder 
  38. $Folder  = 'E:\PowerShellScriptLib' 
  39. $Zipfile = 'C:\foo\ScriptLib.ZIP' 
  40.  
  41. # Ensure file does NOT exist and fodler DOES exist 
  42. If (Test-Path $zipfile -EA -0) { 
  43.    Throw "$Zipfile exists - not safe to continue"
  44. If (!(Test-Path $folder)) { 
  45.    Throw "$Folder does not seem to exist"
  46.     
  47. # Now create the Zip file 
  48. Try { 
  49.   [Zipfile]::CreateFromDirectory( $folder, $zipfile) 
  50.   "Zip file created:";ls $zipfile} 
  51. Catch { 
  52.   "Zip File NOT created" 
  53.   $Error[0]} 

Monday 4 February 2013

Configure-DC1-2.ps1

  1. #### 
  2. # Configure-DC1-2 
  3. # Configures DC1 after dcpromo is completed 
  4. # 
  5. # Version    Date         What Changed 
  6. # -------    -----------  ------------------------------------------- 
  7. # 1.0.0      14 Jan 2013  Initial release 
  8. # 1.1.0      24 Jan 2013  Added code to count how long it all took, 
  9. #                         Added checkpoint at the end of this script 
  10. # 1.1.1      25 Jan 2013  Added auto admin logon 
  11. # 1.1.2      5  Feb 2013  Added forced reboot of DC1-1 at script end  
  12. #### 
  13.  
  14. #     Configuration block 
  15. $Conf = { 
  16.  
  17. $StartTime = Get-Date 
  18. Write-Host "Starting at: $StartTime" 
  19.  
  20. #    Set Credentials for use in this configuration block 
  21. $User       = "Reskit\Administrator" 
  22. $Password   = 'Pa$$w0rd' 
  23. $PasswordSS = ConvertTo-SecureString  -String $Password –AsPlainText `
  24.               -Force 
  25. $Dom        = 'Reskit' 
  26. $CredRK     = New-Object  `
  27.     -Typename System.Management.Automation.PSCredential  `
  28.      -Argumentlist $User,$PasswordSS 
  29.  
  30. #    Define registry path for autologon, then set admin logon 
  31. $RegPath  = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' 
  32. Set-ItemProperty -Path  $RegPath -Name AutoAdminLogon   ` 
  33.                  -Value 1      -EA 0   
  34. Set-ItemProperty -Path  $RegPath -Name DefaultUserName   ` 
  35.                  -Value $User  -EA 0   
  36. Set-ItemProperty -Path  $RegPath -Name DefaultPassword   ` 
  37.                  -Value $Password -EA 0 
  38. Set-ItemProperty -Path  $RegPath -Name DefaultDomainName  ` 
  39.                  -Value $Dom -EA 0  
  40.  
  41. #    Install key Windows features for labs 
  42. Write-Verbose 'Installing Windows fetures needed for DC1' 
  43. $Features = @('PowerShell-ISE'
  44.               'Hyper-V-PowerShell'
  45.               'Rsat-AD-PowerShell',               
  46.               'Web-Server','Web-Mgmt-Tools'
  47.               'Web-Mgmt-Console'
  48.               'Web-Scripting-Tools'
  49.               'Telnet-Client'
  50. Install-WindowsFeature @Features -IncludeManagementTools -Verbose 
  51.  
  52. #    Install and configure DHCP 
  53. Write-Verbose -Message 'Adding and then configuring DHCP' 
  54. Install-WindowsFeature DHCP -IncludeManagementTools 
  55. Add-DhcpServerV4Scope -Name "ReskitNet0"
  56.                       -StartRange 10.0.0.100 ` 
  57.                       -EndRange 10.0.0.119 ` 
  58.                       -SubnetMask 255.255.255.0 
  59. Set-DhcpServerV4OptionValue -DnsDomain Reskit.Org ` 
  60.                             -DnsServer 10.0.0.10 
  61. Add-DhcpServerInDC -DnsName Dc1.reskit.org 
  62.  
  63. #    Add users to the AD and then add them to some groups 
  64. #    Hash table for common new user paraemters 
  65. Write-Verbose -Message 
  66. $NewUserHT  = @{AccountPassword       = $PasswordSS
  67.                 Enabled               = $true
  68.                 PasswordNeverExpires  = $true
  69.                 ChangePasswordAtLogon = $false 
  70.                 } 
  71.  
  72. #     Create one new user (me!) and add to enterprise and domain admins security groups 
  73. New-ADUser @NewUserHT -SamAccountName tfl ` 
  74.                       -UserPrincipalName 'tfl@reskit.org'
  75.                       -Name "tfl"
  76.                       -DisplayName 'Thomas Lee' 
  77. Add-ADPrincipalGroupMembership `
  78.        -Identity "CN=tfl,CN=Users,DC=reskit,DC=org"
  79.        -MemberOf "CN=Enterprise Admins,CN=Users,DC=reskit,DC=org"
  80.                  "CN=Domain Admins,CN=Users,DC=reskit,DC=org"  
  81.  
  82. #     Say nice things and finish 
  83. $FinishTime = Get-Date 
  84. Write-Verbose "Finished at: $FinishTime" 
  85. Write-Verbose "DC1 Configuration took $(($FinishTime - $StartTime).TotalSeconds.ToString('n2')) seconds" 
  86.  
  87. } # End Conf configuration script block 
  88.  
  89. #    Start of script proper 
  90.  
  91. #    Set Credentials 
  92. $Username   = "Reskit\administrator" 
  93. $Password   = 'Pa$$w0rd' 
  94. $PasswordSS = ConvertTo-SecureString  -String $Password -AsPlainText
  95.               -Force 
  96. $CredRK     = New-Object -Typename System.Management.Automation.PSCredential `
  97.         -Argumentlist $Username,$PasswordSS 
  98.  
  99. #    Following code used to test the credentials. Remove the comments on next two lines the first time you  
  100. #    run this script 
  101. Invoke-Command -ComputerName DC1 -ScriptBlock {hostname} -Credential $Credrk -verbose 
  102. Pause 
  103.  
  104. Invoke-Command -ComputerName DC1 -Scriptblock $conf -Credential $CredRK `
  105.                -Verbose 
  106.  
  107. #     OK - script block has completed - reboot the system and wait till it comes up 
  108. Restart-Computer -ComputerName DC1  -Wait -For PowerShell –Force
  109.                  -Credential $CredRK 
  110.   
  111. #    Finally, run a post-DCPromo snapshot 
  112. Checkpoint-VM -VM $(Get-VM DC1) `
  113. -SnapshotName "DC1 - post configuration by ConfigureDC1-2.ps1"