Skip to content

Instantly share code, notes, and snippets.

@kagarlickij
Created January 8, 2017 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kagarlickij/d1b8041051e62aa34f337b3dabc77d9a to your computer and use it in GitHub Desktop.
Save kagarlickij/d1b8041051e62aa34f337b3dabc77d9a to your computer and use it in GitHub Desktop.
This is to build list of servers with valid hardware & software
# Set default minimal RAM in Mb
$DefaultMinimalRAM = 512
# Set default minimal HDD in Gb
$DefaultMinimalHDD = 20
# Set function
function HardwareCheck {
# Set parameters
param(
# Server name is mandatory
[Parameter(Mandatory=$true)]
[string[]]$ServerName,
# RAM & HDD isn't mandatory
[System.Int32]$RAMMinimum = $DefaultMinimalRAM,
[System.Int32]$HDDMinimum = $DefaultMinimalHDD
)
# Write minimum of RAM & HDD to console
Write-Host `n"RAM Minimum = " $RAMMinimum "Mb" -ForegroundColor Blue -BackgroundColor Yellow
Write-Host "HDD Minimum = " $HDDMinimum "Gb" -ForegroundColor Blue -BackgroundColor Yellow `n
# Check RAM & HDD
$ValidRAMServers = @()
$ValidHDDServers = @()
$ServerName | foreach {
$RAM = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $_ `
| Select-Object -ExpandProperty TotalPhysicalMemory
$RAMInMb = $RAM/1Mb -as[int]
$HDD = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $_ `
| Select-Object -ExpandProperty FreeSpace
$HDDInGb = $HDD/1Gb -as[int]
if ($RAMInMb -gt $RAMMinimum) {
$ValidRAMServers += $_
} else {
Write-Host $_ "has not enought RAM" -ForegroundColor Yellow
}
if ($HDDInGb -gt $HDDMinimum) {
$ValidHDDServers += $_
} else {
Write-Host $_ "has not enought HDD" -ForegroundColor Yellow
}
}
Write-Host "Servers with valid RAM: " $ValidRAMServers
Write-Host "Servers with valid HDD: " $ValidHDDServers
$ValidHardwareServers = Compare-Object -ReferenceObject $ValidRAMServers -DifferenceObject $ValidHDDServers -IncludeEqual `
| Where-Object {$_.SideIndicator -eq "=="} | Select-Object -ExpandProperty InputObject
return $ValidHardwareServers
}
# Enter custom Minimal RAM value
Write-Host "Enter RAM Minimum in Megabytes (Integer value). Default value is" $DefaultMinimalRAM "Mb"
$MinimalRAM = Read-Host
# Enter custom Minimal HDD value
Write-Host "Enter HDD Minimum in Gigabytes (Integer value). Default value is" $DefaultMinimalHDD "Gb"
$MinimalHDD = Read-Host
# If bouth RAM & HDD introduced
if (($MinimalRAM -ne [string]::empty) -and ($MinimalHDD -ne [string]::empty)) {
$HardwareCheckResult = HardwareCheck -RAMMinimum $MinimalRAM -HDDMinimum $MinimalHDD
}
# If only RAM introduced
elseif (($MinimalRAM -ne [string]::empty) -and ($MinimalHDD -eq [string]::empty)) {
$HardwareCheckResult = HardwareCheck -RAMMinimum $MinimalRAM
}
# If only HDD introduced
elseif (($MinimalRAM -eq [string]::empty) -and ($MinimalHDD -ne [string]::empty)) {
$HardwareCheckResult = HardwareCheck -HDDMinimum $MinimalHDD
}
# If nothing introduced
else {
$HardwareCheckResult = HardwareCheck
}
# Write hardware check results to console
Write-Host "Servers with valid hardware:" $HardwareCheckResult -BackgroundColor DarkGreen
# Check .Net 3.5 & CryptSvc service
function SoftwareCheck {
$ValidSoftwareServers = @()
$FeatureName = "NET-Framework-Features"
$ServiceName = "CryptSvc"
$HardwareCheckResult | foreach {
if ( ((Get-WindowsFeature -Name $FeatureName -ComputerName $_).Installed -eq "True") `
-and ((Get-Service -Name $ServiceName -ComputerName $_).Status -eq "Running") ) {
$ValidSoftwareServers += $_
} else {
Write-Host "Software on" $_ "sucks" -ForegroundColor Yellow
}
}
return $ValidSoftwareServers
}
$SoftwareCheckResult = SoftwareCheck
# Write software check results to console
Write-Host `n "Servers with valid software:" $SoftwareCheckResult -BackgroundColor DarkGreen
$SoftwareCheckResult | foreach {
$SitePath = "c$\inetpub"
$SourcesPath = "C:\site_content\wwwroot"
# Remove is just for testing
Remove-WindowsFeature -ComputerName $_ -Name Web-Server -Restart
#Remove-Item -Path \\$_\$SitePath\wwwroot -Force -Recurse -ErrorAction SilentlyContinue
sleep -Seconds 90
if ((Install-WindowsFeature -ComputerName $_ -Name Web-Server -IncludeAllSubFeature -Verbose -Restart).ExitCode -eq "Success") {
# Copy code & run IE to check
Copy-Item -Path $SourcesPath -Destination \\$_\$SitePath -Recurse -Force
Start iexplore http://$_
} else {
Write-Host "Failure on server" $_ -ForegroundColor Yellow
}
}
# Variables cleanup
Remove-Variable -Name * -ErrorAction SilentlyContinue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment