Advertisement
Guest User

Untitled

a guest
Jun 11th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1. param([String]$ImportPath, [String]$SourceVC)
  2.  
  3. do {
  4. $user = Read-Host -Prompt 'Enter username for source vCenter'
  5. $pass = Read-Host -AsSecureString -Prompt 'Enter password for source vCenter'
  6. $cont = Read-Host -Prompt 'Type y to continue'
  7. } while($cont -ne 'y')
  8.  
  9. $cred = New-Object ñTypeName System.Management.Automation.PSCredential ñArgumentList $user, $pass
  10.  
  11. Function GetIPinfo($vm, $vmCred) {
  12. if(!$guestOSFullName) {
  13. $guestOSFullName = ($vm | Get-View).Summary.Config.guestFullName
  14. }
  15. if($guestOSFullName -match 'Windows Server') {
  16. $cmd = 'ipconfig'
  17. Write-Host -Fore:Cyan $vm.Name ':' $cmd
  18. $scriptOutputLines = (Invoke-VMScript -ScriptText $cmd -ScriptType Bat -VM $vm -GuestCredential $vmCred).ScriptOutput -split "`r`n"
  19. $gatewayLines = $scriptOutputLines | Where {$_ -match 'Default Gateway' -and (($_ -split '\:')[1] -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')}
  20. $IPv4Lines = $scriptOutputLines | Where {$_ -match 'Address' -and (($_ -split '\:')[1] -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')}
  21. $SubnetLines = $scriptOutputLines | Where {$_ -match 'Subnet Mask' -and (($_ -split '\:')[1] -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')}
  22. Write-Host -Fore:Red 'Number of IP Addresses' $IPv4Lines.Count
  23. Write-Host -Fore:Red 'Number of Subnets' $SubnetLines.Count
  24.  
  25. if($gatewayLines) {
  26. if($gatewayLines -is [Array] -and $gatewayLines.Count -gt 1) {
  27. Write-Host -Fore:Yellow $vm.Name ': Multiple default gateways found, the first one will be used..'
  28. }
  29. $gatewayLine = $gatewayLines | Select -Index 0
  30. $row.gatewayIP = ($gatewayLines -split '\:')[1].Trim()
  31.  
  32.  
  33. for($i=0; $i -lt $IPv4Lines.count; $i++){
  34. $row.NetworkIP += (($IPv4Lines |Select -Index $i) -split '\:')[1].Trim()
  35. $row.NetworkIP += " * "}
  36.  
  37. for($i=0; $i -lt $SubnetLines.count; $i++){
  38. $row.Subnet += (($SubnetLines |Select -Index $i) -split '\:')[1].Trim()
  39. $row.Subnet += " * "
  40. }
  41. }
  42. } else {
  43. Write-Host -Fore:Yellow $vm.Name ': Guest operating system is' $guestOSFullName ', treat it as Linux..'
  44. $cmd = 'netstat -nr'
  45. Write-Host -Fore:Cyan $vm.Name ':' $cmd
  46. $scriptOutputLines = (Invoke-VMScript -ScriptText $cmd -ScriptType Bash -VM $vm -GuestCredential $vmCred).ScriptOutput -split "`n"
  47. $gatewayLines = $scriptOutputLines | Where {$_.Split('\ ', [StringSplitOptions]::RemoveEmptyEntries)[1] -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'}
  48. $gatewayLines = $gatewayLines | Where {$_.Split('\ ', [StringSplitOptions]::RemoveEmptyEntries)[1] -ne '0.0.0.0'}
  49. if($gatewayLines) {
  50. if($gatewayLines -is [Array] -and $gatewayLines.Count -gt 1) {
  51. Write-Host -Fore:Yellow $vm.Name ': Multiple default gateways found, the first one will be used..'
  52. }
  53. $gatewayLine = $gatewayLines | Select -Index 0
  54. $row.gatewayIP = $gatewayLine.Split('\ ', [StringSplitOptions]::RemoveEmptyEntries)[1]
  55. c }
  56. }
  57. $gatewayIp
  58. }
  59.  
  60.  
  61. # Connecting to vCenter
  62.  
  63. Write-Host 'Connecting to' $SourceVC '..'
  64. Connect-VIServer -Server $SourceVC -Credential $cred | Out-Null
  65. if(!$DefaultVIServer) {
  66. Write-Host -Fore:Red 'Connection to vCenter' $SourceVC 'failed, exiting..'
  67. . (Join-Path -Path $scriptPath -ChildPath 'end.ps1')
  68. exit
  69. } else {
  70. Write-Host 'Connection to vCenter' $SourceVC 'succeeded'
  71. Write-Host
  72. }
  73.  
  74.  
  75.  
  76. $csv = Import-CSV -Path $ImportPath | Where {$_.ServerName}
  77. $csv | % {
  78. $_.ServerName = $_.ServerName.Trim()
  79. $_.Username = $_.Username.Trim()
  80. $_.Password = $_.Password.Trim()
  81. }
  82.  
  83. $report = @()
  84. $pgs = Get-VDPortgroup | Where {!$_.IsUplink}
  85.  
  86.  
  87.  
  88. foreach ($vmInfo in $csv){
  89.  
  90. $row = '' | Select ServerName, RAM, Status, PowerState, SourceVC, Folder, Version, OS, NetworkIP, Subnet, gatewayIP, Domain, SourceCluster, LargeVMDK, NumberOfDisks, TotalStorage,SourceDisk, RDM,'NIC #1 Portgroup','NIC #1 VLAN','NIC #1 Type','NIC #2 Portgroup','NIC #2 VLAN','NIC #2 Type'
  91.  
  92. $vm = Get-VM -Name $vmInfo.ServerName -ErrorAction:SilentlyContinue
  93.  
  94. if(!$vm) {
  95. Write-Host -Fore:Red $vminfo.ServerName ': VM not found, skipping..'
  96. $row.Servername = $vminfo.ServerName
  97. $row.Status = 'Not Found'
  98. $report += $row
  99. continue
  100. }
  101.  
  102.  
  103. #Add required number of extra fields for network adapters
  104. $nic = $vm | get-networkadapter
  105. for($i = 0; $i -lt $nic.count; $i++) {
  106. $row | Add-Member -MemberType NoteProperty -Name ('NIC #' + ($i+1) + ' Portgroup') -Value '' -Force
  107. $row | Add-Member -MemberType NoteProperty -Name ('NIC #' + ($i+1) + ' VLAN') -Value '' -Force
  108. $row | Add-Member -MemberType NoteProperty -Name ('NIC #' + ($i+1) + ' Type') -Value '' -Force
  109.  
  110. }
  111.  
  112. #Grab PG name, VLAN ID and NIC type
  113.  
  114. for($i = 0; $i -lt $nic.Count; $i++) {
  115. #$pg = $pgs | Where {($_.Name -replace '%2f','/') -eq $nic[$i].NetworkName}
  116. $pg = ($nic[$i] | Get-VDPortgroup).Name
  117. $VlanType = ($nic[$i] | Get-VDPortgroup).VlanConfiguration.vlantype
  118. $n = $i + 1
  119. if($pg) {
  120. $row."NIC #$n Portgroup" = $pg
  121. Write-Host "Portgroup is "$pg
  122. $row."NIC #$n Type" = $nic[$i].Type
  123. if(!$VlanType) {
  124. $row."NIC #$n VLAN" = 0
  125. } elseif($VlanType -eq 'Vlan') {
  126. $row."NIC #$n VLAN" = ($nic[$i] | Get-VDPortgroup).VlanConfiguration.vlanid
  127. } elseif($pg.VlanConfiguration.VlanType -eq 'Trunk') {
  128. $row."NIC #$n VLAN" = ($nic[$i] | Get-VDPortgroup).VlanConfiguration.Ranges.StartVlanId.ToString() + '-' + $pg.VlanConfiguration.Ranges.EndVlanId.ToString()
  129. } elseif($pg.VlanConfiguration.VlanType -eq 'PrivateVlan') {
  130. $row."NIC #$n VLAN" = ($nic[$i] | Get-VDPortgroup).VlanConfiguration.PrimaryVlanId.ToString() + '#' + $pg.VlanConfiguration.SecondaryVlanId.ToString() + '#' + $pg.VlanConfiguration.PrivateVlanType
  131. } else {
  132. Write-Host -Fore:Red 'Unknown VLAN type' $pg.VlanConfiguration.VlanType ', skipping..'
  133. continue
  134. }
  135. } else {
  136. Write-Host -Fore:Red $vm.Name ': NIC #' $n 'Portgroup' $pg.Name 'not found from distributed switch'
  137. continue
  138. }
  139. }
  140.  
  141. $hdd = $vm | get-harddisk
  142. #Grab general information
  143.  
  144. $row.ServerName = $vm.Name
  145. $row.PowerState = $vm.PowerState
  146. $row.RAM = $vm.MemoryGB
  147. $row.SourceVC = $SourceVC
  148. If($vm.Folder.Name -eq 'vm'){
  149. $row.Folder = 'Root'
  150. }
  151. else {$row.Folder = $vm.Folder.Name}
  152. $row.Version = $vm.Version
  153. $row.OS = $vm.Guest.OSFullName
  154. $row.NumberOfDisks = $vm.HardDisks.Count
  155. $row.TotalStorage = [math]::round($vm.ProvisionedSpaceGB).ToString() + "GB"
  156. $hostname=$vm.Host.Name
  157. $row.SourceCluster = (get-vmhost -Name $hostname).Parent.Name
  158.  
  159. if(($vm.PowerState -eq 'PoweredOn') -and ($vm.ExtensionData.Guest.ToolsRunningStatus -eq "guestToolsRunning")){
  160. $fqdn=$vm.Guest.Hostname
  161. $pos=$fqdn.IndexOf(".")
  162. $row.Domain = $fqdn.Substring($pos+1)}
  163.  
  164. $row.RDM = !!($vm | get-harddisk -disktype RawPhysical,RawVirtual)
  165.  
  166. #Collect source datastore information
  167.  
  168.  
  169. $row.LargeVMDK = "False"
  170. for($i=0; $i -lt $hdd.count; $i++){
  171. $filename=$hdd[$i].Filename
  172. $DSpos=$filename.IndexOf(" ")
  173. $datastore=$filename.Substring(0, $DSpos)
  174. $row.SourceDisk += $datastore
  175. $row.SourceDisk += '*'
  176. if ($hdd[$i].CapacityGB -ge 2032){
  177. $row.LargeVMDK = "True"}
  178. }
  179.  
  180.  
  181. # Collecting IP Address, Subnet Mask and Gateway
  182.  
  183. if($vm.PowerState -eq 'PoweredOn'){
  184. # Skip the VM if tools not running
  185. if(!($vm.ExtensionData.Guest.ToolsRunningStatus -eq "guestToolsRunning")) {
  186. Write-Host -Fore:Red $vm.Name ': VMware Tools is not running so script cannot be executed within VM, skipping..'
  187. $row.Status = 'VMware Tools not running'
  188. $report += $row
  189. continue
  190. }
  191.  
  192. # Skip the VM if credential is not provided
  193. if( !($vminfo.Username -and $vminfo.Password) ) {
  194. Write-Host -Fore:Red $vm.Name ': Username or password is empty, skipping..'
  195. $row.Status = 'Credentials are not provided'
  196. $report += $row
  197. continue
  198. }
  199.  
  200. # Produce a credential to log into VM guest operating system
  201. $guestUser = $vminfo.Username
  202. $guestPass = ConvertTo-SecureString ñString $vminfo.Password ñAsPlainText -Force
  203. $vmCred = New-Object ñTypeName System.Management.Automation.PSCredential ñArgumentList $guestUser,$guestPass
  204.  
  205. # Get the guest OS full name to reduce number of queries
  206. $guestOSFullName = ($vm | Get-View).Summary.Config.guestFullName
  207.  
  208. # Get gateway IP
  209. $gatewayIp = GetIPinfo $vm $vmCred
  210.  
  211. }
  212.  
  213. $report += $row
  214.  
  215. }
  216. $report | Export-Csv -Path F:\Migration\CSV\Runsheet_data\report.csv -NoTypeInformation -Force
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement