Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #loads module
- Get-Module -ListAvailable VMware* | Import-Module | Out-Null
- #Name of cluster
- $Cluster= ""
- #IP or name of VCenter
- $vCenterName = ""
- $vCenterUser = ""
- $vCenterPass = ""
- Connect-VIServer -Server $vCenterName -User $vCenterUser -Password $vCenterPass -WarningAction SilentlyContinue | out-null
- #Function that gets VM folders, filters all static folders away thats not needed.
- Function Get-VMFolder() {
- $VMListe = (Get-Folder).name | Where-Object {$_ -notlike "Datacenters" -and $_ -notlike "vm" -and $_ -notlike "host" -and $_ -notlike "datastore" -and $_ -notlike "Discovered virtual machine" -and $_ -notlike "network"}
- return $VMListe
- }
- #Function that gets VM information, gets VM name, and guest OS ip address, aswell as option to give other filename in if statement.
- function Get-VMStats(){
- $Folder= $VMInfoList.Text
- $VMName =(get-folder -name $Folder| Get-VM).Guest.VM.Name
- $VMIP = (get-folder -name $Folder| Get-VM).Guest.IPAddress | Select-String -Pattern "10.108*"
- $i=0
- if ($FilNavnRadioButton.Checked -eq $true) {
- New-item -ItemType File -Path ("~/Desktop/"+$VMInfoTextBox+".txt") -Force
- ForEach ($Monkey in $VMName) {
- $i++
- }
- }
- if ($DefaultRadioButton.Checked -eq $true){
- New-item -ItemType File -Path ("~/Desktop/"+$Folder+".txt") -Force
- ForEach ($Monkey in $VMName) {
- $VMName[$i] + " "+ $VMIP[$i]+" Brugernavn: root Password: P@ssW0rd" | out-file ("~/Desktop/"+$Folder+".txt") -append
- $i++
- }
- }
- }
- #Starts VMs, by getting VM Folder -> VMs -> Foreach that starts VMs async.
- Function Start-VMs() {
- $Folder= $VMManListe.Text
- Get-Folder -Name $Folder | Get-VM | ForEach-Object{Start-VM -vm $_ -RunAsync -Confirm:$false}
- }
- #Stops VMs, by getting VM Folder -> VMs -> Foreach that starts VMs async.
- Function Stop-VMs(){
- $Folder= $VMManListe.Text
- Get-Folder -Name $Folder | Get-VM | ForEach-Object{Stop-VM -vm $_ -RunAsync -Confirm:$false}
- }
- #Removes VMs, removes the entire folder, deletepermanently parameter makes sure all data is removed from SAN.
- Function Remove-VMFolder() {
- $Folder= $VMManListe.Text
- Remove-Folder -Folder $Folder -DeletePermanently
- }
- #Gets all VM Templates in Datacenter.
- Function Get-VMTemplates () {
- $TListe = Get-Template
- return $TListe
- }
- <#
- HovedFunktion der deployer VMs, laver en folder, navngivning er via Text input box i GUI + Grp og nummer for VM. Deployer til Freenas$i hvor I er et nr. mellem 1-7
- Eksisterende hold henter jeg antallet af VM i folders som int, og bruger den til navngivning af nye VMs.
- #>
- <#
- Main function that deploys VMs, Makes a folder, naming is done via text input box in GUI, + Grp and number for VM. Deploys to Freenas$i where i is a number between 1-7.
- Eksisterende Hold gets number of VMs in folder as a int, and uses that for naming new vms.
- #>
- Function DeployVM ($mForm)
- {
- if ($NyVMRadButton.Checked -eq $true){
- $Hold = $TeamTextBox.Text
- $Antal = $AntalTextBox.Text
- $VMtemplate = $TemplateListe.Text
- Write-Host "Hovedprocessen startes."
- Write-Host "Template -> $VMtemplate"
- Write-Host "Antal -> $Antal - " $Antal.GetType()
- Write-Host "Hold -> $Hold"
- $i=1
- new-folder -Name $Hold -Location (Get-folder vm)
- 1..$Antal | foreach {
- if ($i -ge 8){
- $i=1
- }
- $y="{0:D1}" -f $_
- $VM_name= $Hold+'-Grp'+$y
- $ESXi=Get-Cluster | Get-VMHost -state connected | Get-Random
- write-host "Creation of VM $VM_name initiated" -foreground green
- New-VM -Name $VM_Name -template $VMtemplate -VMHost $ESXi -Datastore "FreeNas$i" -Location $Hold -RunAsync
- $i++
- }
- }
- if ($ExistingVMRadButton.Checked -eq $true){
- $Folder= $ExistingTeamList.Text
- $VMCount =((get-folder -name $Folder| Get-VM).Guest.VM.Name).count +1
- $Hold = $Folder
- [int]$IntVMCount= $VMCount
- [int]$IntAntalTextBox= $AntalTextBox.Text
- $Antal = $IntVMCount+ $IntAntalTextBox -1
- $VMtemplate = $TemplateListe.Text
- Write-Host "Hovedprocessen startes."
- Write-Host "Template -> $VMtemplate"
- Write-Host "Antal -> $IntAntalTextBox"
- Write-Host "Hold -> $Hold"
- $i=1
- $VMcount..$Antal | foreach {
- if ($i -ge 8){
- $i=1
- }
- $y="{0:D1}" -f $_
- $VM_name= $Hold+'-Grp'+$y
- $ESXi=Get-Cluster | Get-VMHost -state connected | Get-Random
- $RDataStore= Get-DatastoreCluster | Get-Datastore | Get-Random
- write-host "Creation of VM $VM_name initiated" -foreground green
- New-VM -Name $VM_Name -template $VMtemplate -VMHost $ESXi -Datastore "FreeNas$i" -Location $Hold -RunAsync
- $i++
- }
- }
- }
- function CreateForm {
- Add-Type -AssemblyName System.Windows.Forms
- Add-Type -AssemblyName System.drawing
- #Form Setup
- $form1 = New-Object System.Windows.Forms.Form
- $TabControl = New-object System.Windows.Forms.TabControl
- $DeployVMPage = New-Object System.Windows.Forms.TabPage
- $VMManagementPage = New-Object System.Windows.Forms.TabPage
- $VMINfoPage = New-Object System.Windows.Forms.TabPage
- $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
- #Form Parameter
- $form1.Text = "ESXI VCenter VM Management"
- $form1.Name = "form1"
- $form1.DataBindings.DefaultDataSourceUpdateMode = 0
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Width = 350
- $System_Drawing_Size.Height = 350
- $form1.ClientSize = $System_Drawing_Size
- #Tab Control
- $tabControl.DataBindings.DefaultDataSourceUpdateMode = 0
- $System_Drawing_Point = New-Object System.Drawing.Point
- $System_Drawing_Point.X = 5
- $System_Drawing_Point.Y = 5
- $tabControl.Location = $System_Drawing_Point
- $tabControl.Name = "tabControl"
- $System_Drawing_Size = New-Object System.Drawing.Size
- $System_Drawing_Size.Height = 340
- $System_Drawing_Size.Width = 340
- $tabControl.Size = $System_Drawing_Size
- $form1.Controls.Add($tabControl)
- $DeployVMPage.DataBindings.DefaultDataSourceUpdateMode = 0
- $DeployVMPage.UseVisualStyleBackColor = $True
- $DeployVMPage.Name = "Deploy VM"
- $DeployVMPage.Text = "Deploy VM”
- $tabControl.Controls.Add($DeployVMPage)
- $VMManagementPage.DataBindings.DefaultDataSourceUpdateMode = 0
- $VMManagementPage.UseVisualStyleBackColor = $True
- $VMManagementPage.Name = "VM Management"
- $VMManagementPage.Text = "VM Management”
- $tabControl.Controls.Add($VMManagementPage)
- $VMInfoPage.DataBindings.DefaultDataSourceUpdateMode = 0
- $VMInfoPage.UseVisualStyleBackColor = $True
- $VMInfoPage.Name = "VM Info"
- $VMInfoPage.Text = "VM Info”
- $tabControl.Controls.Add($VMInfoPage)
- #DeployVMPage
- function display-groups{
- if ($NyVMRadButton.Checked -eq $true){
- $TeamNameText.Visible=$true
- $TeamTextBox.Visible=$true
- $ExistingTeamList.Visible=$false
- $ExistingTeamNameText.Visible=$false
- }
- if ($ExistingVMRadButton.Checked -eq $true){
- $TeamNameText.Visible=$false
- $TeamTextBox.Visible=$false
- $ExistingTeamList.Visible=$true
- $ExistingTeamNameText.Visible=$true
- }
- }
- $DeployVMGroup = New-Object System.Windows.Forms.groupBox #create the group box
- $DeployVMGroup.Location = New-Object System.Drawing.Size(5,5) #location of the group box (px) in relation to the primary window's edges (length, height)
- $DeployVMGroup.size = New-Object System.Drawing.Size(300,40) #the size in px of the group box (length, height)
- $DeployVMGroup.text = "Options:" #labeling the box
- $DeployVMPage.Controls.Add($DeployVMGroup) #activate the group box
- $NyVMRadButton = New-Object System.Windows.Forms.RadioButton #create the radio button
- $NyVMRadButton.Location = new-object System.Drawing.Point(5,15) #location of the radio button(px) in relation to the group box's edges (length, height)
- $NyVMRadButton.size = New-Object System.Drawing.Size(95,20) #the size in px of the radio button (length, height)
- $NyVMRadButton.Checked = $true #is checked by default
- $NyVMRadButton.Text = "New Team" #labeling the radio button
- $NyVMRadButton.Add_Click({display-groups})
- $DeployVMGroup.Controls.Add($NyVMRadButton) #activate the inside the group box
- $TeamNameText = New-Object System.Windows.Forms.Label
- $TeamNameText.Text="TeamName"
- $TeamNameText.Top="70"
- $TeamNameText.Left="10"
- $TeamNameText.Visible=$true
- $TeamNameText.Anchor="Left,Top"
- $TeamNameText.Size = New-Object System.Drawing.Size(250,15)
- $DeployVMPage.Controls.Add($TeamNameText)
- $TeamTextBox = New-Object System.Windows.Forms.TextBox
- $TeamTextBox.Text=""
- $TeamTextBox.Top="85"
- $TeamTextBox.Left="10"
- $TeamTextBox.Anchor="Left,Top"
- $TeamNameText.Visible=$true
- $TeamTextBox.Size = New-Object System.Drawing.Size(150,23)
- $DeployVMPage.Controls.Add($TeamTextBox)
- $ExistingVMRadButton = New-Object System.Windows.Forms.RadioButton #create the radio button
- $ExistingVMRadButton.Location = new-object System.Drawing.Point(120,15) #location of the radio button(px) in relation to the group box's edges (length, height)
- $ExistingVMRadButton.size = New-Object System.Drawing.Size(120,20) #the size in px of the radio button (length, height)
- $ExistingVMRadButton.Checked = $false #is checked by default
- $ExistingVMRadButton.Text = "Existing team" #labeling the radio button
- $ExistingVMRadButton.Add_Click({display-groups})
- $DeployVMGroup.Controls.Add($ExistingVMRadButton) #activate the inside the group box
- $ExistingTeamNameText = New-Object System.Windows.Forms.Label
- $ExistingTeamNameText.Text="Chose Team"
- $ExistingTeamNameText.Top="70"
- $ExistingTeamNameText.Left="10"
- $ExistingTeamNameText.Visible=$false
- $ExistingTeamNameText.Anchor="Left,Top"
- $ExistingTeamNameText.Size = New-Object System.Drawing.Size(250,15)
- $DeployVMPage.Controls.Add($ExistingTeamNameText)
- $VMListe=get-VMFolder
- $ExistingTeamList = New-Object System.Windows.Forms.ComboBox
- $ExistingTeamList.Text=""
- $ExistingTeamList.Top="85"
- $ExistingTeamList.Left="10"
- $ExistingTeamList.Anchor="Left,Top"
- $ExistingTeamList.Visible=$false
- $ExistingTeamList.Size = New-Object System.Drawing.Size(150,23)
- foreach ($item in $VMListe) {
- $ExistingTeamList.Items.Add($item)
- }
- $DeployVMPage.Controls.Add($ExistingTeamList)
- $TemplateTekst = New-Object System.Windows.Forms.Label
- $TemplateTekst.Text="Template"
- $TemplateTekst.Top="115"
- $TemplateTekst.Left="10"
- $TemplateTekst.Anchor="Left,Top"
- $TemplateTekst.Size = New-Object System.Drawing.Size(100,15)
- $DeployVMPage.Controls.Add($TemplateTekst)
- $TListe = Get-VMTemplates
- $TemplateListe = New-Object System.Windows.Forms.ComboBox
- $TemplateListe.Text=""
- $TemplateListe.Top="130"
- $TemplateListe.Left="10"
- $TemplateListe.Anchor="Left,Top"
- $TemplateListe.Size = New-Object System.Drawing.Size(150,23)
- foreach ($item in $TListe) {
- $TemplateListe.Items.Add($item)
- }
- $DeployVMPage.Controls.Add($TemplateListe)
- $GruppeTekst = New-Object System.Windows.Forms.Label
- $GruppeTekst.Text="Number of Grps"
- $GruppeTekst.Top="160"
- $GruppeTekst.Left="10"
- $GruppeTekst.Anchor="Left,Top"
- $GruppeTekst.Size = New-Object System.Drawing.Size(110,15)
- $DeployVMPage.Controls.Add($GruppeTekst)
- $AntalTextBox = New-Object System.Windows.Forms.TextBox
- $AntalTextBox.Text=""
- $AntalTextBox.Top="175"
- $AntalTextBox.Left="10"
- $AntalTextBox.Anchor="Left,Top"
- $AntalTextBox.Size = New-Object System.Drawing.Size(50,23)
- $DeployVMPage.Controls.Add($AntalTextBox)
- $DeployVMButton = New-Object System.Windows.Forms.Button
- $DeployVMButton.Text="Deploy VM's"
- $DeployVMButton.Top="200"
- $DeployVMButton.Left="10"
- $DeployVMButton.Anchor="Left,Top"
- $DeployVMButton.Size = New-Object System.Drawing.Size(200,23)
- $DeployVMButton.Add_Click({DeployVM})
- $DeployVMPage.Controls.Add($DeployVMButton)
- #VM Management Page
- function do-stuffs{
- if ($StartVMRadButton.Checked -eq $true) {Start-VMs}
- if ($StopVMRadButton.Checked -eq $true) {Stop-VMs}
- if ($RemoveVMRadButton.Checked -eq $true) {Remove-VMFolder}
- }
- $VMManText = New-Object System.Windows.Forms.Label
- $VMManText.Text="Chose Team"
- $VMManText.Top="10"
- $VMManText.Left="10"
- $VMManText.Anchor="Left,Top"
- $VMManText.Size = New-Object System.Drawing.Size(250,23)
- $VMManagementPage.Controls.Add($VMManText)
- $VMListe=get-VMFolder
- $VMManListe = New-Object System.Windows.Forms.ComboBox
- $VMManListe.Text=""
- $VMManListe.Top="35"
- $VMManListe.Left="10"
- $VMManListe.Anchor="Left,Top"
- $VMManListe.Size = New-Object System.Drawing.Size(150,23)
- foreach ($item in $VMListe) {
- $VMManListe.Items.Add($item)
- }
- $VMManagementPage.Controls.Add($VMManListe)
- $VMManGroup = New-Object System.Windows.Forms.groupBox #create the group box
- $VMManGroup.Location = New-Object System.Drawing.Size(10,70) #location of the group box (px) in relation to the primary window's edges (length, height)
- $VMManGroup.size = New-Object System.Drawing.Size(200,120) #the size in px of the group box (length, height)
- $VMManGroup.text = "Options:" #labeling the box
- $VMManagementPage.Controls.Add($VMManGroup) #activate the group box
- $StartVMRadButton = New-Object System.Windows.Forms.RadioButton #create the radio button
- $StartVMRadButton.Location = new-object System.Drawing.Point(5,25) #location of the radio button(px) in relation to the group box's edges (length, height)
- $StartVMRadButton.size = New-Object System.Drawing.Size(150,20) #the size in px of the radio button (length, height)
- $StartVMRadButton.Checked = $false #is checked by default
- $StartVMRadButton.Text = "Start VMs" #labeling the radio button
- $VMManGroup.Controls.Add($StartVMRadButton) #activate the inside the group box
- $StopVMRadButton = New-Object System.Windows.Forms.RadioButton #create the radio button
- $StopVMRadButton.Location = new-object System.Drawing.Point(5,50) #location of the radio button(px) in relation to the group box's edges (length, height)
- $StopVMRadButton.size = New-Object System.Drawing.Size(150,20) #the size in px of the radio button (length, height)
- $StopVMRadButton.Checked = $false #is checked by default
- $StopVMRadButton.Text = "Stop VMs" #labeling the radio button
- $VMManGroup.Controls.Add($StopVMRadButton) #activate the inside the group box
- $RemoveVMRadButton = New-Object System.Windows.Forms.RadioButton #create the radio button
- $RemoveVMRadButton.Location = new-object System.Drawing.Point(5,75) #location of the radio button(px) in relation to the group box's edges (length, height)
- $RemoveVMRadButton.size = New-Object System.Drawing.Size(150,20) #the size in px of the radio button (length, height)
- $RemoveVMRadButton.Checked = $false #is checked by default
- $RemoveVMRadButton.Text = "Fjern VMs" #labeling the radio button
- $VMManGroup.Controls.Add($RemoveVMRadButton) #activate the inside the group box
- $DoStuffVMButton = New-Object System.Windows.Forms.Button
- $DoStuffVMButton.Text="Manage VMs"
- $DoStuffVMButton.Top="200"
- $DoStuffVMButton.Left="10"
- $DoStuffVMButton.Anchor="Left,Top"
- $DoStuffVMButton.Size = New-Object System.Drawing.Size(200,23)
- $DoStuffVMButton.Add_Click({do-stuffs})
- $VMManagementPage.Controls.Add($DoStuffVMButton)
- #VM Info Page
- $VMInfoTeamText = New-Object System.Windows.Forms.Label
- $VMInfoTeamText.Text="Chose Team"
- $VMInfoTeamText.Top="10"
- $VMInfoTeamText.Left="10"
- $VMInfoTeamText.Anchor="Left,Top"
- $VMInfoTeamText.Size = New-Object System.Drawing.Size(250,23)
- $VMInfoPage.Controls.Add($VMInfoTeamText)
- $VMInfoList = New-Object System.Windows.Forms.ComboBox
- $VMInfoList.Text=""
- $VMInfoList.Top="35"
- $VMInfoList.Left="10"
- $VMInfoList.Anchor="Left,Top"
- $VMInfoList.Size = New-Object System.Drawing.Size(150,23)
- foreach ($item in $VMListe) {
- $VMInfoList.Items.Add($item)
- }
- $VMInfoPage.Controls.Add($VMInfoList)
- $VMInfoGroup = New-Object System.Windows.Forms.groupBox #create the group box
- $VMInfoGroup.Location = New-Object System.Drawing.Size(10,70) #location of the group box (px) in relation to the primary window's edges (length, height)
- $VMInfoGroup.size = New-Object System.Drawing.Size(200,120) #the size in px of the group box (length, height)
- $VMInfoGroup.text = "Options:" #labeling the box
- $VMInfoPage.Controls.Add($VMInfoGroup) #activate the group box
- $FilNavnRadioButton = New-Object System.Windows.Forms.RadioButton #create the radio button
- $FilNavnRadioButton.Location = new-object System.Drawing.Point(5,25) #location of the radio button(px) in relation to the group box's edges (length, height)
- $FilNavnRadioButton.size = New-Object System.Drawing.Size(150,20) #the size in px of the radio button (length, height)
- $FilNavnRadioButton.Checked = $false #is checked by default
- $FilNavnRadioButton.Text = "~/Desktop/{INPUT}.txt" #labeling the radio button
- $VMInfoGroup.Controls.Add($FilNavnRadioButton) #activate the inside the group box
- $VMInfoTextBox = New-Object System.Windows.Forms.TextBox
- $VMInfoTextBox.Text=""
- $VMInfoTextBox.Top="50"
- $VMInfoTextBox.Left="5"
- $VMInfoTextBox.Anchor="Left,Top"
- $VMInfoTextBox.Size = New-Object System.Drawing.Size(190,120)
- $VMInfoGroup.Controls.Add($VMInfoTextBox)
- $DefaultRadioButton = New-Object System.Windows.Forms.RadioButton #create the radio button
- $DefaultRadioButton.Location = new-object System.Drawing.Point(5,90) #location of the radio button(px) in relation to the group box's edges (length, height)
- $DefaultRadioButton.size = New-Object System.Drawing.Size(250,30) #the size in px of the radio button (length, height)
- $DefaultRadioButton.Checked = $true #is checked by default
- $DefaultRadioButton.Text = "Default ~/Desktop/{TEAMNAME}.txt" #labeling the radio button
- $VMInfoGroup.Controls.Add($DefaultRadioButton) #activate the inside the group box
- $VMInfoButton = New-Object System.Windows.Forms.Button
- $VMInfoButton.Text="Get VM Info"
- $VMInfoButton.Top="200"
- $VMInfoButton.Left="10"
- $VMInfoButton.Anchor="Left,Top"
- $VMInfoButton.Size= New-Object System.Drawing.Size(200,20)
- $VMInfoButton.Add_Click({get-VMStats})
- $VMInfoPage.Controls.Add($VMInfoButton)
- #Save the initial state of the form
- $InitialFormWindowState = $form1.WindowState
- #Init the OnLoad event to correct the initial state of the form
- $form1.add_Load($OnLoadForm_StateCorrection)
- #Show the Form
- $form1.ShowDialog()| Out-Null
- } #End function CreateForm
- CreateForm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement