- <#
- .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]}
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Sunday, 24 February 2013
New-ZipFromDirectory.ps1
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)