Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- param([String]$ImportPath, [String]$SourceVC)
- do {
- $user = Read-Host -Prompt 'Enter username for source vCenter'
- $pass = Read-Host -AsSecureString -Prompt 'Enter password for source vCenter'
- $cont = Read-Host -Prompt 'Type y to continue'
- } while($cont -ne 'y')
- $cred = New-Object ñTypeName System.Management.Automation.PSCredential ñArgumentList $user, $pass
- Function GetIPinfo($vm, $vmCred) {
- if(!$guestOSFullName) {
- $guestOSFullName = ($vm | Get-View).Summary.Config.guestFullName
- }
- if($guestOSFullName -match 'Windows Server') {
- $cmd = 'ipconfig'
- Write-Host -Fore:Cyan $vm.Name ':' $cmd
- $scriptOutputLines = (Invoke-VMScript -ScriptText $cmd -ScriptType Bat -VM $vm -GuestCredential $vmCred).ScriptOutput -split "`r`n"
- $gatewayLines = $scriptOutputLines | Where {$_ -match 'Default Gateway' -and (($_ -split '\:')[1] -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')}
- $IPv4Lines = $scriptOutputLines | Where {$_ -match 'Address' -and (($_ -split '\:')[1] -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')}
- $SubnetLines = $scriptOutputLines | Where {$_ -match 'Subnet Mask' -and (($_ -split '\:')[1] -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')}
- Write-Host -Fore:Red 'Number of IP Addresses' $IPv4Lines.Count
- Write-Host -Fore:Red 'Number of Subnets' $SubnetLines.Count
- if($gatewayLines) {
- if($gatewayLines -is [Array] -and $gatewayLines.Count -gt 1) {
- Write-Host -Fore:Yellow $vm.Name ': Multiple default gateways found, the first one will be used..'
- }
- $gatewayLine = $gatewayLines | Select -Index 0
- $row.gatewayIP = ($gatewayLines -split '\:')[1].Trim()
- for($i=0; $i -lt $IPv4Lines.count; $i++){
- $row.NetworkIP += (($IPv4Lines |Select -Index $i) -split '\:')[1].Trim()
- $row.NetworkIP += " * "}
- for($i=0; $i -lt $SubnetLines.count; $i++){
- $row.Subnet += (($SubnetLines |Select -Index $i) -split '\:')[1].Trim()
- $row.Subnet += " * "
- }
- }
- } else {
- Write-Host -Fore:Yellow $vm.Name ': Guest operating system is' $guestOSFullName ', treat it as Linux..'
- $cmd = 'netstat -nr'
- Write-Host -Fore:Cyan $vm.Name ':' $cmd
- $scriptOutputLines = (Invoke-VMScript -ScriptText $cmd -ScriptType Bash -VM $vm -GuestCredential $vmCred).ScriptOutput -split "`n"
- $gatewayLines = $scriptOutputLines | Where {$_.Split('\ ', [StringSplitOptions]::RemoveEmptyEntries)[1] -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'}
- $gatewayLines = $gatewayLines | Where {$_.Split('\ ', [StringSplitOptions]::RemoveEmptyEntries)[1] -ne '0.0.0.0'}
- if($gatewayLines) {
- if($gatewayLines -is [Array] -and $gatewayLines.Count -gt 1) {
- Write-Host -Fore:Yellow $vm.Name ': Multiple default gateways found, the first one will be used..'
- }
- $gatewayLine = $gatewayLines | Select -Index 0
- $row.gatewayIP = $gatewayLine.Split('\ ', [StringSplitOptions]::RemoveEmptyEntries)[1]
- c }
- }
- $gatewayIp
- }
- # Connecting to vCenter
- Write-Host 'Connecting to' $SourceVC '..'
- Connect-VIServer -Server $SourceVC -Credential $cred | Out-Null
- if(!$DefaultVIServer) {
- Write-Host -Fore:Red 'Connection to vCenter' $SourceVC 'failed, exiting..'
- . (Join-Path -Path $scriptPath -ChildPath 'end.ps1')
- exit
- } else {
- Write-Host 'Connection to vCenter' $SourceVC 'succeeded'
- Write-Host
- }
- $csv = Import-CSV -Path $ImportPath | Where {$_.ServerName}
- $csv | % {
- $_.ServerName = $_.ServerName.Trim()
- $_.Username = $_.Username.Trim()
- $_.Password = $_.Password.Trim()
- }
- $report = @()
- $pgs = Get-VDPortgroup | Where {!$_.IsUplink}
- foreach ($vmInfo in $csv){
- $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'
- $vm = Get-VM -Name $vmInfo.ServerName -ErrorAction:SilentlyContinue
- if(!$vm) {
- Write-Host -Fore:Red $vminfo.ServerName ': VM not found, skipping..'
- $row.Servername = $vminfo.ServerName
- $row.Status = 'Not Found'
- $report += $row
- continue
- }
- #Add required number of extra fields for network adapters
- $nic = $vm | get-networkadapter
- for($i = 0; $i -lt $nic.count; $i++) {
- $row | Add-Member -MemberType NoteProperty -Name ('NIC #' + ($i+1) + ' Portgroup') -Value '' -Force
- $row | Add-Member -MemberType NoteProperty -Name ('NIC #' + ($i+1) + ' VLAN') -Value '' -Force
- $row | Add-Member -MemberType NoteProperty -Name ('NIC #' + ($i+1) + ' Type') -Value '' -Force
- }
- #Grab PG name, VLAN ID and NIC type
- for($i = 0; $i -lt $nic.Count; $i++) {
- #$pg = $pgs | Where {($_.Name -replace '%2f','/') -eq $nic[$i].NetworkName}
- $pg = ($nic[$i] | Get-VDPortgroup).Name
- $VlanType = ($nic[$i] | Get-VDPortgroup).VlanConfiguration.vlantype
- $n = $i + 1
- if($pg) {
- $row."NIC #$n Portgroup" = $pg
- Write-Host "Portgroup is "$pg
- $row."NIC #$n Type" = $nic[$i].Type
- if(!$VlanType) {
- $row."NIC #$n VLAN" = 0
- } elseif($VlanType -eq 'Vlan') {
- $row."NIC #$n VLAN" = ($nic[$i] | Get-VDPortgroup).VlanConfiguration.vlanid
- } elseif($pg.VlanConfiguration.VlanType -eq 'Trunk') {
- $row."NIC #$n VLAN" = ($nic[$i] | Get-VDPortgroup).VlanConfiguration.Ranges.StartVlanId.ToString() + '-' + $pg.VlanConfiguration.Ranges.EndVlanId.ToString()
- } elseif($pg.VlanConfiguration.VlanType -eq 'PrivateVlan') {
- $row."NIC #$n VLAN" = ($nic[$i] | Get-VDPortgroup).VlanConfiguration.PrimaryVlanId.ToString() + '#' + $pg.VlanConfiguration.SecondaryVlanId.ToString() + '#' + $pg.VlanConfiguration.PrivateVlanType
- } else {
- Write-Host -Fore:Red 'Unknown VLAN type' $pg.VlanConfiguration.VlanType ', skipping..'
- continue
- }
- } else {
- Write-Host -Fore:Red $vm.Name ': NIC #' $n 'Portgroup' $pg.Name 'not found from distributed switch'
- continue
- }
- }
- $hdd = $vm | get-harddisk
- #Grab general information
- $row.ServerName = $vm.Name
- $row.PowerState = $vm.PowerState
- $row.RAM = $vm.MemoryGB
- $row.SourceVC = $SourceVC
- If($vm.Folder.Name -eq 'vm'){
- $row.Folder = 'Root'
- }
- else {$row.Folder = $vm.Folder.Name}
- $row.Version = $vm.Version
- $row.OS = $vm.Guest.OSFullName
- $row.NumberOfDisks = $vm.HardDisks.Count
- $row.TotalStorage = [math]::round($vm.ProvisionedSpaceGB).ToString() + "GB"
- $hostname=$vm.Host.Name
- $row.SourceCluster = (get-vmhost -Name $hostname).Parent.Name
- if(($vm.PowerState -eq 'PoweredOn') -and ($vm.ExtensionData.Guest.ToolsRunningStatus -eq "guestToolsRunning")){
- $fqdn=$vm.Guest.Hostname
- $pos=$fqdn.IndexOf(".")
- $row.Domain = $fqdn.Substring($pos+1)}
- $row.RDM = !!($vm | get-harddisk -disktype RawPhysical,RawVirtual)
- #Collect source datastore information
- $row.LargeVMDK = "False"
- for($i=0; $i -lt $hdd.count; $i++){
- $filename=$hdd[$i].Filename
- $DSpos=$filename.IndexOf(" ")
- $datastore=$filename.Substring(0, $DSpos)
- $row.SourceDisk += $datastore
- $row.SourceDisk += '*'
- if ($hdd[$i].CapacityGB -ge 2032){
- $row.LargeVMDK = "True"}
- }
- # Collecting IP Address, Subnet Mask and Gateway
- if($vm.PowerState -eq 'PoweredOn'){
- # Skip the VM if tools not running
- if(!($vm.ExtensionData.Guest.ToolsRunningStatus -eq "guestToolsRunning")) {
- Write-Host -Fore:Red $vm.Name ': VMware Tools is not running so script cannot be executed within VM, skipping..'
- $row.Status = 'VMware Tools not running'
- $report += $row
- continue
- }
- # Skip the VM if credential is not provided
- if( !($vminfo.Username -and $vminfo.Password) ) {
- Write-Host -Fore:Red $vm.Name ': Username or password is empty, skipping..'
- $row.Status = 'Credentials are not provided'
- $report += $row
- continue
- }
- # Produce a credential to log into VM guest operating system
- $guestUser = $vminfo.Username
- $guestPass = ConvertTo-SecureString ñString $vminfo.Password ñAsPlainText -Force
- $vmCred = New-Object ñTypeName System.Management.Automation.PSCredential ñArgumentList $guestUser,$guestPass
- # Get the guest OS full name to reduce number of queries
- $guestOSFullName = ($vm | Get-View).Summary.Config.guestFullName
- # Get gateway IP
- $gatewayIp = GetIPinfo $vm $vmCred
- }
- $report += $row
- }
- $report | Export-Csv -Path F:\Migration\CSV\Runsheet_data\report.csv -NoTypeInformation -Force
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement