From 90c096487826d4981a36d74d525f99a34a836f25 Mon Sep 17 00:00:00 2001 From: DemitasseNZ Date: Fri, 15 Apr 2016 13:59:32 +1200 Subject: [PATCH] More emailing & Validate script Validation of PowerCLI versions More services for VC 6 Better handling of VC 6 & 5.5 --- Build/Automate/CS1/Build.ps1 | 18 +++ Build/Automate/CS2/Build.ps1 | 17 +++ Build/Automate/DC/Phase2.ps1 | 4 +- Build/Automate/SS/Build.ps1 | 17 +++ Build/Automate/Validate.ps1 | 24 +++- Build/Automate/vShield15.ps1 | 215 --------------------------------- Build/Automate/vShield51.ps1 | 228 ----------------------------------- 7 files changed, 77 insertions(+), 446 deletions(-) delete mode 100644 Build/Automate/vShield15.ps1 delete mode 100644 Build/Automate/vShield51.ps1 diff --git a/Build/Automate/CS1/Build.ps1 b/Build/Automate/CS1/Build.ps1 index 280e08b..5e25690 100644 --- a/Build/Automate/CS1/Build.ps1 +++ b/Build/Automate/CS1/Build.ps1 @@ -14,6 +14,9 @@ if (Test-Path "B:\Automate\automate.ini") { Write-BuildLog " Timezone set to $timezone." tzutil /s "$timezone" $AdminPWD = ((Select-String -SimpleMatch "Adminpwd=" -Path "B:\Automate\automate.ini").line).substring(9) + $emailto = ((Select-String -SimpleMatch "emailto=" -Path "B:\Automate\automate.ini").line).substring(8) + $SmtpServer = ((Select-String -SimpleMatch "SmtpServer=" -Path "B:\Automate\automate.ini").line).substring(11) + } If (([System.Environment]::OSVersion.Version.Major -eq 6) -and ([System.Environment]::OSVersion.Version.Minor -ge 2)) { Write-BuildLog "Disabling autorun of ServerManager at logon." @@ -73,4 +76,19 @@ B:\automate\_Common\Autologon vi-admin lab $AdminPWD reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v Build /f Write-BuildLog "Install VMware Tools" b:\VMTools\Setup64.exe /s /v "/qn" +if (([bool]($emailto -as [Net.Mail.MailAddress])) -and ($SmtpServer -ne "none")){ + $mailmessage = New-Object system.net.mail.mailmessage + $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) + $mailmessage.from = "AutoLab" + $mailmessage.To.add($emailto) + $Summary = "Completed AutoLab VM build.`r`n" + $Summary += "The build of $env:computername has finished, installing VMware Tools and rebooting`r`n" + $Summary += "The build log is attached`r`n" + $mailmessage.Subject = "$env:computername VM build finished" + $mailmessage.Body = $Summary + $attach = new-object Net.Mail.Attachment("C:\buildlog.txt", 'text/plain') + $mailmessage.Attachments.Add($attach) + $message.Attachments.Add($attach) + $SMTPClient.Send($mailmessage) +} Read-Host "Rebooting after VMTools Install" diff --git a/Build/Automate/CS2/Build.ps1 b/Build/Automate/CS2/Build.ps1 index 91cc3d8..94b7d7e 100644 --- a/Build/Automate/CS2/Build.ps1 +++ b/Build/Automate/CS2/Build.ps1 @@ -14,6 +14,8 @@ if (Test-Path "B:\Automate\automate.ini") { Write-BuildLog " Timezone set to $timezone." tzutil /s "$timezone" $AdminPWD = ((Select-String -SimpleMatch "Adminpwd=" -Path "B:\Automate\automate.ini").line).substring(9) + $emailto = ((Select-String -SimpleMatch "emailto=" -Path "B:\Automate\automate.ini").line).substring(8) + $SmtpServer = ((Select-String -SimpleMatch "SmtpServer=" -Path "B:\Automate\automate.ini").line).substring(11) } If (([System.Environment]::OSVersion.Version.Major -eq 6) -and ([System.Environment]::OSVersion.Version.Minor -ge 2)) { Write-BuildLog "Disabling autorun of ServerManager at logon." @@ -79,6 +81,21 @@ copy $Installer C:\ reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v Build /t REG_SZ /d "cmd /c c:\Build.cmd" /f >> c:\buildlog.txt Write-BuildLog "Install VMware Tools" b:\VMTools\Setup64.exe /s /v "/qn" +if (([bool]($emailto -as [Net.Mail.MailAddress])) -and ($SmtpServer -ne "none")){ + $mailmessage = New-Object system.net.mail.mailmessage + $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) + $mailmessage.from = "AutoLab" + $mailmessage.To.add($emailto) + $Summary = "Completed AutoLab VM build.`r`n" + $Summary += "The build of $env:computername has finished, installing VMware Tools and rebooting`r`n" + $Summary += "The build log is attached`r`n" + $mailmessage.Subject = "$env:computername VM build finished" + $mailmessage.Body = $Summary + $attach = new-object Net.Mail.Attachment("C:\buildlog.txt", 'text/plain') + $mailmessage.Attachments.Add($attach) + $message.Attachments.Add($attach) + $SMTPClient.Send($mailmessage) +} Read-Host "Reboot?" diff --git a/Build/Automate/DC/Phase2.ps1 b/Build/Automate/DC/Phase2.ps1 index c5ab216..a94fa89 100644 --- a/Build/Automate/DC/Phase2.ps1 +++ b/Build/Automate/DC/Phase2.ps1 @@ -454,16 +454,15 @@ if (Test-Path "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\sqlcmd.exe") } } If (((([System.Environment]::OSVersion.Version.Major *10) +[System.Environment]::OSVersion.Version.Minor) -le 62)) { + Write-BuildLog "Doing Windows Server 2008 specific build actions" if (Test-Path B:\sqlmsssetup.exe) { Rename-Item B:\sqlmsssetup.exe SQLManagementStudio_x64_ENU.exe } - if (Test-Path B:\SQLManagementStudio_x64_ENU.exe) { if ( (!(Get-ChildItem B:\SQLManagementStudio_x64_ENU.exe).VersionInfo.ProductVersion -like "10.50.2500*") -and ($vc6SQL -or $vc5SQL -or $vc4SQL)) { Write-BuildLog "The version of SQL Management Studio on the Build share is incompatible with SQL Server 2008 Express R2 SP1. Please see ReadMe.html on the Build share." } else { Write-BuildLog "SQL Management Studio found; installing." - Install-WindowsFeature Net-Framework-Core Start-Process B:\SQLManagementStudio_x64_ENU.exe -ArgumentList "/ACTION=INSTALL /IACCEPTSQLSERVERLICENSETERMS /FEATURES=Tools /q" -Wait -Verb RunAs } } else { Write-BuildLog "SQL Management Studio not found (optional)."} @@ -478,6 +477,7 @@ If (((([System.Environment]::OSVersion.Version.Major *10) +[System.Environment]: } } If (((([System.Environment]::OSVersion.Version.Major *10) +[System.Environment]::OSVersion.Version.Minor) -ge 62)) { + Write-BuildLog "Doing Windows Server 2012 specific build actions" Write-BuildLog "Disabling autorun of ServerManager at logon." Start-Process schtasks -ArgumentList ' /Change /TN "\Microsoft\Windows\Server Manager\ServerManager" /DISABLE' -Wait -Verb RunAs Write-BuildLog "Disabling screen saver" diff --git a/Build/Automate/SS/Build.ps1 b/Build/Automate/SS/Build.ps1 index 41456c8..26e6b83 100644 --- a/Build/Automate/SS/Build.ps1 +++ b/Build/Automate/SS/Build.ps1 @@ -14,6 +14,8 @@ if (Test-Path "B:\Automate\automate.ini") { Write-BuildLog " Timezone set to $timezone." tzutil /s "$timezone" $AdminPWD = ((Select-String -SimpleMatch "Adminpwd=" -Path "B:\Automate\automate.ini").line).substring(9) + $emailto = ((Select-String -SimpleMatch "emailto=" -Path "B:\Automate\automate.ini").line).substring(8) + $SmtpServer = ((Select-String -SimpleMatch "SmtpServer=" -Path "B:\Automate\automate.ini").line).substring(11) } If (([System.Environment]::OSVersion.Version.Major -eq 6) -and ([System.Environment]::OSVersion.Version.Minor -ge 2)) { Write-BuildLog "Disabling autorun of ServerManager at logon." @@ -69,4 +71,19 @@ B:\automate\_Common\Autologon vi-admin lab $AdminPWD reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v Build /f Write-BuildLog "Install VMware Tools" b:\VMTools\Setup64.exe /s /v "/qn" +if (([bool]($emailto -as [Net.Mail.MailAddress])) -and ($SmtpServer -ne "none")){ + $mailmessage = New-Object system.net.mail.mailmessage + $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) + $mailmessage.from = "AutoLab" + $mailmessage.To.add($emailto) + $Summary = "Completed AutoLab VM build.`r`n" + $Summary += "The build of $env:computername has finished, installing VMware Tools and rebooting`r`n" + $Summary += "The build log is attached`r`n" + $mailmessage.Subject = "$env:computername VM build finished" + $mailmessage.Body = $Summary + $attach = new-object Net.Mail.Attachment("C:\buildlog.txt", 'text/plain') + $mailmessage.Attachments.Add($attach) + $message.Attachments.Add($attach) + $SMTPClient.Send($mailmessage) +} Read-Host "Rebooting after VMTools Install" diff --git a/Build/Automate/Validate.ps1 b/Build/Automate/Validate.ps1 index feb4b58..c3b797e 100644 --- a/Build/Automate/Validate.ps1 +++ b/Build/Automate/Validate.ps1 @@ -168,7 +168,9 @@ if ($CompName -eq "DC") { if (Test-Path "b:\VMware-PowerCLI-5*.exe") { If (($vSphere50 -and ((Get-ChildItem B:\VMware-PowerCLI-*.exe | where {$_.VersionInfo.ProductVersion -like "5.0*"}) -eq $Null))) {Write-Host "vSphere 5.0 found, matching PowerCLI version missing. Please check the Build share." -foregroundcolor "Yellow"} If (($vSphere51 -and ((Get-ChildItem B:\VMware-PowerCLI-*.exe | where {$_.VersionInfo.ProductVersion -like "5.1*"}) -eq $Null))) {Write-Host "vSphere 5.1 found, matching PowerCLI version missing. Please check the Build share." -foregroundcolor "Yellow"} - If (($vSphere55 -and (((Get-ChildItem B:\VMware-PowerCLI-*.exe | where {$_.VersionInfo.ProductVersion -like "5.5*"}) -eq $Null) -or !((Get-ChildItem B:\VMware-PowerCLI-*.exe | where {$_.VersionInfo.ProductVersion -like "5.8*"}) -eq $Null)))) {Write-Host "vSphere 5.5 found, matching PowerCLI version missing. Please check the Build share." -foregroundcolor "Yellow"} + If (($vSphere55 -and (((Get-ChildItem B:\VMware-PowerCLI-*.exe | where {$_.VersionInfo.ProductVersion -like "5.5*"}) -eq $Null) -and ((Get-ChildItem B:\VMware-PowerCLI-*.exe | where {$_.VersionInfo.ProductVersion -like "5.8*"}) -eq $Null)))) {Write-Host "vSphere 5.5 found, matching PowerCLI version missing. Please check the Build share." -foregroundcolor "Yellow"} + If (($vSphere60 -and (((Get-ChildItem B:\VMware-PowerCLI-*.exe | where {$_.VersionInfo.ProductVersion -like "5.8*"}) -eq $Null) -and ((([System.Environment]::OSVersion.Version.Major *10) +[System.Environment]::OSVersion.Version.Minor) -le 62)))) {Write-Host "vSphere 6.0 on Windows 2008 found, matching PowerCLI version 5.8 missing. Please check the Build share." -foregroundcolor "Yellow"} + If (($vSphere60 -and (((Get-ChildItem B:\VMware-PowerCLI-*.exe | where {$_.VersionInfo.ProductVersion -like "6*"}) -eq $Null) -and ((([System.Environment]::OSVersion.Version.Major *10) +[System.Environment]::OSVersion.Version.Minor) -ge 62)))) {Write-Host "vSphere 6.0 on Windows 2012 found, matching PowerCLI version 6 missing. Please check the Build share." -foregroundcolor "Yellow"} } Check-OptionalFile "\\192.168.199.7\Build\Win2K3.iso" "Windows Server 2003 ISO" Check-OptionalFile "\\192.168.199.7\Build\WinXP.iso" "Windows XP ISO" @@ -207,6 +209,19 @@ if ($CompName -eq "DC") { Write-Host "ESXi 5.1 TFTP files not found on DC, but they exist on Build share." -foregroundcolor "red" $Global:Pass = $false } + if ($ESXi55 -and (Test-Path "C:\TFTP-Root\ESXi515\*")) { + Write-Host "ESXi 5.5 TFTP files found." -foregroundcolor "green" + } elseif ($ESXi55 -and !(Test-Path "C:\TFTP-Root\ESXi55\*")) { + Write-Host "ESXi 5.5 TFTP files not found on DC, but they exist on Build share." -foregroundcolor "red" + $Global:Pass = $false + } + if ($ESXi60 -and (Test-Path "C:\TFTP-Root\ESXi60\*")) { + Write-Host "ESXi 6.0 TFTP files found." -foregroundcolor "green" + } elseif ($ESXi60 -and !(Test-Path "C:\TFTP-Root\ESXi60\*")) { + Write-Host "ESXi 6.0 TFTP files not found on DC, but they exist on Build share." -foregroundcolor "red" + $Global:Pass = $false + } + $vcinstall = ((Select-String -SimpleMatch "VCInstall=" -Path "B:\Automate\automate.ini").line).substring(10) If ($vcinstall -eq "50") {$vcinstall = "5"} If (!($vSphere50) -and ($vcinstall -eq "5")) { @@ -221,6 +236,10 @@ if ($CompName -eq "DC") { Write-Host "You wish to install vSphere 5.5 but the installers aren't on the build share" -foregroundcolor "red" $Global:Pass = $false } + If (!($vSphere60) -and ($vcinstall -eq "60")) { + Write-Host "You wish to install vSphere 6.0 but the installers aren't on the build share" -foregroundcolor "red" + $Global:Pass = $false + } Write-Host "Check Domain" -foregroundcolor "cyan" $domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain() if ($domain.Name -eq "lab.local") { @@ -289,6 +308,9 @@ if ($CompName -eq "VC") { if ($VCVer.StartsWith("6")) { if ((((Select-String -SimpleMatch "DeployVUM=" -Path "B:\Automate\automate.ini").line).substring(10)) -like "true") {Check-ServiceRunning "VMware vSphere Update Manager Service"} Check-ServiceRunning "VMware VirtualCenter Server" + Check-ServiceRunning "VMware vSphere Web Client" + Check-ServiceRunning "VMware Directory Service" + Check-ServiceRunning "VMware vSphere Profile-Driven Storage Service" } } diff --git a/Build/Automate/vShield15.ps1 b/Build/Automate/vShield15.ps1 deleted file mode 100644 index eddc300..0000000 --- a/Build/Automate/vShield15.ps1 +++ /dev/null @@ -1,215 +0,0 @@ -# Script to add add VMware vShield Manager to the AutoLab infrastructure -# -# Thanks to Alan Renouf (http://www.virtu-al.net/2011/09/14/powershell-automated-install-of-vshield-5/) -# Thanks to Jeff Hicks for the Test-Website Function: http://jdhitsolutions.com/blog/2010/04/hey-are-you-awake/ -# Thanks to William Lam for the trick to change the Zebra file: http://www.virtuallyghetto.com/2011/09/how-to-automate-deployment.html -# AutoLab v1.1 -# -# -. "C:\PSFunctions.ps1" -Function New-ZebraFile ($vShieldHostName, $vShieldIP, $vShieldID, $vShieldGW) { -$ZebraFile = @" -! -hostname $vShieldHostName -! -interface mgmt - ip address $vShieldIP/$vShieldID -! -ip route 0.0.0.0/0 $vShieldGW -! -line vty - no login -! -"@ - -$ZebraFile | Out-File $ENV:TEMP\zebra.conf -Encoding "ASCII" -} -Function Post-vShieldAPI ($URL, $Body) { - $wc = New-Object System.Net.WebClient - - # Add Authorization headers - $authbytes = [System.Text.Encoding]::ASCII.GetBytes($vshieldUser + ":" + $vShieldPass) - $base64 = [System.Convert]::ToBase64String($authbytes) - $authorization = "Authorization: Basic " + $base64 - $wc.Headers.Add($authorization) - - $response = $wc.UploadString($URL, "POST", $Body) -} -Function Set-vShieldConfiguration ($vCenter, $Username, $Password, $PrimaryDNS, $SecondaryDNS) { - $Body = @" - - -$($PrimaryDNS) -$($SecondaryDNS) - - -"@ - Post-vShieldAPI -URL "https://$vShieldIP/api/2.0/global/config" -Body $Body -} -Function Wait-vShieldBoot { - do { - $VM = Get-VM $vShieldHostName - Sleep 5 - } until ($VM.ToolsStatus -eq "toolsOK") -} -Function Test-WebSite { - [cmdletBinding()] - Param ( - [Parameter( - ValueFromPipeline=$True,Position=0,Mandatory=$True, - HelpMessage="The URL to test. Include http:// or https://")] - [string]$url - ) - - Begin { - CS2 "Begin function" - } - Process { - CS2 "Requesting $url" - - $wr=[system.net.webrequest]::Create($url) - #set timeout to 7 seconds - $wr.Timeout=7000 - $start=Get-Date - - Try { - $response=$wr.GetResponse() - if ($response) { - CS2 "Response returned" - $Status=$response.StatusCode - $StatusCode=($response.Statuscode -as [int]) - } - } - Catch [system.net.webexception] { - CS2 "Failed to get a response from $url" - $status = $_.Exception.Response.StatusCode - $statuscode = ( $_.Exception.Response.StatusCode -as [int]) - } - - $end=Get-Date - $timespan=$end-$start - $ResponseMS=$timespan.TotalMilliseconds - - CS2 "status is $status" - CS2 "statuscode is $statuscode" - CS2 "timer is $responseMS" - - $obj=New-Object PSObject -Property @{ - DateTime=$start - URL=$url - Status=$status - StatusCode=$statuscode - ResponseMS=$ResponseMS - } - Write-Output $obj - - } #end Process - End { - CS2 "End function" - } -} -Function Wait-vShieldWebsite { - do { - $web = test-website https://$vShieldIP - Sleep 5 - } until ($Web.Status -eq "OK") -} - -if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null ) -{ - try { - Write-Host "Loading PowerCLI plugin, this will take a little while" -foregroundcolor "cyan" - Add-PsSnapin VMware.VimAutomation.Core - } - catch { - Write-Host "Unable to load the PowerCLI plugin. Please verify installation and run this script again." - exit - } -} -#$null = Set-PowerCLIConfiguration -DisplayDeprecationWarnings:$false -InvalidCertificateAction Ignore -Confirm:$false -for ($i=1;$i -le 2; $i++){ - $vmhost = "host$i.lab.local" - $ping = new-object System.Net.NetworkInformation.Ping - $Reply = $ping.send($vmhost) - if ($Reply.status –ne "Success") { - Write-Host $vmhost " not responding to ping, exiting" -foregroundcolor "red" - Write-Host "Re-run this script when both ESXi hosts are running" -foregroundcolor "red" - exit - } -} -$vShieldHostName = "vShield" -$vShieldFQDN = "vshield.lab.local" -$vShieldCluster = "Local" -$vShieldIP = "192.168.199.40" -$vShieldID = "24" -$vShieldGW = "192.168.199.2" -$vShieldPrimaryDNS = "192.168.199.4" -$vShieldSecondaryDNS = "192.168.199.4" -$vShieldUser = "admin" -$vShieldPass = "default" -$vCenter = "192.168.199.5" -$vcUsername = "Lab\vi-admin" -$vcPass = "VMware1!" -$Newproperty = New-VIProperty -Name ToolsStatus -ObjectType VirtualMachine -Value { - param($vm) - $vm.ExtensionData.Guest.ToolsStatus -} -Force - -Write-Host "Connecting to vCenter" -try { - $Connect = Connect-VIServer -Server $vCenter -User $vcUsername -Password $vcPass -ErrorAction Stop - $VMhost = Get-Cluster $vShieldCluster | Get-VMHost | Select -First 1 -} -catch { - Write-Host "Unable to connect to to $vCenter. Exiting." - exit -} -# Work out which iSCSI datastore has the most free space -$vShieldDS = Get-Datastore -name iSCSI* | Select Name, FreeSpaceGB | Sort-Object -Property FreeSpaceGB | Select-Object -First 1 - -if (Test-Path "\\192.168.199.7\Build\vCD_15\VMware-vShield-Manager-5.0.*.ova") { - $vshieldOVA = (Get-ChildItem \\192.168.199.7\Build\vCD_15\VMware-vShield-Manager-5.0.*.ova).FullName - Write-Host "Importing the vShield OVA" - try { - $va = Import-VApp -Name $vShieldHostName -Datastore $vShieldDS.Name -VMHost $VMHost -Source $vshieldOVA -ErrorAction Stop - $null = Get-VM $vShieldHostName | Set-VM -MemoryMB 512 -Confirm:$false - $null = Get-VMResourceConfiguration -VM $vShieldHostName | Set-VMResourceConfiguration -MemReservationMB 0 - Write-Host "Starting the vShield VM" - $Start = Start-VM $vShieldHostName -Confirm:$false - Wait-vShieldBoot - Write-Host "vShield Manager import complete." - } - catch { - write-host "Unable to import vShield. Exiting." - exit - } -} else { - Write-Host "vShield OVA not found. Please copy the file to the Build share and try again." -} - -### Commented out due to issues with vShield VM authentication during Invoke-VMScript -# Write-Host "Waiting until the vShield VM has started" -# Wait-vShieldBoot -# Write-Host "Setting the initial IP address after boot" -# $Zebrafile = New-Zebrafile -vShieldHostName $vShieldFQDN -vShieldIP $vShieldIP -vShieldID $vShieldID -vShieldGW $vShieldGW -# $Password = ConvertTo-SecureString -AsPlainText $vShieldPass -Force -# $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "admin", $Password - -# $invoke = Invoke-VMScript -VM vShield -ScriptText "mv /common/configs/cli/zebra.conf /common/configs/cli/zebra.conf.bak" -ScriptType Bash -GuestCredential $cred -# $ReIP = Copy-VMGuestFile -VM $vShieldHostName -Source $ENV:TEMP\zebra.conf -Destination "/common/configs/cli/" -LocalToGuest -GuestUser $vShieldUser -GuestPassword $vShieldPass - -# Write-Host "Powering Off the vShield VM" -# Sleep 5 -# $Stop = Stop-VM $vShieldHostName -Confirm:$false -# Write-Host "Starting the vShield VM" -# $Start = Start-VM $vShieldHostName -Confirm:$false - -# Write-Host "Waiting until the vShield VM has started" -# Wait-vShieldBoot - -# Write-Host "Waiting until the vShield Management site has started" -# Wait-vShieldWebsite - -# Write-Host "Linking vShield to vCenter and set DNS entries" -# $SetIP = Set-vShieldConfiguration -vCenter $vCenter -Username $vcUsername -Password $vcPass -PrimaryDNS $vShieldPrimaryDNS -SecondaryDNS $vShieldSecondaryDNS -# Write-Host "Configuration Complete" \ No newline at end of file diff --git a/Build/Automate/vShield51.ps1 b/Build/Automate/vShield51.ps1 deleted file mode 100644 index 9a58030..0000000 --- a/Build/Automate/vShield51.ps1 +++ /dev/null @@ -1,228 +0,0 @@ -# Script to add add VMware vShield Manager to the AutoLab infrastructure -# -# Thanks to Alan Renouf (http://www.virtu-al.net/2011/09/14/powershell-automated-install-of-vshield-5/) -# Thanks to Jeff Hicks for the Test-Website Function: http://jdhitsolutions.com/blog/2010/04/hey-are-you-awake/ -# Thanks to William Lam for the trick to change the Zebra file: http://www.virtuallyghetto.com/2011/09/how-to-automate-deployment.html -# AutoLab v1.1 -# -# -. "C:\PSFunctions.ps1" -Function New-ZebraFile ($vShieldHostName, $vShieldIP, $vShieldID, $vShieldGW) { -$ZebraFile = @" -! -hostname $vShieldHostName -! -interface mgmt - ip address $vShieldIP/$vShieldID -! -ip route 0.0.0.0/0 $vShieldGW -! -line vty - no login -! -"@ - -$ZebraFile | Out-File $ENV:TEMP\zebra.conf -Encoding "ASCII" -} -Function Post-vShieldAPI ($URL, $Body) { - $wc = New-Object System.Net.WebClient - - # Add Authorization headers - $authbytes = [System.Text.Encoding]::ASCII.GetBytes($vshieldUser + ":" + $vShieldPass) - $base64 = [System.Convert]::ToBase64String($authbytes) - $authorization = "Authorization: Basic " + $base64 - $wc.Headers.Add($authorization) - - $response = $wc.UploadString($URL, "POST", $Body) -} -Function Set-vShieldConfiguration ($vCenter, $Username, $Password, $PrimaryDNS, $SecondaryDNS) { - $Body = @" - - -$($PrimaryDNS) -$($SecondaryDNS) - - -"@ - Post-vShieldAPI -URL "https://$vShieldIP/api/2.0/global/config" -Body $Body -} -function Wait-vShieldBoot { - do { - $VM = Get-VM $vShieldHostName - Sleep 5 - } until ($VM.ToolsStatus -eq "toolsOK") -} -Function Test-WebSite { - [cmdletBinding()] - Param ( - [Parameter( - ValueFromPipeline=$True,Position=0,Mandatory=$True, - HelpMessage="The URL to test. Include http:// or https://")] - [string]$url - ) - - Begin { - Write-Verbose "Begin function" - } - Process { - Write-Verbose "Requesting $url" - - $wr=[system.net.webrequest]::Create($url) - #set timeout to 7 seconds - $wr.Timeout=7000 - $start=Get-Date - - Try { - $response=$wr.GetResponse() - if ($response) { - Write-Verbose "Response returned" - $Status=$response.StatusCode - $StatusCode=($response.Statuscode -as [int]) - } - } - Catch [system.net.webexception] { - Write-Verbose "Failed to get a response from $url" - $status = $_.Exception.Response.StatusCode - $statuscode = ( $_.Exception.Response.StatusCode -as [int]) - } - - $end=Get-Date - $timespan=$end-$start - $ResponseMS=$timespan.TotalMilliseconds - - Write-Verbose "status is $status" - Write-Verbose "statuscode is $statuscode" - Write-Verbose "timer is $responseMS" - - $obj=New-Object PSObject -Property @{ - DateTime=$start - URL=$url - Status=$status - StatusCode=$statuscode - ResponseMS=$ResponseMS - } - Write-Output $obj - - } #end Process - End { - Write-Verbose "End function" - } -} -Function Wait-vShieldWebsite { - do { - $web = test-website https://$vShieldIP - Sleep 5 - } until ($Web.Status -eq "OK") -} - -if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null ) -{ - try { - Write-Host "Loading PowerCLI plugin, this will take a little while" -foregroundcolor "cyan" - Add-PsSnapin VMware.VimAutomation.Core - } - catch { - Write-Host "Unable to load the PowerCLI plugin. Please verify installation and run this script again." - exit - } -} -$PCLIver = (((Get-PowerCLIVersion).Major * 10) + (Get-PowerCLIVersion).Minor) -If ($PCLIver -ge 51) { - $null = Set-PowerCLIConfiguration -DisplayDeprecationWarnings:$false -InvalidCertificateAction Ignore -Confirm:$false -Scope "Session" - } Else { - Write-Host "Upgrade PowerCLI to version 5.1 or later" - Write-Host "This version cannot deploy thin provisioned vApps" - Read-Host "Press to exit" - Exit - } - -for ($i=1;$i -le 2; $i++){ - $vmhost = "host$i.lab.local" - $ping = new-object System.Net.NetworkInformation.Ping - $Reply = $ping.send($vmhost) - if ($Reply.status –ne "Success") { - Write-Host $vmhost " not responding to ping, exiting" -foregroundcolor "red" - Write-Host "Re-run this script when both ESXi hosts are running" -foregroundcolor "red" - exit - } -} -$vShieldHostName = "vShield" -$vShieldFQDN = "vshield.lab.local" -$vShieldCluster = "Local" -$vShieldIP = "192.168.199.40" -$vShieldID = "24" -$vShieldGW = "192.168.199.2" -$vShieldPrimaryDNS = "192.168.199.4" -$vShieldSecondaryDNS = "192.168.199.4" -$vShieldUser = "admin" -$vShieldPass = "default" -$vCenter = "192.168.199.5" -$vcUsername = "Lab\vi-admin" -$vcPass = "VMware1!" -$Newproperty = New-VIProperty -Name ToolsStatus -ObjectType VirtualMachine -Value { - param($vm) - $vm.ExtensionData.Guest.ToolsStatus -} -Force - -Write-Host "Connecting to vCenter" -try { - $Connect = Connect-VIServer -Server $vCenter -User $vcUsername -Password $vcPass -ErrorAction Stop - $VMhost = Get-Cluster $vShieldCluster | Get-VMHost | Select -First 1 -} -catch { - Write-Host "Unable to connect to to $vCenter. Exiting." - exit -} -# Work out which iSCSI datastore has the most free space -$vShieldDS = Get-Datastore -name iSCSI* | Select Name, FreeSpaceGB | Sort-Object -Property FreeSpaceGB | Select-Object -Last 1 - -if (Test-Path "\\192.168.199.7\Build\vCD_51\VMware-vShield-Manager-5.1.*.ova") { - $vshieldOVA = (Get-ChildItem \\192.168.199.7\Build\vCD_51\VMware-vShield-Manager-5.1.*.ova).FullName - Write-Host "Importing the vShield OVA" - try { - $va = Import-VApp -Name $vShieldHostName -Datastore $vShieldDS.Name -VMHost $VMHost -Source $vshieldOVA -DiskStorageFormat Thin -ErrorAction Stop - $null = Get-VM $vShieldHostName | Set-VM -MemoryMB 512 -Confirm:$false - $null = Get-VMResourceConfiguration -VM $vShieldHostName | Set-VMResourceConfiguration -MemReservationMB 0 - Write-Host "Starting the vShield VM" - $Start = Start-VM $vShieldHostName -Confirm:$false - Wait-vShieldBoot - Write-Host "vShield Manager import complete." - } - catch { - write-host "Unable to import vShield. Exiting." - exit - } -} else { - Write-Host "vShield OVA not found. Please copy the file to the Build share and try again." -} - -### Commented out due to issues with vShield VM authentication during Invoke-VMScript -# Write-Host "Waiting until the vShield VM has started" -# Wait-vShieldBoot -# Write-Host "Setting the initial IP address after boot" -# $Zebrafile = New-Zebrafile -vShieldHostName $vShieldFQDN -vShieldIP $vShieldIP -vShieldID $vShieldID -vShieldGW $vShieldGW -# $Password = ConvertTo-SecureString -AsPlainText $vShieldPass -Force -# $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "admin", $Password - -# $invoke = Invoke-VMScript -VM vShield -ScriptText "mv /common/configs/cli/zebra.conf /common/configs/cli/zebra.conf.bak" -ScriptType Bash -GuestCredential $cred -# $ReIP = Copy-VMGuestFile -VM $vShieldHostName -Source $ENV:TEMP\zebra.conf -Destination "/common/configs/cli/" -LocalToGuest -GuestUser $vShieldUser -GuestPassword $vShieldPass - -# Write-Host "Powering Off the vShield VM" -# Sleep 5 -# $Stop = Stop-VM $vShieldHostName -Confirm:$false -# Write-Host "Starting the vShield VM" -# $Start = Start-VM $vShieldHostName -Confirm:$false - -# Write-Host "Waiting until the vShield VM has started" -# Wait-vShieldBoot - -# Write-Host "Waiting until the vShield Management site has started" -# Wait-vShieldWebsite - -# Write-Host "Linking vShield to vCenter and set DNS entries" -# $SetIP = Set-vShieldConfiguration -vCenter $vCenter -Username $vcUsername -Password $vcPass -PrimaryDNS $vShieldPrimaryDNS -SecondaryDNS $vShieldSecondaryDNS -# Write-Host "Configuration Complete" -if (Test-Path "C:\Program Files\VMware\VMware Tools\VMwareToolboxCmd.exe") { - Read-Host " Configuration complete, press to continue." -} -exit \ No newline at end of file