vNuggletsUtilityMod_functions.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 |
function Find-VNVMWithDuplicateMACAddress { <# .Description Get information about the VM(s) that have a network adapter whose MAC address is a duplicate of another network adapter in the vCenter(s) to which this PowerCLI session is connected .Synopsis Get information about duplicate MAC addresses .Example Find-VNVMWithDuplicateMACAddress VMName DuplicatedMAC MoRef Count ------ ------------- ----- ----- {myVM03, oldVM322} 00:50:56:3F:FF:FF {VirtualMachine-vm-16277, VirtualMachine-vm-109} 2 Find VMs with network adapters whose MAC address is the same, and return a bit of info about them .Example Find-VNVMWithDuplicateMACAddress VMName DuplicatedMAC MoRef Count ------ ------------- ----- ----- myVM21 00:50:56:00:00:09 VirtualMachine-vm-16277 2 Find VM (just one, apparently, in this vCenter) with network adapters whose MAC address is the same -- in this case, the VM has at least two network adapters, both of which have the same MAC address .Link http://vNugglets.com .Outputs System.Management.Automation.PSCustomObject #> [CmdletBinding()] [OutputType([System.Management.Automation.PSCustomObject])] Param () ## end param process { ## get VirtualMachine .NET views where the items is not marked as a Template $colDevMacAddrInfo = ` Get-View -ViewType VirtualMachine -Property Name,Config.Hardware.Device -Filter @{"Config.Template" = "False"} | Foreach-Object { $viewThisVM = $_ $_.Config.Hardware.Device | Where-Object {$_ -is [VMware.Vim.VirtualEthernetCard]} | Foreach-Object { New-Object -Type PSObject -Property @{VMName = $viewThisVM.Name; MacAddr = $_.MacAddress; MoRef = $viewThisVM.MoRef} } ## end foreach-object } ## end foreach-object ## get the non-unique MAC addresses (if any), $arrDuplicatedMAC_GroupInfo = $colDevMacAddrInfo | Group-Object MacAddr | Where-Object {$_.count -gt 1} ## for each duplicated MAC, return an object with the given properties if ($null -ne $arrDuplicatedMAC_GroupInfo) { $arrDuplicatedMAC_GroupInfo | Foreach-Object { New-Object -Type PSObject -Property ([ordered]@{ VMName = $_.Group | Foreach-Object {$_.VMName} | Select-Object -Unique DuplicatedMAC = $_.Name MoRef = $_.Group | Foreach-Object {$_.MoRef} | Select-Object -Unique Count = $_.Count }) ## end new-object } ## end foreach-object } ## end if else {Write-Verbose "no duplicate MAC addresses found on non-template VMs"} } ## end process } ## end function function Get-VNNetworkClusterInfo { <# .Description Get information about the VMware HA Cluster(s) in which the given virtual network (virtual portgroup) is defined. May 2015 .Synopsis Get VMware HA Cluster info for a virtual network .Example Get-VNNetworkClusterInfo 101,234 Name ClusterName ClusterId Type MoRef ---- ----------- --------- ---- ----- my.Portgrp101 myCluster0 ClusterCom...n-c94 VMware.Vim.Network Network-network-3588 my.Portgrp234 myCluster20 ClusterCom...n-c99 VMware.Vim.DistributedVirtualPortgroup Network-network-4687 Gets information about virtual networks whose names match the (very simple) regular expressions, "101" and "121", returning an object for each matching network with network name and cluster name/ID properties .Example Get-VNNetworkClusterInfo -LiteralName my.Portgroup0 | ft -a Name ClusterName ClusterId Type MoRef ---- ----------- --------- ---- ----- my.Portgroup0 myCluster0 ClusterComputeResource-domain-c94 VMware.Vim.Network Network-network-3588 Gets information about virtual networks whose names are literally "my.Portgroup0" (not matching "my.Portgroup01" or "test_myXPortgroup0" -- just a literal match only), returning an object with network name and cluster name/ID properties .Link http://vNugglets.com .Notes This code also gives a nice example of using LinkedViews in .NET View objects to most quickly/efficiently get information about related objects in the vSphere environment. .Inputs String -- network (virtual portgroup) name pattern(s) or literal name(s) .Outputs System.Management.Automation.PSCustomObject #> [CmdletBinding(DefaultParameterSetName="NameAsRegEx")] [OutputType([System.Management.Automation.PSCustomObject])] Param ( ## Name pattern of virtual network for which to get information. This is a regular expression [parameter(Mandatory=$true,ParameterSetName="NameAsRegEx",Position=0)][String[]]$Name, ## Literal name of virtual network for which to get information. This RegEx-escapes the string and adds start/end anchors ("^" and "$") so that the only match is an exact match [parameter(Mandatory=$true,ParameterSetName="NameAsLiteral",Position=0)][String[]]$LiteralName ) ## end param process { $hshParamForNewRegExPattern = Switch ($PsCmdlet.ParameterSetName) { "NameAsRegEx" {@{String = $Name; EscapeAsLiteral = $false}; break} ## if literal, do escape as literal "NameAsLiteral" {@{String = $LiteralName; EscapeAsLiteral = $true}} } ## end switch ## make the actual RegEx pattern, joining all values, and escaping if strings are to be literal $strNetworkNameFilter = _New-RegExJoinedOrPattern @hshParamForNewRegExPattern Get-View -ViewType Network -Property Name,Host -Filter @{Name = $strNetworkNameFilter} | Foreach-Object { ## get the updated View data for the network's hosts' parent's name $_.UpdateViewData("Host.Parent.Name") New-Object -Type PSObject -Property ([ordered]@{ Name = $_.Name ## the name(s) of the cluster(s) in which this network is defined ClusterName = $_.LinkedView.Host.LinkedView.Parent.Name | Select-Object -Unique ## the ID(s) of the cluster(s) in which this network is defined ClusterId = $_.LinkedView.Host.LinkedView.Parent.MoRef | Select-Object -Unique Type = ($_ | Get-Member).TypeName | Select-Object -Unique ## the network's MoRef (for ease of getting the exact network object subsequently) MoRef = $_.MoRef }) ## end new-object } ## end foreach-object } ## end process } ## end function function Get-VNVMHostBrokenUplink { <# .Description For the given VMHost(s), list all VMNICs that are connected to a virtual standard switch (vSSwitch), but that have no link .Example Get-VNVMHostBrokenUplink VMHost vSwitch BustedVmnic BitRatePerSec ------ ------- ----------- ------------- myhost03.dom.com vSwitch0 vmnic1 0 myhost22.dom.com vSwitch5 vmnic7 0 myhost24.dom.com vSwitch1 vmnic3 0 Get information for all VMHost's vSSwitches' uplinks .Example Get-VNVMHostBrokenUplink -LiteralName myhost24.dom.com VMHost vSwitch BustedVmnic BitRatePerSec ------ ------- ----------- ------------- myhost24.dom.com vSwitch1 vmnic3 0 Get information for the particular VMHost's vSSwitches' uplinks .Link http://vNugglets.com .Outputs System.Management.Automation.PSCustomObject #> [CmdletBinding(DefaultParameterSetName="NameAsRegEx")] [OutputType([System.Management.Automation.PSCustomObject])] Param ( ## Name pattern of VMHost for which to get information. This is a regular expression. If none specified, will check all VMHosts [parameter(ParameterSetName="NameAsRegEx",Position=0)][String[]]$Name = ".+", ## Literal name of VMHost for which to get information. This RegEx-escapes the string and adds start/end anchors ("^" and "$") so that the only match is an exact match [parameter(Mandatory=$true,ParameterSetName="NameAsLiteral",Position=0)][String[]]$LiteralName ) ## end param process { $hshParamForNewRegExPattern = Switch ($PsCmdlet.ParameterSetName) { "NameAsRegEx" {@{String = $Name; EscapeAsLiteral = $false}; break} ## if literal, do escape as literal "NameAsLiteral" {@{String = $LiteralName; EscapeAsLiteral = $true}} } ## end switch ## make the actual RegEx pattern, joining all values, and escaping if strings are to be literal $strVMHostNameFilter = _New-RegExJoinedOrPattern @hshParamForNewRegExPattern ## get all matching HostSystems, and for all of their vSwitches, find the Pnics that do not have a link or the link speed is 0 Get-View -ViewType HostSystem -Property Name, Config.Network.Vswitch, Config.Network.Pnic -Filter @{"Name" = $strVMHostNameFilter} | Foreach-Object { $viewThisHost = $_ Write-Verbose "working on VMHost '$($viewThisHost.Name)'" ## for each vSwitch (that has uplinks) on the host, check the Pnics $viewThisHost.Config.Network.Vswitch | Where-Object {$_.Pnic} | Foreach-Object { $oThisVswitch = $_ ## for each Pnic key in this vSwitch $oThisVswitch.Pnic | Foreach-Object { $strPnicKey = $_ ## get the actual Pnic, check its LinkSpeed $oPnic = $viewThisHost.Config.Network.Pnic | Where-Object {$_.key -eq $strPnicKey} if (($null -eq $oPnic.LinkSpeed) -or ($oPnic.LinkSpeed.SpeedMb -eq 0)) { ## create a new object with some info about the Pnic New-Object -Type PSObject -Property ([ordered]@{ VMHost = $viewThisHost.Name vSwitch = $oThisVswitch.Name BustedVmnic = $oPnic.Device BitRatePerSec = if ($null -eq $oPnic.LinkSpeed.SpeedMb) {0} else {$oPnic.LinkSpeed.SpeedMb} }) ## end new-object } ## end if } ## end foreach-object } ## end foreach-object } ## end foreach-object } ## end process } ## end fn function Get-VNVMByAddress { <# .Description Find all VMs with a NIC that has the given MAC address or IP address (explicit or wildcard). Get-by-MAC portion written Jul 2011. .Example Get-VNVMByAddress -MAC 00:50:56:b0:00:01 Name MacAddress MoRef ------ ------------ ----- myvm0 {00:50:56:b0:00:01,00:50:56:b0:00:18} VirtualMachine-vm-2155 Get VMs with given MAC address, return VM name and its MAC addresses .Example Get-VNVMByAddress -IP 10.37.31.12 Name IP MoRef Client ------ --- ----- ------ myvm10 {192.16.13.1, 10.37.31.12, fe80::000...} VirtualMachine-vm-13 VMware.Vim.VimClientImpl Get VMs with given IP as reported by VMware Tools, return VM name and its IP addresses .Example Get-VNVMByAddress -AddressWildcard 10.0.0.* Name IP MoRef ---- -- ----- myvm3 {10.0.0.20, fe80::000:5600:fe00:6007} VirtualMachine-vm-153 mytestVM001 10.0.0.200 VirtualMachine-vm-162 Use -AddressWildcard to find VMs with approximate IP .Example Get-VNVMByAddress -IP 10.37.31.12 Name GuestHostname MoRef Client ------ ------------- ----- ------ myvm10 myvm10.dom.com VirtualMachine-vm-13 VMware.Vim.VimClientImpl Get VMs with given hostname configured in the guest OS as reported by VMware Tools, return VM name and its guest hostname .Example Get-VNVMByAddress -Uuid b99b546a-ee00-43f3-856a-80779ffddd0e Name Uuid MoRef Client ------ ---- ----- ------ myvm37 b99b546a-ee00-43f3-856a-80779ffddd0e VirtualMachine-vm-19991 VMware.Vim.VimClientImpl Get VMs with given SMBIOS UUID, return VM name and its UUID .Link Get-VNVMByRDM Get-VNVMByVirtualPortGroup http://vNugglets.com .Notes Finding VMs by IP address / Guest hostname relies on information returned from VMware Tools in the guest, so VMware Tools must be installed in the guest and have been running in the guest at least recently. .Outputs Selected.VMware.Vim.VirtualMachine #> [CmdletBinding(DefaultParametersetName="FindByMac")] [OutputType([System.Management.Automation.PSCustomObject])] param ( ## MAC address in question, if finding VM by MAC; expects address in format "00:50:56:00:00:01" [parameter(Mandatory=$true,ParameterSetName="FindByMac",Position=0)][string[]]$MAC, ## IP address in question, for finding VM by IP. Accepts IPv4 and IPv6 addresses [parameter(Mandatory=$true,ParameterSetName="FindByIP",Position=0)][ValidateScript({[bool][System.Net.IPAddress]::Parse($_)})][string]$IP, ## wildcard string IP address (standard wildcards like "10.0.0.*"), if finding VM by approximate IP [parameter(Mandatory=$true,ParameterSetName="FindByIPWildcard",Position=0)][string]$AddressWildcard, ## Fully qualified DNS hostname as it appears in guest OS, for finding VM by guest hostname [parameter(Mandatory=$true,ParameterSetName="FindByGuestHostname",Position=0)][string]$GuestHostname, ## VM SMBIOS UUID, for finding VM by UUID [parameter(Mandatory=$true,ParameterSetName="FindByUuid",Position=0)][string]$Uuid ) ## end param begin { ## array of properties to select on VirtualMachine object return when searching by IP or IP Wildcard, by Guest hostname, etc. $arrPropertiesForReturnWhenSearchByIP = "Name", @{n="IP"; e={$_.Guest.Net | Foreach-Object {$_.IpAddress} | Sort-Object}}, "MoRef", "Client" $arrPropertiesForReturnWhenSearchByGuestHostname = "Name", @{n="GuestHostname"; e={$_.Guest.HostName}}, "MoRef", "Client" $arrPropertiesForReturnWhenSearchByUuid = "Name", @{n="Uuid"; e={$_.Config.Uuid}}, "MoRef", "Client" } ## end begin Process { Switch ($PsCmdlet.ParameterSetName) { "FindByMac" { ## return the some info for the VM(s) with the NIC w/ the given MAC Get-View -Viewtype VirtualMachine -Property Name, Config.Hardware.Device | Where-Object {$_.Config.Hardware.Device | Where-Object {($_ -is [VMware.Vim.VirtualEthernetCard]) -and ($MAC -contains $_.MacAddress)}} | Select-Object Name, @{n="MacAddress"; e={$_.Config.Hardware.Device | Where-Object {$_ -is [VMware.Vim.VirtualEthernetCard]} | Foreach-Object {$_.MacAddress} | Sort-Object}}, MoRef break } ## end case "FindByIp" { ## get the SearchIndex object(s) (one from each connected vCenter) Get-View -Id SearchIndex-SearchIndex -PipelineVariable viewThisSearchIndex | Foreach-Object { ## the vCenter name for this SearchIndex (ServiceUrl is like "https://myvcenter.dom.com/sdk", and the .Host property of a .NET URI object is just the DNS hostname portion of the URI) $strVcenterOfThisSearchIndex = ([System.Uri]$viewThisSearchIndex.Client.ServiceUrl).Host ## SearchIndex Find* methods return MoRefs; docs at http://pubs.vmware.com/vsphere-65/index.jsp#com.vmware.wssdk.apiref.doc/vim.SearchIndex.html # FindAllByIp(moref datacenter*, string ip, bool vmSearch) $viewThisSearchIndex.FindAllByIp($null, $IP, $true) | Select-Object -Unique | Foreach-Object {Get-View -Id $_ -Property Name, Guest.Net -Server $strVcenterOfThisSearchIndex} | Select-Object -Property $arrPropertiesForReturnWhenSearchByIP } ## end foreach-object break } ## end case "FindByGuestHostname" { ## get the SearchIndex object(s) (one from each connected vCenter) Get-View -Id SearchIndex-SearchIndex -PipelineVariable viewThisSearchIndex | Foreach-Object { ## the vCenter name for this SearchIndex (ServiceUrl is like "https://myvcenter.dom.com/sdk", and the .Host property of a .NET URI object is just the DNS hostname portion of the URI) $strVcenterOfThisSearchIndex = ([System.Uri]$viewThisSearchIndex.Client.ServiceUrl).Host ## SearchIndex Find* methods return MoRefs; docs at http://pubs.vmware.com/vsphere-65/index.jsp#com.vmware.wssdk.apiref.doc/vim.SearchIndex.html # FindAllByDnsName(moref datacenter*, string dnsName, bool vmSearch) $viewThisSearchIndex.FindAllByDnsName($null, $GuestHostname, $true) | Select-Object -Unique | Foreach-Object {Get-View -Id $_ -Property Name, Guest.HostName -Server $strVcenterOfThisSearchIndex} | Select-Object -Unique -Property $arrPropertiesForReturnWhenSearchByGuestHostname } ## end foreach-object break } ## end case "FindByUuid" { ## get the SearchIndex object(s) (one from each connected vCenter) Get-View -Id SearchIndex-SearchIndex -PipelineVariable viewThisSearchIndex | Foreach-Object { ## the vCenter name for this SearchIndex (ServiceUrl is like "https://myvcenter.dom.com/sdk", and the .Host property of a .NET URI object is just the DNS hostname portion of the URI) $strVcenterOfThisSearchIndex = ([System.Uri]$viewThisSearchIndex.Client.ServiceUrl).Host ## SearchIndex Find* methods return MoRefs; docs at http://pubs.vmware.com/vsphere-65/index.jsp#com.vmware.wssdk.apiref.doc/vim.SearchIndex.html # FindAllByUuid(moref datacenter*, string uuid, bool vmSearch, bool instanceUuid*) $viewThisSearchIndex.FindAllByUuid($null, $Uuid, $true, $false) | Select-Object -Unique | Foreach-Object {Get-View -Id $_ -Property Name, Config.Uuid -Server $strVcenterOfThisSearchIndex} | Select-Object -Unique -Property $arrPropertiesForReturnWhenSearchByUuid } ## end foreach-object break } ## end case "FindByIPWildcard" { ## scriptblock to use for the Where clause in finding VMs $sblkFindByIP_WhereStatement = {$_.IpAddress | Where-Object {$_ -like $AddressWildcard}} ## return the .Net View object(s) for the VM(s) with the NIC(s) w/ the given IP Get-View -Viewtype VirtualMachine -Property Name, Guest.Net | Where-Object {$_.Guest.Net | Where-Object $sblkFindByIP_WhereStatement} | Select-Object -Property $arrPropertiesForReturnWhenSearchByIP } ## end case } ## end switch } ## end process } ## end fn function Get-VNVMByRDM { <# .Description Function to find what VM(s) (if any) are using a LUN as an RDM, based on the LUN's SCSI canonical name. Assumes that the best practice of all hosts in a cluster seeing the same LUNs is followed. .Example Get-VNVMByRDM -CanonicalName naa.60000112233445501000000000000001 -Cluster someCluster VMName : myvm002 HardDiskName : Hard disk 7 CompatibilityMode : physicalMode CanonicalName : naa.60000112233445501000000000000001 DeviceDisplayName : myvm002-logs MoRef : VirtualMachine-vm-174 Find VM using the given LUN as an RDM, returning an object with the information about that RDM harddisk on the VM .Example Get-VNVMByRDM -CanonicalName naa.60000112233445501000000000000002 -Cluster someCluster | ft -a VMName, HardDiskName, DeviceDisplayName, CanonicalName VMName HardDiskName DeviceDisplayName CanonicalName ------ ---------- ----------------- ------------- myvm0050 Hard disk 10 myMSCluster-quorum naa.60000112233445501000000000000002 myvm0051 Hard disk 10 myMSCluster-quorum naa.60000112233445501000000000000002 Find VMs using the given LUN as an RDM, formatting output in auto-sized table with just the given properties .Link Get-VNVMByAddress Get-VNVMByVirtualPortGroup http://vNugglets.com .Outputs Zero or more PSObjects with info about the VM and its corresponding RDM disk #> [CmdletBinding()] [OutputType([System.Management.Automation.PSCustomObject])] Param( ## The canonical name(s) of the LUN(s) in question (the LUNs used as RDMs) [parameter(Mandatory=$true)][string[]]$CanonicalName, ## The cluster whose hosts see this LUN [parameter(Mandatory=$true)][string]$ClusterName ) ## end param process { ## get the View object of the cluster in question $viewCluster = Get-View -ViewType ClusterComputeResource -Property Name -Filter @{"Name" = "^$([RegEx]::escape($ClusterName))$"} ## get the View of a host in the given cluster (presumably all hosts in the cluster see the same storage) $viewHostInGivenCluster = Get-View -ViewType HostSystem -Property Name -SearchRoot $viewCluster.MoRef | Get-Random ## get the Config.StorageDevice.ScsiLun property of the host (retrieved _after_ getting the View object for speed, as this property is only retrieved for this object, not all hosts' View objects) $viewHostInGivenCluster.UpdateViewData("Config.StorageDevice.ScsiLun") ## get the View objects for all VMs in the given cluster Get-View -ViewType VirtualMachine -Property Name, Config.Hardware.Device -SearchRoot $viewCluster.MoRef | Foreach-Object {$viewThisVM = $_ ## for all of the RDM devices on this VM, see if the canonical name matches the canonical name in question $viewThisVM.Config.Hardware.Device | Where-Object {($_ -is [VMware.Vim.VirtualDisk]) -and ("physicalMode","virtualMode" -contains $_.Backing.CompatibilityMode)} | Foreach-Object { $hdThisDisk = $_ $lunScsiLunOfThisDisk = $viewHostInGivenCluster.Config.StorageDevice.ScsiLun | Where-Object {$_.UUID -eq $hdThisDisk.Backing.LunUuid} ## if the canonical names match, create a new PSObject with some info about the VirtualDisk and the VM using it if ($CanonicalName -contains $lunScsiLunOfThisDisk.CanonicalName) { New-Object -TypeName PSObject -Property ([ordered]@{ VMName = $viewThisVM.Name HardDiskName = $hdThisDisk.DeviceInfo.Label CompatibilityMode = $_.Backing.CompatibilityMode CanonicalName = $lunScsiLunOfThisDisk.CanonicalName DeviceDisplayName = $lunScsiLunOfThisDisk.DisplayName MoRef = $viewThisVM.MoRef }) ## end new-object } ## end if } ## end where-object } ## end where-object } ## end process } ## end fn function Get-VNVMByVirtualPortGroup { <# .Description Function to get information about which VMs are on a given virtual network (a.k.a. "virtual portgroup") .Example Get-VNVMByVirtualPortGroup -NetworkName VLAN19 | ft VMName,Network,VMHost,Cluster VMName Network VMHost Cluster ------ ----------- ------ ------- vm0.dom.com VLAN19.Sekurr vmhost0.dom.com myCluster0 vm10.dom.com VLAN19.Sekurr vmhost3.dom.com myCluster0 vm32.dom.com VLAN19.Sekurr vmhost2.dom.com myCluster0 ... Get networks matching "VLAN19", and get their VMs' names and some other VM information .Example Get-VNVMByVirtualPortGroup -NetworkLiteralName VLAN3 | ft -a VMName Network VMHost Cluster PowerState MoRef ------ ------- ------ ------- ---------- ----- myVM0 VLAN3 myVMHost.dom.com myCluster2 poweredOn VirtualMachine-vm-142 ... Get network named exactly "VLAN3", and get its VMs' names and some other VM information .Link Get-VNVMByAddress Get-VNVMByRDM http://vNugglets.com .Outputs System.Management.Automation.PSCustomObject #> [CmdletBinding(DefaultParameterSetName="NameAsRegEx")] [OutputType([System.Management.Automation.PSCustomObject])] param( ## Name of the virtual network (virtual portgroup) to get. This is a Regular Expression pattern [parameter(Mandatory=$true,ParameterSetName="NameAsRegEx",Position=0)][string[]]$NetworkName, ## Literal name of virtual network for which to get information. This RegEx-escapes the string and adds start/end anchors ("^" and "$") so that the only match is an exact match [parameter(Mandatory=$true,ParameterSetName="NameAsLiteral",Position=0)][String[]]$NetworkLiteralName ) ## end param process { ## make the params for generating the new RegEx pattern $hshParamForNewRegExPattern = Switch ($PsCmdlet.ParameterSetName) { "NameAsRegEx" {@{String = $NetworkName; EscapeAsLiteral = $false}; break} ## if literal, do escape as literal "NameAsLiteral" {@{String = $NetworkLiteralName; EscapeAsLiteral = $true}} } ## end switch ## make the actual RegEx pattern, joining all values, and escaping if strings are to be literal $strNetworkNameFilter = _New-RegExJoinedOrPattern @hshParamForNewRegExPattern $arrNetworkViews = Get-View -ViewType Network -Property Name -Filter @{Name = $strNetworkNameFilter} if (($arrNetworkViews | Measure-Object).Count -eq 0) {Write-Warning "No networks found matching name '$($hshParamForNewRegExPattern['String'])'"} else { ## get the networks' VMs' info $arrNetworkViews | Foreach-Object {$_.UpdateViewData("Vm.Name","Vm.Runtime.Host.Name","Vm.Runtime.Host.Parent.Name","Vm.Runtime.PowerState")} ## for each item, return a new info object $arrNetworkViews | Foreach-Object { $viewNetwk = $_ $viewNetwk.LinkedView.Vm | Foreach-Object { New-Object -TypeName PSObject -Property ([ordered]@{ VMName = $_.Name Network = $viewNetwk.Name VMHost = $_.Runtime.LinkedView.Host.Name Cluster = $_.Runtime.LinkedView.Host.LinkedView.Parent.Name PowerState = $_.Runtime.PowerState MoRef = $_.MoRef }) ## end new-object } ## end foreach-object } ## end foreach-object } ## end else } ## end process } ## end fn function Get-VNVMEVCInfo { <# .Description Function to get VMs' EVC mode and that of the cluster in which the VMs reside .Example Get-Cluster myCluster | Get-VNVMEVCInfo | ?{$_.VMEVCMode -ne $_.ClusterEVCMode} Name PowerState VMEVCMode ClusterEVCMode ClusterName ---- ---------- --------- -------------- ----------- myvm001 poweredOff intel-nehalem myCluster0 myvm100 poweredOff intel-nehalem myCluster0 Get all VMs in given clusters where the VM's EVC mode does not match the Cluster's EVC mode .Example Get-VM myVM0,myVM1 | Get-VNVMEVCInfo Get the EVC info for the given VMs and the cluster in which they reside .Outputs System.Management.Automation.PSCustomObject #> [CmdletBinding(DefaultParameterSetName="ByCluster")] [OutputType([System.Management.Automation.PSCustomObject])] param( ## Cluster whose VMs about which to get EVC information [parameter(ValueFromPipeline=$true,ParameterSetName="ByCluster",Position=0)][VMware.VimAutomation.Types.Cluster[]]$Cluster, ## VM for which to get EVC info [parameter(ValueFromPipeline=$true,ParameterSetName="ByVM",Position=0)][VMware.VimAutomation.Types.VirtualMachine[]]$VM ) ## end param begin { $hshParamForGetVMView = @{Property = "Name","Runtime.PowerState","Summary.Runtime.MinRequiredEVCModeKey"} ## helper function to create a new info object function _New-InfoObj ([VMware.Vim.VirtualMachine]$VMView, [string]$ClusterEVCModeKey, [string]$ClusterName) { New-Object -Type PSObject -Property ([ordered]@{ Name = $VMView.Name PowerState = $VMView.Runtime.PowerState VMEVCMode = $VMView.Summary.Runtime.MinRequiredEVCModeKey ClusterEVCMode = $ClusterEVCModeKey ClusterName = $ClusterName }) } ## end helper fn } ## end begin process { ## on VirtualMachine View objects #.Summary.Runtime.MinRequiredEVCModeKey ## on ClusterComputeResource, though, not necessarily there, so needed to just get .Summary in the Get-View call #.Summary.CurrentEVCModeKey Switch ($PSCmdlet.ParameterSetName) { "ByCluster" { Get-View -Property Name,Summary -Id $Cluster.Id | Foreach-Object { $viewThisCluster = $_ Get-View -ViewType VirtualMachine @hshParamForGetVMView -SearchRoot $viewThisCluster.MoRef | Foreach-Object { _New-InfoObj -VMView $_ -ClusterEVCModeKey $viewThisCluster.Summary.CurrentEVCModeKey -ClusterName $viewThisCluster.Name } ## end foreach-object } ## end foreach-object break } ## end case "ByVM" { Get-View @hshParamForGetVMView -Id $VM.Id | Foreach-Object { ## update the View data to get the cluster name and the cluster summary (which has the cluster's EVCMode) $_.UpdateViewData("Runtime.Host.Parent.Name") $_.Runtime.LinkedView.Host.LinkedView.Parent.UpdateViewData("Summary") _New-InfoObj -VMView $_ -ClusterEVCModeKey $_.Runtime.LinkedView.Host.LinkedView.Parent.Summary.CurrentEVCModeKey -ClusterName $_.Runtime.LinkedView.Host.LinkedView.Parent.Name } ## end foreach-object } ## end case } ## end switch } ## end process } ## end fn function Get-VNVMHostHBAWWN { <# .Description Get the Port- and Node WWNs for HBA(s) in VMHost(s) .Example Get-VMHost myVMHost.dom.com | Get-VMHostHBAWWN VMHostName DeviceName HBAPortWWN HBANodeWWN HBAStatus ---------- ---------- ---------- ---------- --------- myVMHost.dom.com vmhba2 10:00:00:00:aa:bb:cc:53 20:00:00:00:aa:bb:cc:53 online myVMHost.dom.com vmhba3 10:00:00:00:aa:bb:cc:86 20:00:00:00:aa:bb:cc:86 online Get the HBA WWNs for host myVMHost .Example Get-Cluster mycluster | Get-VMHostHBAWWN Get the HBA WWNs for hosts in the cluster "mycluster" .Example Get-VMHostHBAWWN -VMHostName myvmhost.* Get the HBA WWNs for hosts whose name match the regular expression pattern myvmhost.* .Example Get-VMHostHBAWWN -VMHostName ^as.+ Get the HBA WWNs for hosts whose name match the regular expression pattern ^as.+ .Link http://vNugglets.com .Outputs System.Management.Automation.PSCustomObject #> [CmdletBinding()] [OutputType([System.Management.Automation.PSCustomObject])] Param( ## Name pattern of the host for which to get HBA info (treated as a RegEx pattern) [parameter(Mandatory=$true,ParameterSetName="SearchByHostName")][string]$VMHostName, ## The ID (MoRef) the host for which to get HBA info [parameter(Mandatory=$true,ParameterSetName="SearchByHostId",ValueFromPipelineByPropertyName=$true)][Alias("Id")][string]$VMHostId, ## The cluster for whose hosts to get HBA info [parameter(Mandatory=$true,ParameterSetName="SearchByCluster",ValueFromPipeline=$true)][VMware.VimAutomation.ViCore.Types.V1.Inventory.Cluster]$Cluster ) ## end param Process { ## params for the Get-View expression for getting the View objects $hshGetViewParams = @{ Property = "Name", "Config.StorageDevice.HostBusAdapter" } ## end hashtable Switch ($PsCmdlet.ParameterSetName) { "SearchByHostId" {$hshGetViewParams["Id"] = $VMHostId; break} {"SearchByHostName","SearchByCluster" -contains $_} {$hshGetViewParams["ViewType"] = "HostSystem"} ## if host name pattern was provided, filter on it in the Get-View expression "SearchByHostName" {$hshGetViewParams["Filter"] = @{"Name" = $VMHostName}; break} ## end case ## if cluster name pattern was provided, set it as the search root for the Get-View expression "SearchByCluster" {$hshGetViewParams["SearchRoot"] = $Cluster.Id; break} } ## end switch Get-View @hshGetViewParams | Foreach-Object { $viewHost = $_ $viewHost.Config.StorageDevice.HostBusAdapter | Where-Object {$_ -is [VMware.Vim.HostFibreChannelHba]} | Foreach-Object { New-Object -TypeName PSObject -Property ([ordered]@{ VMHostName = $viewHost.Name DeviceName = $_.Device HBAPortWWN = _Format-AsHexWWNString -WWN $_.PortWorldWideName HBANodeWWN = _Format-AsHexWWNString -WWN $_.NodeWorldWideName HBAStatus = $_.Status }) ## end new-object } ## end foreach-object } ## end foreach-object } ## end process } ## end fn function Move-VNTemplateFromVMHost { <# .Description Function to move (by act of marking as VM, but on a different VMHost, and then marking back as template) templates from one VMhost to the rest of the hosts in the cluster (at random, for good dispersion). Does not move any disk/config files -- leverages API calls to essentially register template as VM on a different host, then mark as template again. .Example Move-VNTemplateFromVMHost -VMHost (Get-VMHost myhost.dom.com) -DestinationCluster (Get-Cluster someOtherCluster) Moves templates from myhost.dom.com to random available hosts in someOtherCluster .Example Get-VMHost myhost.dom.com | Move-VNTemplateFromVMHost -Verbose Moves templates from myhost.dom.com to random available hosts in same cluster as myhost.dom.com .Link http://vNugglets.com .Outputs VMware.VimAutomation.Types.Template of each moved template #> [CmdletBinding(SupportsShouldProcess=$true)] [OutputType([VMware.VimAutomation.Types.Template])] param( ## VMHost from which to evacuate templates [parameter(ValueFromPipeline=$true, Mandatory=$true)][VMware.VimAutomation.Types.VMHost]$VMHost, ## VMHost cluster to which to move templates; if not specified, templates will be moved to other available VMHosts in the same cluster as the source VMHost [VMware.VimAutomation.Types.Cluster]$DestinationCluster ) ## end param begin { ## name of resource pool in the cluster into which to migrate the template (name of default ResourcePool in a cluster is "Resources") $strDestResPoolName = "Resources" } ## end begin process { ## full name of VMHost from which to move templates $strSourceVMHostName = $VMHost.Name ## .NET View object of VMHost on which templates reside # $viewSourceVMHost = Get-View -ViewType HostSystem -Property Parent -Filter @{"Name" = [RegEx]::escape($strSourceVMHostName)} $viewSourceVMHost = Get-View -Id $VMHost.Id -Property Parent ## if some number of hosts other than 1 was found if (($viewSourceVMHost | Measure-Object).Count -ne 1) {Throw "Either zero or more than one VMHost View found; connected to only the vCenter in which VMHost resides? (error when getting View of VMHost)"} ## .NET View object of cluster in which the VMHost (and, so, templates) reside $viewTemplsCluster = Get-View -Id $viewSourceVMHost.Parent -Property Name ## the .NET View object of destination cluster to which to move templates (may be the same as source) $viewDestCluster = if ($PSBoundParameters.ContainsKey("DestinationCluster")) {Get-View -Property Name -Id $DestinationCluster.Id} else {$viewTemplsCluster} ## get the view object of the destination resource pool $viewDestResPool = Get-View -ViewType ResourcePool -Property Name -SearchRoot $viewDestCluster.MoRef -Filter @{"Name" = "^$strDestResPoolName$"} ## array of .NET View objects of templates to move $arrTemplViewsToMove = Get-View -ViewType VirtualMachine -Property Name -SearchRoot $viewSourceVMHost.MoRef -Filter @{"Config.Template" = "true"} ## array of .NET View objects of VMHosts to which to move templates $arrDestHostViews = Get-View -ViewType HostSystem -Property Name,Runtime.ConnectionState -SearchRoot $viewDestCluster.MoRef -Filter @{"Runtime.InMaintenanceMode" = "False"} | Where-Object {($_.Name -ne $strSourceVMHostName) -and ($_.RunTime.ConnectionState -eq "connected")} ## move the templates to other hosts $arrTemplViewsToMove | Foreach-Object { $viewThisTemplate = $_ $strThisTemplateName = $viewThisTemplate.Name $viewDestHostSystem = $arrDestHostViews | Get-Random if ($PsCmdlet.ShouldProcess($strThisTemplateName, "Move template to HostSystem '$($viewDestHostSystem.Name)'")) { try { Write-Verbose "Working on template '$strThisTemplateName' (Id '$($viewThisTemplate.MoRef.ToString())')" ## MigrateVM_Task() not supported on templates (does not work), so go this route: ## mark template as a VM, putting the template on a different host in the process (takes advantage of the Host param to change the Host on which the template resides in the process of marking it as a VM) # http://pubs.vmware.com/vsphere-50/topic/com.vmware.wssdk.apiref.doc_50/vim.VirtualMachine.html?path=5_0_2_5_12_4#markAsVirtualMachine $_.MarkAsVirtualMachine($viewDestResPool.MoRef, $viewDestHostSystem.MoRef) ## mark VM as template again # http://pubs.vmware.com/vsphere-50/topic/com.vmware.wssdk.apiref.doc_50/vim.VirtualMachine.html?path=5_0_2_5_12_3#markAsTemplate $_.MarkAsTemplate() Get-Template -Id $_.MoRef Write-Verbose "Moved template '$strThisTemplateName' (Id '$($viewThisTemplate.MoRef.ToString())') to VMHost '$($viewDestHostSystem.Name)'" } ## end try catch {Throw $_} } ## end if } ## end foreach-object } ## end template } ## end fn # ## real 1337 way to move them back to their original host # <# # $arrTemplViewsToMove | %{ # ## MigrateVM_Task() not supported on templates (does not work), so go this route: # ## mark template as a VM, putting the template on a different host in the process (takes advantage of the Host param to change the Host on which the template resides in the process of marking it as a VM) # $_.MarkAsVirtualMachine($viewDestResPool.MoRef, $viewSourceVMHost.MoRef) # ## mark VM as template again # $_.MarkAsTemplate() # } ## end foreach-object # #> function Get-VNVMHostNICFirmwareAndDriverInfo { <# .Description Function to get NIC driver and firmware information for VMHosts .Example Get-VMHost myhost0.dom.com | Get-VNVMHostNICFirmwareAndDriverInfo VMHostName NicDriverVersion NicFirmwareVersion ---------- ---------------- ------------------ myhost0.dom.com nx_nic driver 5.0.619 nx_nic device firmware 4.0.588 Grab NIC driver- and firmware version(s) for NICs on given host .Example Get-Cluster myCluster0 | Get-VMHost | Get-VNVMHostNICFirmwareAndDriverInfo | sort VMHostName VMHostName NicDriverVersion NicFirmwareVersion ---------- ---------------- ------------------ myhost0.dom.com nx_nic driver 5.0.619 nx_nic device firmware 4.0.588 myhost1.dom.com nx_nic driver 5.0.619 nx_nic device firmware 4.0.588 myhost2.dom.com nx_nic driver 5.0.619 nx_nic device firmware 4.0.588 ... Grab NIC driver- and firmware version(s) for NICs on hosts in given cluster .Link Get-VNVMHostFirmwareInfo http://vNugglets.com .Outputs System.Management.Automation.PSCustomObject #> [CmdletBinding(DefaultParametersetName="ByVMHostName")] param( ## Name pattern(s) of VMHost(s) for which to get information [parameter(ParameterSetName="ByVMHostName",Mandatory=$true,Position=0)][Alias("Name")][string[]]$VMHostName, ## VMHost ID(s) for which to get information. Most useful when passing VMHost via pipeline [parameter(ParameterSetName="ByVMHostId",Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)][Alias("Id", "MoRef")][string[]]$VMHostId ) ## end param begin { ## the properties of the HostSystem(s) to retrieve $arrHostSystemPropertiesToGet = Write-Output Name, Runtime.HealthSystemRuntime.SystemHealthInfo.NumericSensorInfo ## the hashtable to use as the filter for the Get-View call $hshHostSystemFilter = @{"Runtime.ConnectionState" = "connected|maintenance"; "Runtime.PowerState" = "poweredOn"} ## Regular Expression against which to match the names of the numeric sensors to get the driver- and firmware sensors $strNumericSensorInfoNamePattern = " driver | device firmware " } ## end begin process { ## get the collection of host(s) for which to get NIC driver/firmware info, based on param passed $arrHostViews = Switch ($PsCmdlet.ParameterSetName) { "ByVMHostName" { $hshHostSystemFilter["Name"] = $VMHostName -join "|" Get-View -ViewType HostSystem -Property $arrHostSystemPropertiesToGet -Filter $hshHostSystemFilter break } ## end case "ByVMHostId" { Get-View -Id $VMHostId -Property $arrHostSystemPropertiesToGet } ## end case } ## end switch ## return the NIC driver/firmware info $arrHostViews | Foreach-Object { ## get the NumericSensorInfo items that match the given pattern (not the strongest / most robust / most reliable way, maybe; revisit how to do better?) $arrNicInfoItems = $_.Runtime.HealthSystemRuntime.SystemHealthInfo.NumericSensorInfo | Where-Object {$_.Name -match $strNumericSensorInfoNamePattern} | Select-Object -Unique Name New-Object -Type PSObject -Property ([ordered]@{ VMHostName = $_.Name NicDriverVersion = $arrNicInfoItems | Where-Object {$_.Name -like "*driver*"} | Foreach-Object {$_.Name} | Sort-Object NicFirmwareVersion = $arrNicInfoItems | Where-Object {$_.Name -like "*device firmware*"} | Foreach-Object {$_.Name} | Sort-Object }) ## end new-object } ## end foreach-object } ## end process } ## end fn function Get-VNVMHostFirmwareInfo { <# .Description Function to get System BIOS date, HP Smart Array firmware version, and HP iLO firmware version for HP VMHosts .Example Get-VNVMHostFirmwareInfo Get all VMHosts' firmware info .Example Get-Cluster MyCluster | Get-VMHost | Get-VNVMHostFirmwareInfo Get firmware info for VMHosts in the cluster "MyCluster" .Example Get-VNVMHostFirmwareInfo | sort HPSmartArray,VMHost | ft -a VMHostName,SystemBIOS,HPSmartArray Get all hosts' firmware info, and return table of just the given properties, sorted on HPSmartArray version then VMHost name .Link Get-VNVMHostNICFirmwareAndDriverInfo http://vNugglets.com .Outputs System.Management.Automation.PSCustomObject #> [CmdletBinding(DefaultParametersetName="ByVMHostName")] [OutputType([System.Management.Automation.PSCustomObject])] param( ## Name pattern(s) of VMHost(s) for which to get information [parameter(ParameterSetName="ByVMHostName",Position=0)][Alias("Name")][string[]]$VMHostName = ".+", ## VMHost ID(s) for which to get information. Most useful when passing VMHost via pipeline [parameter(ParameterSetName="ByVMHostId",Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)][Alias("Id", "MoRef")][string[]]$VMHostId ) ## end param begin { ## the properties of the HostSystem(s) to retrieve $arrHostSystemPropertiesToGet = Write-Output Name, Runtime.HealthSystemRuntime.SystemHealthInfo.NumericSensorInfo, Hardware.SystemInfo.Model } ## end begin process { ## get the collection of host(s) for which to get NIC driver/firmware info, based on param passed $arrHostViews = Switch ($PsCmdlet.ParameterSetName) { "ByVMHostName" { $hshHostSystemFilter = @{Name = $VMHostName -join "|"} Get-View -ViewType HostSystem -Property $arrHostSystemPropertiesToGet -Filter $hshHostSystemFilter break } ## end case "ByVMHostId" { Get-View -Id $VMHostId -Property $arrHostSystemPropertiesToGet } ## end case } ## end switch $arrHostViews | Foreach-Object { $viewHostSystem = $_ $arrNumericSensorInfo = @($viewHostSystem.Runtime.HealthSystemRuntime.SystemHealthInfo.NumericSensorInfo) ## HostNumericSensorInfo for BIOS, iLO, array controller $nsiBIOS = $arrNumericSensorInfo | Where-Object {$_.Name -like "*System BIOS*"} $nsiArrayCtrlr = $arrNumericSensorInfo | Where-Object {$_.Name -like "HP Smart Array Controller*"} $nsiILO = $arrNumericSensorInfo | Where-Object {$_.Name -like "Hewlett-Packard BMC Firmware*"} New-Object PSObject -Property ([ordered]@{ VMHostName = $viewHostSystem.Name SystemBIOS = $nsiBIOS.name HPSmartArray = $nsiArrayCtrlr.Name iLOFirmware = $nsiILO.Name Model = $viewHostSystem.Hardware.SystemInfo.Model }) ## end new-object } ## end Foreach-Object } ## end process } ## end fn function Update-VNTitleBarForPowerCLI { <# .Description Function to update the PowerShell window's title bar with the VIServers to which the current session is connected. Called automatically by functions Connect-VNVIServer and Disconnect-VNVIServer. .Example Update-VNTitleBarForPowerCLI Updates PowerShell window's title bar .Link Connect-VNVIServer Disconnect-VNVIServer http://vNugglets.com .Outputs Null #> process { $host.ui.RawUI.WindowTitle = "[PowerCLI] {0}" -f $( if ($global:DefaultVIServers.Count -gt 0) { if ($global:DefaultVIServers.Count -eq 1) {"Connected to {0} as {1}" -f $global:DefaultVIServers[0].Name, $global:DefaultVIServers[0].User} else {"Connected to {0} servers: {1}." -f $global:DefaultVIServers.Count, (($global:DefaultVIServers | Foreach-Object {$_.Name}) -Join ", ")} } ## end if else {"Not Connected"} ) ## end -f call } ## end process } ## end fn function Connect-VNVIServer { <# .Description Function to use for connecting to VIServers instead of the default "Connect-VIServer" cmdlet -- includes call to function to update PowerShell window's title bar with the VIServer(s) to which the current session has connections .Example Connect-VNVIServer -Credential $myCred -Server myVC0.dom.com, myVC1.dom.com Connects to the given vCenters using the given credentials, and updates the PowerShell window's title bar accordingly .Link Disconnect-VNVIServer Update-VNTitleBarForPowerCLI http://vNugglets.com .Outputs VMware.VimAutomation.Types.VIServer #> param( ## Name of VI server to which to connect [parameter(Mandatory=$true, Position=0)][string[]]$Server, ## Credential to use for connection [parameter(Position=1)][ValidateNotNullOrEmpty()][System.Management.Automation.PSCredential]$Credential ) ## end param process { ## check that given target VIServers are responsive to ping requests (assumes that ICMP echo traffic is allowed from the target machine) $arrVIServersToWhichToConnect = $Server | Foreach-Object { $strVIServerToWhichToConnect = $_ if (-not (Test-Connection -Quiet -Count 2 $strVIServerToWhichToConnect)) {Write-Warning "server at '$strVIServerToWhichToConnect' not reachable; not trying to connect"} else {$strVIServerToWhichToConnect} } ## end foreach $hshConnectVIServerParams = @{Server = $arrVIServersToWhichToConnect} if ($PSBoundParameters.ContainsKey("Credential")) {$hshConnectVIServerParams["Credential"] = $Credential} ## connect to the given server(s) Connect-VIServer @hshConnectVIServerParams ## update the PowerShell WindowTitle Update-VNTitleBarForPowerCLI } ## end process } ## end fn function Disconnect-VNVIServer { <# .Description Function to use for disconnecting from VIServers, to be used instead of the "Disconnect-VIServer" cmdlet, as it includes a call to a function that updates the PowerShell window's title bar with the VIServer(s) to which the current session still has connections (if any) .Example Disconnect-VNVIServer Disconnects from all VIServers to which current PowerCLI session had connections .Link Connect-VNVIServer Update-VNTitleBarForPowerCLI http://vNugglets.com .Outputs Null #> param ( ## Name(s) of the VIServers from which to disconnect. Accepts wildcards. Disconnects from all VIServer if none specified here. [string[]]$Server = "*" ) ## end param process { Disconnect-VIServer -Server $Server -Confirm:$false ## update the PowerShell WindowTitle Update-VNTitleBarForPowerCLI } ## end process } ## end fn function Get-VNVMHostLogicalVolumeInfo { <# .Description Get logical volume information for VMHost from StorageStatusInfo of their managed objects. Depends on CIM provider being installed and in good health, presumably. .Example Get-Cluster myCluster0 | Get-VMHost | Get-VNVMHostLogicalVolumeInfo VMHostName LogicalVolume ---------- ------------- myhost20.dom.com Logical Volume 1 on HPSA1 : RAID 1 : 136GB : Disk 1,2 myhost21.dom.com Logical Volume 1 on HPSA1 : RAID 1 : 136GB : Disk 1,2 myhost22.dom.com Logical Volume 1 on HPSA1 : RAID 1 : 279GB : Disk 1,2,3 .Example Get-VNVMHostLogicalVolumeInfo -VMHostName myhost0,myhost1,myhost22 VMHostName LogicalVolume ---------- ------------- myhost0.dom.com Logical Volume 1 on HPSA1 : RAID 5 : 273GB : Disk 1,3,4 myhost1.dom.com Logical Volume 1 on HPSA1 : RAID 1 : 136GB : Disk 1,2 myhost22.dom.com Logical Volume 1 on HPSA1 : RAID 1 : 279GB : Disk 1,2,3 .Link http://vNugglets.com .Outputs System.Management.Automation.PSCustomObject #> [CmdletBinding(DefaultParameterSetName="ByVMHostName")] [OutputType([System.Management.Automation.PSCustomObject])] param ( ## Name pattern(s) of VMHost(s) for which to get information [parameter(ParameterSetName="ByVMHostName",Position=0)][Alias("Name")][string[]]$VMHostName = ".+", ## VMHost ID(s) for which to get information. Most useful when passing VMHost via pipeline [parameter(ParameterSetName="ByVMHostId",Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)][Alias("Id", "MoRef")][string[]]$VMHostId ) ## end param begin { ## the properties of the HostSystem(s) to retrieve $arrHostSystemPropertiesToGet = Write-Output Name, Runtime.HealthSystemRuntime.HardwareStatusInfo.StorageStatusInfo $hshParamForGetView = @{Property = $arrHostSystemPropertiesToGet} } ## end begin process { Switch ($PsCmdlet.ParameterSetName) { "ByVMHostName" { $hshParamForGetView["ViewType"] = "HostSystem" ## if there is a host name filter, add it if ($PSBoundParameters.ContainsKey("VMHostName")) {$hshParamForGetView["Filter"] = @{Name = $(_New-RegExJoinedOrPattern -String $VMHostName)}} break } ## end case "ByVMHostId" { $hshParamForGetView["Id"] = $VMHostId } ## end case } ## end switch ## get the HostSystem(s) of interest and foreach, create a new info object with info about the logical volumes on the VMHost(s) Get-View @hshParamForGetView | Foreach-Object { New-Object -Type PSObject -Property ([ordered]@{ VMHostName = $_.Name LogicalVolume = $_.Runtime.HealthSystemRuntime.HardwareStatusInfo.StorageStatusInfo | Where-Object {$_.Name -like "Logical*"} | Foreach-Object {$_.Name} }) ## end new-object } ## end foreach-object } ## end process } ## end fn function Copy-VNVIRole { <# .Description Copy a VIRole to another VIRole, either in same vCenter or to a different vCenter. This assumes that connections to source/destination vCenter(s) are already established. If role of given name already exists in destination vCenter, this attempt will stop. .Example Get-VIRole -Server vcenter.dom.com -Name SysAdm | Copy-VNVIRole -DestinationVCName othervcenter.dom.com Copy the VIRole "SysAdm" from the given source vCenter (taken from pipeline) to a new VIRole of the same name as the source VIRole and in the given destination vCenter .Example Get-VIRole -Server vcenter.dom.com -Name TestRole0 | Copy-VNVIRole -DestinationRoleName TestRole0_copy Make a copy the VIRole with the new role name in the source vCenter .Example Copy-VNVIRole -SourceRoleName SysAdm -DestinationRoleName SysAdm_copyTest -SourceVCName vcenter.dom.com -DestinationVCName othervcenter.dom.com Copy the VIRole "SysAdm" from the given source vCenter to a new VIRole named "SysAdm_copyTest" in the given destination vCenter .Link http://vNugglets.com .Outputs VMware.VimAutomation.Types.PermissionManagement.Role if role is created/updated, String in Warning stream and/or Error object, and nothing in standard out otherwise #> [CmdletBinding(SupportsShouldProcess=$true)] [OutputType([VMware.VimAutomation.Types.PermissionManagement.Role])] param( ## Source VIRole object to copy [parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="SourceVIRoleByObj")][VMware.VimAutomation.Types.PermissionManagement.Role]$SourceRole, ## Name of the source VIRole [parameter(Mandatory=$true,Position=0, ParameterSetName="SourceVIRoleByName")][string]$SourceRoleName, ## Name to use for new destination VIRole. If none, will use name from source role [parameter(Position=1)][string]$DestinationRoleName, ## Source vCenter connection name [parameter(Mandatory=$true, ParameterSetName="SourceVIRoleByName")][string]$SourceVCName, ## Destination vCenter connection name, if different from source vCenter (to copy VIRole to same vCenter, either do not use this parameter, or use same vCenter name for destination as used for source) [string]$DestinationVCName ) ## end param process { $oSrcVIRole = Switch ($PSCmdlet.ParameterSetName) { "SourceVIRoleByObj" {$SourceRole; break} "SourceVIRoleByName" { ## get the VIRole from the source vCenter try {Get-VIRole -Server $SourceVCName -Name $SourceRoleName -ErrorAction:Stop} ## if there was issue getting the role from the source vCenter catch { Write-Warning "Issue getting '$SourceRoleName' from source vCenter '$SourceVCName'. Is connection to vCenter ok, and does that VIRole exist there?" $PsCmdlet.ThrowTerminatingError($_) } ## end catch } ## end case } ## end switch $strDestVIRoleName = if ($PSBoundParameters.ContainsKey("DestinationRoleName")) {$DestinationRoleName} else {$oSrcVIRole.Name} $strDestVCName = if ($PSBoundParameters.ContainsKey("DestinationVCName")) {$DestinationVCName} else {$oSrcVIRole.Server} ## see if there is VIRole by the given name in the destination vCenter $oDestVIRole = Get-VIRole -Server $strDestVCName -Name $strDestVIRoleName -ErrorAction:SilentlyContinue ## if the role already exists in the destination vCenter if ($null -ne $oDestVIRole) {Throw "VIRole '$strDestVIRoleName' already exists in destination vCenter '$strDestVCName'"} ## else, create the role else { New-VIRole -Server $strDestVCName -Name $strDestVIRoleName -Privilege (Get-VIPrivilege -Server $strDestVCName -Id $oSrcVIRole.PrivilegeList) } ## end else } ## end process } ## end fn function Get-VNUplinkNicForVM { <# .Description Script to retrieve Netports' (virtual portgroup ports) client, uplink information, vSwitch, and more information. This is useful for knowing which actual VMHost physcial uplink a VM is currently using. Also includes things like VMKernel ports and Mangement uplinks. .Example Get-VNUplinkNicForVM -VMHost myhost0.dom.com -Credential (Get-Credential root) ClientName TeamUplink vSwitch VMHost ---------- ---------- ------- --------------- Management n/a vSwitch0 myhost0.dom.com vmk1 vmnic0 vSwitch0 myhost0.dom.com vmk0 vmnic0 vSwitch0 myhost0.dom.com myvm001 vmnic0 vSwitch0 myhost0.dom.com myvm002 vmnic3 vSwitch1 myhost0.dom.com myvm003 vmnic5 vSwitch2 myhost0.dom.com myvm050.eth0 vmnic4 DvsPortset-0 myhost0.dom.com ... Get the Netports on given VMHost, and return their client name, uplink vmnic, vSwitch, etc. Currently the vSwitch name for virtual distributed switches is just a generic "DvsPortset-0" type of name. The ToDo for this function includes making this property have more robust values for VDSwitches .Link http://vNugglets.com .Outputs System.Management.Automation.PSCustomObject #> [CmdletBinding()] [OutputType([System.Management.Automation.PSCustomObject])] param( ## The DNS name of the VMHost whose VMs' uplink information to get (not VMHost object name, but the name to use for connecting to said VIServer for use of Get-EsxTop cmdlet -- so, do not use wildcards) [parameter(Mandatory=$true)][string]$VMHostToCheck, ## PSCredential to use for connecting to VMHost; will prompt for credentials if not passed in here [System.Management.Automation.PSCredential]$Credential = $host.ui.PromptForCredential("Need credentials to connect to VMHost", "Please enter credentials for '$VMHostToCheck'", $null, $null) ) ## end param process { $strThisVMHostName = $VMHostToCheck ## check if VMHost name given is responsive on the network; if not, exit if (-not (Test-Connection -Quiet -Count 3 -ComputerName $strThisVMHostName)) {Write-Warning "VMHost '$strThisVMHostName' not responding on network -- not proceeding"} else { ## connect to the given VIServer (VMHost, here); use -Force (new in PowerCLI v5.1) to "Suppress all user interface prompts during the cmdlet execution. Currently these include 'Multiple default servers' and 'Invalid certificate action'" $oVIServer = Connect-VIServer $strThisVMHostName -Credential $Credential ## if connecting to VMHost failed, write warning and exit if (-not $oVIServer) {Write-Warning "Did not connect to VMHost '$strThisVMHostName' -- not proceeding"} else { ## array with PortID to vSwitch info, for determining vSwitch name from PortID ## get vSwitch ("PortsetName") and PortID info, grouped by vSwitch #$arrNetPortsetEntries = (Get-EsxTop -TopologyInfo NetPortset).Entries ## or, get vSwitch ("PortsetName") and PortID info, not grouped $arrNetPortEntries = (Get-EsxTop -Server $strThisVMHostName -TopologyInfo NetPort).Entries ## calculated property for vSwitch name $hshVSwitchInfo = @{n="vSwitch"; e={$oNetportCounterValue = $_; ($arrNetPortEntries | Where-Object {$_.PortId -eq $oNetportCounterValue.PortId}).PortsetName}} ## get the VM, uplink NIC, vSwitch, and VMHost info Get-EsxTop -Server $strThisVMHostName -CounterName NetPort | Select-Object ClientName, TeamUplink, $hshVSwitchInfo, @{n="VMHost"; e={$_.Server}} Disconnect-VIServer $strThisVMHostName -Confirm:$false } ## end else } ## end else } ## end process } ## end fn function Get-VNVMDiskAndRDM { <# .Description Function to get a VM's hard disk and RDM information. .Example Get-VM someVM | Get-VNVMDiskAndRDM VMName : someVM HardDiskName : Hard disk 1 ScsiId : 0:0 DeviceDisplayName : SizeGB : 50 ScsiCanonicalName : VMName : someVM HardDiskName : Hard disk 2 ScsiId : 1:0 DeviceDisplayName : someVM-/log_dir SizeGB : 20 ScsiCanonicalName : naa.60000111111115615641111111111111 Get the disks (including RDMs) for "someVM". Note, the ScsiCanonicalName property is only valid (and only populated for) RDM disks. .Example Get-VNVMDiskAndRDM -VMName someVM | ft -a VMName HardDiskName ScsiId DeviceDisplayName SizeGB ScsiCanonicalName ------ ------------ ------ ----------------- ------ ----------------- someVM0 Hard disk 1 0:0 50 someVM0 Hard disk 2 1:0 someVM0-/log_dir 20 naa.60000111111115615641111111111111 someVM1 Hard disk 1 0:0 180 someVM1 Hard disk 2 1:0 someVM1-/log_dir 120 naa.60000111111115615641111111111112 Get the disks (including RDMs) for VMs matching the name regular expression pattern "someVM", formatting output in auto-sized table. Note, the ScsiCanonicalName property is only valid (and only populated for) RDM disks. .Link http://vNugglets.com .Outputs System.Management.Automation.PSCustomObject #> [CmdletBinding(DefaultParameterSetName="NameAsRegEx")] [OutputType([System.Management.Automation.PSCustomObject])] param( ## Name pattern of VM for which to get information. This is a regular expression [parameter(Mandatory=$true,ParameterSetName="NameAsRegEx",Position=0)][String[]]$VMName, ## Literal name of VM for which to get information. This RegEx-escapes the string and adds start/end anchors ("^" and "$") so that the only match is an exact match [parameter(Mandatory=$true,ParameterSetName="NameAsLiteral",Position=0)][String[]]$VMLiteralName, ## MoRef/Id of VM for which to get disk information -- most useful when passing VM or VirtualMachine object via pipeline [parameter(ParameterSetName="ById",ValueFromPipelineByPropertyName=$true,Position=0)][Alias("MoRef")][string[]]$Id, ## Switch: Also return the VMDK's datastore path? [switch]$ShowVMDKDatastorePath ) ## end param begin { $hshParamForGetViewForVMachine = @{Property = Write-Output Name Config.Hardware.Device Runtime.Host} } ## end begin process { ## get the VM object(s) (the cool, FaF way using .NET View objects) Switch ($PSCmdlet.ParameterSetName) { {"NameAsRegEx","NameAsLiteral" -contains $_} { $strThisParamSetName = $_ $hshParamForGetViewForVMachine["ViewType"] = "VirtualMachine" ## make a temporary hashtable to be used for creating the Get-View filter string value, and the message for the warning (if any warning) $hshParamForNewRegExPattern = Switch ($strThisParamSetName) { "NameAsRegEx" { @{String = $VMName; EscapeAsLiteral = $false} break } ## end case ## if literal, do escape as literal "NameAsLiteral" { @{String = $VMLiteralName; EscapeAsLiteral = $true} } ## end case } ## end inner switch ## make the actual RegEx pattern for the Get-View filter, joining all values, and escaping if strings are to be literal $strVirtualMachineNameFilter = _New-RegExJoinedOrPattern @hshParamForNewRegExPattern ## message for warning, if any warning $strMessageForWarning = "name pattern '$strVirtualMachineNameFilter'" $hshParamForGetViewForVMachine["Filter"] = @{"Name" = $strVirtualMachineNameFilter} break } ## end case "ById" { $hshParamForGetViewForVMachine["Id"] = $Id $strMessageForWarning = "Id '$Id'" } ## end case } ## end outer switch $arrVMViewsForStorageInfo = Get-View @hshParamForGetViewForVMachine if (($arrVMViewsForStorageInfo | Measure-Object).Count -eq 0) {Throw "No VirtualMachine objects found matching $strMessageForWarning"} ## end if $arrVMViewsForStorageInfo | Foreach-Object { $viewVMForStorageInfo = $_ ## get the view of the HostSystem on which the VM currently resides $viewHostWithStorage = Get-View -Id $viewVMForStorageInfo.Runtime.Host -Property Config.StorageDevice.ScsiLun $viewVMForStorageInfo.Config.Hardware.Device | Where-Object {$_ -is [VMware.Vim.VirtualDisk]} | Foreach-Object { $hdThisDisk = $_ $oScsiLun = $viewHostWithStorage.Config.StorageDevice.ScsiLun | Where-Object {$_.UUID -eq $hdThisDisk.Backing.LunUuid} ## the properties to return in new object $hshThisVMProperties = ([ordered]@{ VMName = $viewVMForStorageInfo.Name ## the disk's "name", like "Hard disk 1" HardDiskName = $hdThisDisk.DeviceInfo.Label ## get device's SCSI controller and Unit numbers (1:0, 1:3, etc) ScsiId = &{$strControllerKey = $_.ControllerKey.ToString(); "{0}`:{1}" -f $strControllerKey[$strControllerKey.Length - 1], $_.Unitnumber} DeviceDisplayName = $oScsiLun.DisplayName SizeGB = [Math]::Round($_.CapacityInKB / 1MB, 0) ScsiCanonicalName = $oScsiLun.CanonicalName }) ## end hsh ## add property for VMDKDStorePath if desired if ($ShowVMDKDatastorePath) {$hshThisVMProperties["VMDKDStorePath"] = $hdThisDisk.Backing.Filename} New-Object -Type PSObject -Property $hshThisVMProperties } ## end foreach-object } ## end foreach-object } ## end process } ## end fn function Invoke-VNEvacuateDatastore { <# .Description Script to evacuate virtual disks and/or VM config files from a given datastore; does not move the entire VM and all its disks if they reside elsewhere .Example Invoke-VNEvacuateDatastore -SourceDatastore datastoreToEvac -Destination destinationDatastore -RunAsync -OutVariable arrMyMoveTasks Move virtual disks and/or VM config files (if any) from source datastore to the destination datastore, running asynchronously. This also saves the resulting tasks' MoRefs into the output variable $arrMyMoveTasks. One can then check status on these particular tasks like: Get-Task -Id $arrMyMoveTasks .Example Invoke-VNEvacuateDatastore -SourceDatastore datastoreToEvac -Destination (Get-DatastoreCluster my_datastoreCluster) -Verbose Synchronously and serially moves VM disks and files from source datastore to datastores in given datastore cluster, with a bit of Verbose output .Link http://vNugglets.com .Outputs If running asynchronously, returns the VMware.Vim.ManagedObjectReference for each RelocateVM task. Else, returns nothing #> [CmdletBinding(SupportsShouldProcess=$true)] param( ## The name of the source datastore (the one to evacuate) [parameter(Mandatory=$true)][string]$SourceDatastore, ## The name of the destination datastore or datastore cluster, or a datastore cluster itself [parameter(Mandatory=$true)]$Destination, ## Name(s) of VM/template to exclude from evacuation activities (exact name) [string[]]$ExcludeVMName, ## Switch: exclude all templates on the source datastore from this evacuation effort? [Switch]$ExcludeAllTemplate, ## Switch: Run asynchonously? If not, this cmdlet runs synchronously, waiting for each virtual disk relocation before starting the next. [switch]$RunAsync ) ## end parameter Begin { ## get the datastore View object, either from one of the datastore IDs in the datastorecluster (if passed), or by the datastore that matches the datastore name given $arrDestDatastoreView = if ($Destination -is [VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.DatastoreCluster]) { Get-View -Id ($Destination.ExtensionData.ChildEntity) -Property Name } else {Get-View -ViewType Datastore -Property Name -Filter @{"Name" = "^${Destination}$"}} } ## end begin Process { ## Set proper variable name from the supplied parameter $strSrcDatastore = $SourceDatastore ## Get the .NET view of the source datastore $viewSrcDatastore = Get-View -ViewType Datastore -Property Name -Filter @{"Name" = "^${strSrcDatastore}$"} ## Get the linked view that contains the list of VMs on the source datastore $viewSrcDatastore.UpdateViewData("Vm.Config.Files.VmPathName", "Vm.Config.Hardware.Device", "Vm.Config.Template", "Vm.Runtime.Host", "Vm.Name") ## Create a VirtualMachineMovePriority object for the RelocateVM task; 0 = defaultPriority, 1 = highPriority, 2 = lowPriority (per http://pubs.vmware.com/vsphere-51/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvim.VirtualMachine.MovePriority.html) $specVMMovePriority = New-Object VMware.Vim.VirtualMachineMovePriority -Property @{"value__" = 1} ## For each VM View object, initiate the RelocateVM_Task() method; for each template object, initiate the RelocateVM() method $viewSrcDatastore.LinkedView.Vm | Foreach-Object { $viewVMToMove = $_ ## if this machine was to be excluded, do not move its files if (($ExcludeAllTemplate -and ($viewVMToMove.Config.Template -eq "True")) -or ($ExcludeVMName -contains $viewVMToMove.Name)) {Write-Verbose -Verbose "not moving files for excluded machine '$($viewVMToMove.Name)'"} ## else, doit else { ## Create a VirtualMachineRelocateSpec object for the RelocateVM task $specVMRelocate = New-Object Vmware.Vim.VirtualMachineRelocateSpec ## Create an array containing all the virtual disks for the current VM/template $arrVirtualDisks = $viewVMToMove.Config.Hardware.Device | Where-Object {$_ -is [VMware.Vim.VirtualDisk]} ## If the VM/template's config files reside on the source datastore, set this to the destination datastore (if not specified, the config files are not moved) if ($viewVMToMove.Config.Files.VmPathName.Split("]")[0].Trim("[") -eq $strSrcDatastore) { $specVMRelocate.Datastore = ($arrDestDatastoreView | Get-Random).MoRef } ## end if ## For each VirtualDisk for this VM/template, make a VirtualMachineRelocateSpecDiskLocator object (to move disks that are on the source datastore, and leave other disks on their current datastore) ## But first, make sure the VM/template actually has any disks if ($arrVirtualDisks) { foreach($oVirtualDisk in $arrVirtualDisks) { $oVMReloSpecDiskLocator = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator -Property @{ ## If this virtual disk's filename matches the source datastore name, set the VMReloSpecDiskLocator Datastore property to the destination datastore's MoRef, else, set this property to the virtual disk's current datastore MoRef DataStore = if ($oVirtualDisk.Backing.Filename -match $strSrcDatastore) {($arrDestDatastoreView | Get-Random).MoRef} else {$oVirtualDisk.Backing.Datastore} DiskID = $oVirtualDisk.Key } ## end new-object $specVMRelocate.disk += $oVMReloSpecDiskLocator } ## end foreach } ## end if if ($PSCmdlet.ShouldProcess("VM '$($viewVMToMove.Name)'", "Relocate files from datastore '$($viewSrcDatastore.Name)', with '$($arrDestDatastoreView.Name -join ", ")' as potential destination")) { ## Determine if template or VM, then perform necessary relocation steps if ($viewVMToMove.Config.Template -eq "True") { ## Gather necessary objects to mark template as a VM (VMHost where template currently resides and default, root resource pool of the cluster) $viewTemplateVMHost = Get-View -Id $_.Runtime.Host -Property Parent $viewTemplateResPool = Get-View -ViewType ResourcePool -Property Name -SearchRoot $viewTemplateVMHost.Parent -Filter @{"Name" = "^Resources$"} ## Mark the template as a VM $_.MarkAsVirtualMachine($viewTemplateResPool.MoRef, $viewTemplateVMHost.MoRef) ## Relocate the template synchronously (i.e. one at a time) Write-Verbose -Verbose "moving template '$($viewVMToMove.Name)' synchronously (template -> VM -> move datastores -> template)" $oThisReloTask_moref = $viewVMToMove.RelocateVM_Task($specVMRelocate, $specVMMovePriority) Write-Verbose "migrate task Id: '$oThisReloTask_moref'" Get-Task -Id $oThisReloTask_moref | Wait-Task ## Convert VM back to template $viewVMToMove.MarkAsTemplate() } ## end if else { ## Initiate the RelocateVM task (asynchronously), if RunAsync switch is $true if ($RunAsync) {$viewVMToMove.RelocateVM_Task($specVMRelocate, $specVMMovePriority)} ## else, invoke the RelocateVM method (synchronously) else {$viewVMToMove.RelocateVM($specVMRelocate, $specVMMovePriority)} } ## end else } ## end if } ## end else } ## end foreach-object } ## end process } ## end fn |