<# This script utilizes the "AnyBox" GUI menu and saved credentials to search for a VM/computer/IP by hostname, VM name, or IP. The script retrieves data from a pre-prepared CSV file (the script for CSV preparation is also available on this site). Some parameters in the script will need to be changed to match the environment being used. #>
Clear-Host
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force
$ErrorActionPreference= 'SilentlyContinue'
Add-PsSnapin VMware.VimAutomation.Core
Disconnect-VIServer * -Confirm:$false
$CSVName = "V:\SCRIPTS\Scheduled\VM_REPORT.csv" # Change this parameter
#============ZEROING$PARAMETERS============#
$vC_NAME = ""
$DataObj = ""
$VMName = ""
$VMName1 = ""
$VMName2 = ""
$VMName3 = ""
$VMName4 = ""
$VMName5 = ""
$VMName6 = ""
$VM_IP = ""
$VM_IP1 = ""
$VM_IP2 = ""
$VM_IP3 = ""
$VM_IP4 = ""
$VM_IP5 = ""
$VM_IP6 = ""
$vC = ""
$vC_NAME = ""
$vC_NAME1 = ""
$vC_NAME2 = ""
$vC_NAME3 = ""
$vC_NAME4 = ""
$vC_NAME5 = ""
$vC_NAME6 = ""
$vC_IP1 = ""
$vC_IP2 = ""
$vC_IP3 = ""
$vC_IP4 = ""
$vC_IP5 = ""
$vC_IP6 = ""
$DNSHostName = ""
$DNSHostName1 = ""
$DNSHostName2 = ""
$DNSHostName3 = ""
$DNSHostName4 = ""
$DNSHostName5 = ""
$DNSHostName6 = ""
$VM_State1 = ""
$VM_State2 = ""
$VM_State3 = ""
$VM_State4 = ""
$VM_State5 = ""
$VM_State6 = ""
#============CREDENTIALS============#
$User = "User@MyDomain.local" # Change this parameter
$Credential = Import-Clixml "C:\cred\Credentials_MyDomain.xml" # Change this parameter
$User = $Credential.UserName
$Pass = $Credential.GetNetworkCredential().Password
# $Name = Read-Host "Put a Name" #
# Write-Host = Enter VM/Host/IP for searching =
# GUI input box
[Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') > $NULL
$Name = [Microsoft.VisualBasic.Interaction]::InputBox('Enter VM/Host/IP for searching', 'VM Search Box')
Write-Host The Name you entered is "'$Name'"
$VMName = -join ($Name,"*")
$DNSHostName = -join ($Name,"*")
$VM_IP = $Name
$DataObj = Import-Csv $CSVName | Where-Object {($_."VMName" -like $VMName) -or ($_."DNSHostName" -like $DNSHostName) -or ($_."VM_IP" -like $VM_IP)} | Select VMName, DNSHostName, VM_IP, VM_State, VC_NAME, VC_IP
$DataObj | FT
$NUM = $DataObj.Count
Write-Host Count = $NUM
# $DataObj | Out-GridView -Title $Name
#============PARAMETERS============#
$VMName1 = $DataObj[0].VMName
$VMName2 = $DataObj[1].VMName
$VMName3 = $DataObj[2].VMName
$VMName4 = $DataObj[3].VMName
$VMName5 = $DataObj[4].VMName
$VMName6 = $DataObj[5].VMName
$VM_IP1 = $DataObj[0].VM_IP
$VM_IP2 = $DataObj[1].VM_IP
$VM_IP3 = $DataObj[2].VM_IP
$VM_IP4 = $DataObj[3].VM_IP
$VM_IP5 = $DataObj[4].VM_IP
$VM_IP6 = $DataObj[5].VM_IP
$vC_NAME1 = $DataObj[0].vC_NAME
$vC_NAME2 = $DataObj[1].vC_NAME
$vC_NAME3 = $DataObj[2].vC_NAME
$vC_NAME4 = $DataObj[3].vC_NAME
$vC_NAME5 = $DataObj[4].vC_NAME
$vC_NAME6 = $DataObj[5].vC_NAME
$vC_IP1 = $DataObj[0].vC_IP
$vC_IP2 = $DataObj[1].vC_IP
$vC_IP3 = $DataObj[2].vC_IP
$vC_IP4 = $DataObj[3].vC_IP
$vC_IP5 = $DataObj[4].vC_IP
$vC_IP6 = $DataObj[5].vC_IP
$DNSHostName1 = $DataObj[0].DNSHostName
$DNSHostName2 = $DataObj[1].DNSHostName
$DNSHostName3 = $DataObj[2].DNSHostName
$DNSHostName4 = $DataObj[3].DNSHostName
$DNSHostName5 = $DataObj[4].DNSHostName
$DNSHostName6 = $DataObj[5].DNSHostName
$VM_State1 = $DataObj[0].VM_State
$VM_State2 = $DataObj[1].VM_State
$VM_State3 = $DataObj[2].VM_State
$VM_State4 = $DataObj[3].VM_State
$VM_State5 = $DataObj[4].VM_State
$VM_State6 = $DataObj[5].VM_State
$VMName_INFO = ""
$VM_IP_INFO = ""
$vC_NAME_INFO = ""
$vC_IP_INFO = ""
$DNSHostName_INFO = ""
$VM_State_Info = ""
#============MENU============#
DO
{
$menu=@"
1. $VMName1; $DNSHostName1; $VM_IP1; $VM_State1
2. $VMName2; $DNSHostName2; $VM_IP2; $VM_State2
3. $VMName3; $DNSHostName3; $VM_IP3; $VM_State3
4. $VMName4; $DNSHostName4; $VM_IP4; $VM_State4
5. $VMName5; $DNSHostName5; $VM_IP5; $VM_State5
6. $VMName6; $DNSHostName6; $VM_IP6; $VM_State6
Select a VM Name by it's number
"@
#============#============#
#Clear-Host
Write-Host ""
Write-Host "=VM Menu=" -ForegroundColor Cyan
Write-Host ""
$Selection = Read-Host $Menu
#============#============#
$a = "N"
Switch ($Selection) { #Begin Switch
"1"{
$VMName_INFO = $VMName1
$VM_IP_INFO = $VM_IP1
$vC_NAME_INFO = $vC_NAME1
$vC_IP_INFO = $vC_IP1
$DNSHostName_INFO = $DNSHostName1
$VM_State_Info = $VM_State1
Connect-VIServer -User $User -Password $Pass $VC_IP_INFO
Write-Host VM "'$VMName_INFO'" consists in vC "'$vC_NAME_INFO'" "'$VC_IP_INFO'"
Write-Host DNSHostName = "'$DNSHostName_INFO'" and IP = "'$VM_IP_INFO'" and Status = "'$VM_State_Info'"
$a = "Y"
}
"2" {
$VMName_INFO = $VMName2
$VM_IP_INFO = $VM_IP2
$vC_NAME_INFO = $vC_NAME2
$vC_IP_INFO = $vC_IP2
$DNSHostName_INFO = $DNSHostName2
$VM_State_Info = $VM_State2
Connect-VIServer -User $User -Password $Pass $VC_IP_INFO
Write-Host VM "'$VMName_INFO'" consists in vC "'$vC_NAME_INFO'" "'$VC_IP_INFO'"
Write-Host DNSHostName = "'$DNSHostName_INFO'" and IP = "'$VM_IP_INFO'" and Status = "'$VM_State_Info'"
$a = "Y"
}
"3" {
$VMName_INFO = $VMName3
$VM_IP_INFO = $VM_IP3
$vC_NAME_INFO = $vC_NAME3
$vC_IP_INFO = $vC_IP3
$DNSHostName_INFO = $DNSHostName3
$VM_State_Info = $VM_State3
Connect-VIServer -User $User -Password $Pass $VC_IP_INFO
Write-Host VM "'$VMName_INFO'" consists in vC "'$vC_NAME_INFO'" "'$VC_IP_INFO'"
Write-Host DNSHostName = "'$DNSHostName_INFO'" and IP = "'$VM_IP_INFO'" and Status = "'$VM_State_Info'"
$a = "Y"
}
"4" {
$VMName_INFO = $VMName4
$VM_IP_INFO = $VM_IP4
$vC_NAME_INFO = $vC_NAME4
$vC_IP_INFO = $vC_IP4
$DNSHostName_INFO = $DNSHostName4
$VM_State_Info = $VM_State4
Connect-VIServer -User $User -Password $Pass $VC_IP_INFO
Write-Host VM "'$VMName_INFO'" consists in vC "'$vC_NAME_INFO'" "'$VC_IP_INFO'"
Write-Host DNSHostName = "'$DNSHostName_INFO'" and IP = "'$VM_IP_INFO'" and Status = "'$VM_State_Info'"
$a = "Y"
}
"5" {
$VMName_INFO = $VMName5
$VM_IP_INFO = $VM_IP5
$vC_NAME_INFO = $vC_NAME5
$vC_IP_INFO = $vC_IP5
$DNSHostName_INFO = $DNSHostName5
$VM_State_Info = $VM_State5
Connect-VIServer -User $User -Password $Pass $VC_IP_INFO
Write-Host VM "'$VMName_INFO'" consists in vC "'$vC_NAME_INFO'" "'$VC_IP_INFO'"
Write-Host DNSHostName = "'$DNSHostName_INFO'" and IP = "'$VM_IP_INFO'" and Status = "'$VM_State_Info'"
$a = "Y"
}
"6" {
$VMName_INFO = $VMName6
$VM_IP_INFO = $VM_IP6
$vC_NAME_INFO = $vC_NAME6
$vC_IP_INFO = $vC_IP6
$DNSHostName_INFO = $DNSHostName6
$VM_State_Info = $VM_State6
Connect-VIServer -User $User -Password $Pass $VC_IP_INFO
Write-Host VM "'$VMName_INFO'" consists in vC "'$vC_NAME_INFO'" "'$VC_IP_INFO'"
Write-Host DNSHostName = "'$DNSHostName_INFO'" and IP = "'$VM_IP_INFO'" and Status = "'$VM_State_Info'"
$a = "Y"
}
default {
Write-Host "I don't understand what do you want to do." -ForegroundColor Yellow
}
} #End of switch
}
UNTIL ($a -eq "Y")
#============#============#
# This part of code is using "AnyBox" feature
# $Open_Console = Get-VM $VMNAME | Open-VMConsoleWindow
$Prompt = New-AnyBoxPrompt `
-Name 'Action' -InputType Text `
-Message ''$VMNAME_INFO' ; Choose an action' `
-DefaultValue 'Open-Console' `
-ValidateSet @('Open-Console', 'Open-RDP', 'Restart_Promptly', 'Hard_Reset', 'Shutdown_Promptly', 'Start-VM', 'Extend_C:', 'Other') `
$q = Show-AnyBox -Icon 'Question' -Prompt $prompt -Buttons 'OK'
$OK = $q.OK
$Action = $q.Action
If(($OK -eq $True) -and ($Action -eq "Open-Console")){ Get-VM $VMNAME_INFO | Open-VMConsoleWindow }
If(($OK -eq $True) -and ($Action -eq "Open-RDP")){ mstsc /v:"$DNSHostName_INFO":3389 }
If(($OK -eq $True) -and ($Action -eq "Restart_Promptly")){ Get-VM $VMNAME_INFO | Open-VMConsoleWindow ; Get-VM $VMNAME_INFO | Restart-VMGuest }
If(($OK -eq $True) -and ($Action -eq "Hard_Reset")){ Get-VM $VMNAME_INFO | Open-VMConsoleWindow ; Get-VM $VMNAME_INFO | Restart-VM }
If(($OK -eq $True) -and ($Action -eq "Shutdown_Promptly")){ Get-VM $VMNAME_INFO | Open-VMConsoleWindow ; Get-VM $VMNAME_INFO | Shutdown-VMGuest}
If(($OK -eq $True) -and ($Action -eq "Start-VM")){ Get-VM $VMNAME_INFO | Open-VMConsoleWindow ; Get-VM $VMNAME_INFO | Start-VM }
Write-Host ""
Disconnect-VIServer * -Confirm:$false
<#
Additional tasks to do:
Add opening Excel-CSV;
Rename VM;
Create snapshots: Get-VM | Get-Snapshot
Delete snapshot: Get-VM | Get-Snapshot | Remove Snapshot
Move-VM to another Datastore;
Move Disk to another VM;
Extend disks space (the most difficult is to identify match between VMDK number and Windows guest disk letter because there is no full proof solution).
#>
Комментариев нет:
Отправить комментарий