Posted in Virtual Machine Manager, Windows Powershell | No Comment | 1,766 views | 31/03/2014 17:04
You can grant a user role to a virtual machine with following command:
1
2
3
| $UserRole = Get-SCUserRole -Name "Test User Role"
$VM | Set-SCVirtualMachine -Owner $UserRole.Members[0].Name -UserRole $UserRole
Grant-SCResource -Resource $VM -UserName $UserRole.Members[0].Name -UserRoleID @($UserRole.ID.Guid) |
$UserRole = Get-SCUserRole -Name "Test User Role"
$VM | Set-SCVirtualMachine -Owner $UserRole.Members[0].Name -UserRole $UserRole
Grant-SCResource -Resource $VM -UserName $UserRole.Members[0].Name -UserRoleID @($UserRole.ID.Guid)
After that you will be able to reach it from System Center App Controller.
Posted in Virtual Machine Manager, Windows Powershell | 2 Comments | 3,772 views | 31/03/2014 16:45
You can use following script to get VM CPU Counts on SCVMM 2012 R2:
1
2
3
4
5
6
7
8
9
| PS C:\Users\yusufozt\Desktop> $Results = Get-VM | Measure-Object -Property CPUCount -Sum
PS C:\Users\yusufozt\Desktop> $Results
Count : 108
Average :
Sum : 292
Maximum :
Minimum :
Property : CPUCount |
PS C:\Users\yusufozt\Desktop> $Results = Get-VM | Measure-Object -Property CPUCount -Sum
PS C:\Users\yusufozt\Desktop> $Results
Count : 108
Average :
Sum : 292
Maximum :
Minimum :
Property : CPUCount
You can get total count by using $Results.Sum value.
Posted in Virtual Machine Manager, Windows Powershell | 2 Comments | 6,050 views | 25/03/2014 16:50
You can get VM list in a VMHostCluster with following command:
1
| ((Get-SCVMHOST -VMHostCluster $Cluster).Name | foreach { Get-VM -VMHost $_ }).Name |
((Get-SCVMHOST -VMHostCluster $Cluster).Name | foreach { Get-VM -VMHost $_ }).Name
That will output all virtual machine names in that cluster.
Posted in Virtual Machine Manager, Windows Powershell, Windows Server | No Comment | 1,738 views | 25/03/2014 16:44
You can use this script to move your VMs into another Cloud (Cloud Migration) and Cluster on SCVMM 2012 R2.
This script only works on SCVMM 2012 R2 due to vHBA control.
You can remove vHBA control to make it work on SCVMM 2012 SP1.
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
| # Parameters
$DestinationCloud = "New Cloud"
$DestinationHostCluster = "cluster.domain.com"
# Create VM Array
$VMArray = New-Object System.Collections.ArrayList
$VMArray.Clear();
# Add VMs into Array
$AddArray = $VMArray.Add("VM01")
$AddArray = $VMArray.Add("VM02")
$AddArray = $VMArray.Add("VM03")
$AddArray = $VMArray.Add("VM04")
# Get Destination Cloud
$Cloud = Get-SCCloud -Name $DestinationCloud
# Cloud Host Group Path
$HostGroupPath = (($Cloud.HostGroup)[0]).Path + "*"
foreach ($VMName in $VMArray)
{
# Get VM
$VM = Get-SCVirtualMachine -Name $VMName
# Output
Write-Host VM Name: $VMName
Write-Host " "
if ($VM.HasPassthroughDisk -eq $False -and $VM.HasVirtualFibreChannelAdapters -eq $False)
{
# Current Cloud
$CurrentCloud = Get-SCCloud -Name $VM.Cloud.Name
# Current Hyper-V Host
$CurrentVMHost = Get-SCVMHost -ComputerName $VM.VMHost.Name
# Create Job Guid
$JobGuid = [System.Guid]::NewGuid().toString()
# Remove from Cloud
$SetCloud = $VM | Set-SCVirtualMachine -RemoveFromCloud
# Get Best Available Hyper-V Host
if ($DestinationHostCluster)
{
$VMHostName = (@(Get-SCVMHost | Where {$_.VMHostGroup -like $HostGroupPath -and $_.HostCluster.Name -eq $DestinationHostCluster -and $_.CoresPerCPU -eq $CurrentVMHost.CoresPerCPU -and $_.CPUArchitecture -eq $CurrentVMHost.CPUArchitecture -and $_.CPUFamily -eq $CurrentVMHost.CPUFamily } | Select Name,AvailableMemory | Sort AvailableMemory -Descending)[0]).Name
}
else
{
$VMHostName = ((Get-SCVMHost | Where {$_.VMHostGroup -like $HostGroupPath -and $_.CoresPerCPU -eq $CurrentVMHost.CoresPerCPU -and $_.CPUArchitecture -eq $CurrentVMHost.CPUArchitecture -and $_.CPUFamily -eq $CurrentVMHost.CPUFamily } | Select Name,AvailableMemory | Sort AvailableMemory -Descending)[0]).Name
}
if ($VMHostName)
{
# Output
Write-Host Target Host: $VMHostName
# Get Best Available CSV
$VolumeName = ((Get-SCStorageVolume | Where {$_.VMHost -eq $VMHostName -and $_.IsClusterSharedVolume -eq $True} | Select Name,FreeSpace | Sort FreeSpace -Descending)[0]).Name
# Output
Write-Host Target Volume: $VolumeName
Write-Host " "
# Get Hyper-V Host Information
$VMHost = Get-SCVMHost -ComputerName $VMHostName
[int64]$VMHostAvailableMemory = [int64]$VMHost.AvailableMemory + 10240 # Leave 10 GB Available Memory
# Get CSV Information
$Volume = Get-SCStorageVolume -Name $VolumeName -VMHost $VMHostName
[int64]$VolumeFreeSpace = [int64]$Volume.FreeSpace + 107374182400 # Leave 100 GB Free Space
# Control Free Memory
if ($VM.Memory -lt $VMHostAvailableMemory -and $VM.TotalSize -lt $VolumeFreeSpace)
{
# Get Virtual Network Adapters
$VirtualNetworkAdapters = $VM | Get-SCVirtualNetworkAdapter
foreach ($VirtualNetworkAdapter in $VirtualNetworkAdapters)
{
# Clear VM Network
$VMNetwork = $Null;
# Get VM Network
$VMNetwork = Get-SCVMNetwork | Where {$_.Name -eq $VirtualNetworkAdapter.VMNetwork.Name}
if (!$VMNetwork)
{
$VMNetwork = Get-SCVMNetwork | Where {$_.VMSubnet.SubnetVLANs.VLanID -eq $VirtualNetworkAdapter.VLanID}
}
# Destination Virtual Network
$VirtualNetwork = ((Get-VM -Cloud $Cloud | Where {$_.VirtualNetworkAdapters.VMNetwork.Name -eq $VirtualNetworkAdapter.VMNetwork.Name -and $_.VirtualNetworkAdapters.VirtualNetwork})[0]).VirtualNetworkAdapters.VirtualNetwork
# Set VM Network Adapter
$SetSCVirtualNetworkAdapter = Set-SCVirtualNetworkAdapter -VirtualNetworkAdapter $VirtualNetworkAdapter -VirtualNetwork $VirtualNetwork -VMNetwork $VMNetwork -JobGroup $JobGuid
}
# Move VM
$MoveSCVirtualMachine = $VM | Move-SCVirtualMachine -VMHost $VMHostName -HighlyAvailable $True -UseLAN -UseDiffDiskOptimization -JobGroup $JobGuid -Path $VolumeName
Write-Host "Migration process is finished."
Write-Host "Please check job results to ensure that if operation is successful.."
Write-Host " "
Write-Host " "
# Set Cloud
$SetCloud = $VM | Set-SCVirtualMachine -Cloud $Cloud
# Refresh VM
$RefreshVM = $VM | Refresh-VM
}
else
{
Write-Host "Not enough resources to move VM.."
Write-Host "Skipping migration.."
Write-Host " "
Write-Host " "
# Set Cloud
$SetCloud = $VM | Set-SCVirtualMachine -Cloud $CurrentCloud
}
}
else
{
Write-Host "There is no available host to migrate VM.."
Write-Host "Skipping migration.."
Write-Host " "
Write-Host " "
}
}
else
{
Write-Host "VM has Pass-through disks or vHBA.."
Write-Host "Skipping migration.."
Write-Host " "
Write-Host " "
}
} |
# Parameters
$DestinationCloud = "New Cloud"
$DestinationHostCluster = "cluster.domain.com"
# Create VM Array
$VMArray = New-Object System.Collections.ArrayList
$VMArray.Clear();
# Add VMs into Array
$AddArray = $VMArray.Add("VM01")
$AddArray = $VMArray.Add("VM02")
$AddArray = $VMArray.Add("VM03")
$AddArray = $VMArray.Add("VM04")
# Get Destination Cloud
$Cloud = Get-SCCloud -Name $DestinationCloud
# Cloud Host Group Path
$HostGroupPath = (($Cloud.HostGroup)[0]).Path + "*"
foreach ($VMName in $VMArray)
{
# Get VM
$VM = Get-SCVirtualMachine -Name $VMName
# Output
Write-Host VM Name: $VMName
Write-Host " "
if ($VM.HasPassthroughDisk -eq $False -and $VM.HasVirtualFibreChannelAdapters -eq $False)
{
# Current Cloud
$CurrentCloud = Get-SCCloud -Name $VM.Cloud.Name
# Current Hyper-V Host
$CurrentVMHost = Get-SCVMHost -ComputerName $VM.VMHost.Name
# Create Job Guid
$JobGuid = [System.Guid]::NewGuid().toString()
# Remove from Cloud
$SetCloud = $VM | Set-SCVirtualMachine -RemoveFromCloud
# Get Best Available Hyper-V Host
if ($DestinationHostCluster)
{
$VMHostName = (@(Get-SCVMHost | Where {$_.VMHostGroup -like $HostGroupPath -and $_.HostCluster.Name -eq $DestinationHostCluster -and $_.CoresPerCPU -eq $CurrentVMHost.CoresPerCPU -and $_.CPUArchitecture -eq $CurrentVMHost.CPUArchitecture -and $_.CPUFamily -eq $CurrentVMHost.CPUFamily } | Select Name,AvailableMemory | Sort AvailableMemory -Descending)[0]).Name
}
else
{
$VMHostName = ((Get-SCVMHost | Where {$_.VMHostGroup -like $HostGroupPath -and $_.CoresPerCPU -eq $CurrentVMHost.CoresPerCPU -and $_.CPUArchitecture -eq $CurrentVMHost.CPUArchitecture -and $_.CPUFamily -eq $CurrentVMHost.CPUFamily } | Select Name,AvailableMemory | Sort AvailableMemory -Descending)[0]).Name
}
if ($VMHostName)
{
# Output
Write-Host Target Host: $VMHostName
# Get Best Available CSV
$VolumeName = ((Get-SCStorageVolume | Where {$_.VMHost -eq $VMHostName -and $_.IsClusterSharedVolume -eq $True} | Select Name,FreeSpace | Sort FreeSpace -Descending)[0]).Name
# Output
Write-Host Target Volume: $VolumeName
Write-Host " "
# Get Hyper-V Host Information
$VMHost = Get-SCVMHost -ComputerName $VMHostName
[int64]$VMHostAvailableMemory = [int64]$VMHost.AvailableMemory + 10240 # Leave 10 GB Available Memory
# Get CSV Information
$Volume = Get-SCStorageVolume -Name $VolumeName -VMHost $VMHostName
[int64]$VolumeFreeSpace = [int64]$Volume.FreeSpace + 107374182400 # Leave 100 GB Free Space
# Control Free Memory
if ($VM.Memory -lt $VMHostAvailableMemory -and $VM.TotalSize -lt $VolumeFreeSpace)
{
# Get Virtual Network Adapters
$VirtualNetworkAdapters = $VM | Get-SCVirtualNetworkAdapter
foreach ($VirtualNetworkAdapter in $VirtualNetworkAdapters)
{
# Clear VM Network
$VMNetwork = $Null;
# Get VM Network
$VMNetwork = Get-SCVMNetwork | Where {$_.Name -eq $VirtualNetworkAdapter.VMNetwork.Name}
if (!$VMNetwork)
{
$VMNetwork = Get-SCVMNetwork | Where {$_.VMSubnet.SubnetVLANs.VLanID -eq $VirtualNetworkAdapter.VLanID}
}
# Destination Virtual Network
$VirtualNetwork = ((Get-VM -Cloud $Cloud | Where {$_.VirtualNetworkAdapters.VMNetwork.Name -eq $VirtualNetworkAdapter.VMNetwork.Name -and $_.VirtualNetworkAdapters.VirtualNetwork})[0]).VirtualNetworkAdapters.VirtualNetwork
# Set VM Network Adapter
$SetSCVirtualNetworkAdapter = Set-SCVirtualNetworkAdapter -VirtualNetworkAdapter $VirtualNetworkAdapter -VirtualNetwork $VirtualNetwork -VMNetwork $VMNetwork -JobGroup $JobGuid
}
# Move VM
$MoveSCVirtualMachine = $VM | Move-SCVirtualMachine -VMHost $VMHostName -HighlyAvailable $True -UseLAN -UseDiffDiskOptimization -JobGroup $JobGuid -Path $VolumeName
Write-Host "Migration process is finished."
Write-Host "Please check job results to ensure that if operation is successful.."
Write-Host " "
Write-Host " "
# Set Cloud
$SetCloud = $VM | Set-SCVirtualMachine -Cloud $Cloud
# Refresh VM
$RefreshVM = $VM | Refresh-VM
}
else
{
Write-Host "Not enough resources to move VM.."
Write-Host "Skipping migration.."
Write-Host " "
Write-Host " "
# Set Cloud
$SetCloud = $VM | Set-SCVirtualMachine -Cloud $CurrentCloud
}
}
else
{
Write-Host "There is no available host to migrate VM.."
Write-Host "Skipping migration.."
Write-Host " "
Write-Host " "
}
}
else
{
Write-Host "VM has Pass-through disks or vHBA.."
Write-Host "Skipping migration.."
Write-Host " "
Write-Host " "
}
}
After migrations, please check SCVMM job results to see if migrations are successful.
Posted in Windows Powershell, Windows Server | No Comment | 2,486 views | 24/03/2014 11:02
You can query network adapter information via WMI. Just add your all hosts into Servers.txt and use following script:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| $Servers = Get-Content Servers.txt
foreach ($Server in $Servers)
{
$ErrorActionPreference = "silentlycontinue"
$Ping = New-Object System.Net.Networkinformation.ping
$Status = $Null
$Status = ($Ping.Send("$Server", 1)).Status
if ($Status -eq "Success")
{
ForEach ($Adapter in (Get-WmiObject Win32_NetworkAdapter -ComputerName $Server))
{
$Config = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "Index = '$($Adapter.Index)'" -ComputerName $Server
if ($Config.IPAddress)
{
$Output = $Server + ";" + $Adapter.Name + ";" + $Config.IPAddress[0] + ";" + $Config.MacAddress
Write-Host $Output
}
}
}
} |
$Servers = Get-Content Servers.txt
foreach ($Server in $Servers)
{
$ErrorActionPreference = "silentlycontinue"
$Ping = New-Object System.Net.Networkinformation.ping
$Status = $Null
$Status = ($Ping.Send("$Server", 1)).Status
if ($Status -eq "Success")
{
ForEach ($Adapter in (Get-WmiObject Win32_NetworkAdapter -ComputerName $Server))
{
$Config = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "Index = '$($Adapter.Index)'" -ComputerName $Server
if ($Config.IPAddress)
{
$Output = $Server + ";" + $Adapter.Name + ";" + $Config.IPAddress[0] + ";" + $Config.MacAddress
Write-Host $Output
}
}
}
}
After that you will see all network adapters and ip configurations.
Posted in Virtual Machine Manager, Windows Powershell, Windows Server | No Comment | 7,377 views | 17/03/2014 15:51
You can use following script to get HBA Info and Port WWN:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| function Get-WinHBAInfo
{
param ($ComputerName="localhost")
# Get HBA Information
$Port = Get-WmiObject -ComputerName $ComputerName -Class MSFC_FibrePortHBAAttributes -Namespace "root\WMI"
$HBAs = Get-WmiObject -ComputerName $ComputerName -Class MSFC_FCAdapterHBAAttributes -Namespace "root\WMI"
$HBAProperties = $HBAs | Get-Member -MemberType Property, AliasProperty | Select -ExpandProperty name | ? {$_ -notlike "__*"}
$HBAs = $HBAs | Select $HBAProperties
$HBAs | %{ $_.NodeWWN = ((($_.NodeWWN) | % {"{0:x2}" -f $_}) -join ":").ToUpper() }
ForEach($HBA in $HBAs) {
# Get Port WWN
$PortWWN = (($Port |? { $_.instancename -eq $HBA.instancename }).attributes).PortWWN
$PortWWN = (($PortWWN | % {"{0:x2}" -f $_}) -join ":").ToUpper()
Add-Member -MemberType NoteProperty -InputObject $HBA -Name PortWWN -Value $PortWWN
# Output
$HBA
}
} |
function Get-WinHBAInfo
{
param ($ComputerName="localhost")
# Get HBA Information
$Port = Get-WmiObject -ComputerName $ComputerName -Class MSFC_FibrePortHBAAttributes -Namespace "root\WMI"
$HBAs = Get-WmiObject -ComputerName $ComputerName -Class MSFC_FCAdapterHBAAttributes -Namespace "root\WMI"
$HBAProperties = $HBAs | Get-Member -MemberType Property, AliasProperty | Select -ExpandProperty name | ? {$_ -notlike "__*"}
$HBAs = $HBAs | Select $HBAProperties
$HBAs | %{ $_.NodeWWN = ((($_.NodeWWN) | % {"{0:x2}" -f $_}) -join ":").ToUpper() }
ForEach($HBA in $HBAs) {
# Get Port WWN
$PortWWN = (($Port |? { $_.instancename -eq $HBA.instancename }).attributes).PortWWN
$PortWWN = (($PortWWN | % {"{0:x2}" -f $_}) -join ":").ToUpper()
Add-Member -MemberType NoteProperty -InputObject $HBA -Name PortWWN -Value $PortWWN
# Output
$HBA
}
}
Usage:
Get-WinHBAInfo -ComputerName Server01 |
Get-WinHBAInfo -ComputerName Server01
After that you will see HBA and Port WWN information.
Posted in Windows Powershell, Windows Server | No Comment | 6,310 views | 16/03/2014 08:38
You can use following script to verify your PTR records.
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
| # Get Reverse Lookup Zones
$ReverseLookupZones = Get-DnsServerZone | Where IsReverseLookupZone -eq $True | Where IsAutoCreated -eq $False
foreach ($ReverseLookupZone in $ReverseLookupZones)
{
# Clear Variables
$Servers = $Null;
# Get Zone Information
$DNSZoneName = $ReverseLookupZone.ZoneName
# Get IP Information
$ReverseIP = $ReverseLookupZone.ZoneName.TrimEnd(".in-addr.arpa");
$ReverseIPSuffix = $ReverseIP.Split(".")
[array]::reverse($ReverseIPSuffix)
$ReverseIPSuffix = $ReverseIPSuffix -join "."
# Get Servers
$Servers = Get-DnsServerResourceRecord -ZoneName $DNSZoneName | Where HostName -ne "@"
foreach ($Server in $Servers)
{
# Get Server IP Address
$ServerHostName = $Server.HostName
$ServerIPSuffix = $ServerHostName.Split(".")
[array]::reverse($ServerIPSuffix)
$ServerIPSuffix = $ServerIPSuffix -join "."
$ServerIPAddress = $ReverseIPSuffix + "." + $ServerIPSuffix
# Get Server DNS Hostname
$ServerDNSName = $Server.RecordData.PtrDomainName
$ServerDNSName = $ServerDNSName.TrimEnd(".")
Write-Host Working on $ServerDNSName ..
# Get Server DNS Subnet
$ServerDNSSubnet = $ServerIPAddress.Split(".")[0] + "." + $ServerIPAddress.Split(".")[1] + "." + $ServerIPAddress.Split(".")[2] + ".0/24"
# Resolve DNS Name
$DNSName = (Resolve-DnsName $ServerDNSName)
if ($DNSName)
{
# Clear Values
$Control = 0;
foreach ($DNSRecord in $DNSName)
{
# Get Reverse DNS Name
$DNSIPAddress = $DNSRecord.IPAddress
if ($DNSIPAddress -eq $ServerIPAddress)
{
$Control = 1;
}
}
if ($Control -eq "0")
{
$Output = $ServerIPAddress + ";" + $ServerDNSSubnet + ";" + $ServerDNSName + ";" + $DNSIPAddress
Add-Content -Value $Output -Path PTRError.txt
Write-Warning $Output
}
}
}
} |
# Get Reverse Lookup Zones
$ReverseLookupZones = Get-DnsServerZone | Where IsReverseLookupZone -eq $True | Where IsAutoCreated -eq $False
foreach ($ReverseLookupZone in $ReverseLookupZones)
{
# Clear Variables
$Servers = $Null;
# Get Zone Information
$DNSZoneName = $ReverseLookupZone.ZoneName
# Get IP Information
$ReverseIP = $ReverseLookupZone.ZoneName.TrimEnd(".in-addr.arpa");
$ReverseIPSuffix = $ReverseIP.Split(".")
[array]::reverse($ReverseIPSuffix)
$ReverseIPSuffix = $ReverseIPSuffix -join "."
# Get Servers
$Servers = Get-DnsServerResourceRecord -ZoneName $DNSZoneName | Where HostName -ne "@"
foreach ($Server in $Servers)
{
# Get Server IP Address
$ServerHostName = $Server.HostName
$ServerIPSuffix = $ServerHostName.Split(".")
[array]::reverse($ServerIPSuffix)
$ServerIPSuffix = $ServerIPSuffix -join "."
$ServerIPAddress = $ReverseIPSuffix + "." + $ServerIPSuffix
# Get Server DNS Hostname
$ServerDNSName = $Server.RecordData.PtrDomainName
$ServerDNSName = $ServerDNSName.TrimEnd(".")
Write-Host Working on $ServerDNSName ..
# Get Server DNS Subnet
$ServerDNSSubnet = $ServerIPAddress.Split(".")[0] + "." + $ServerIPAddress.Split(".")[1] + "." + $ServerIPAddress.Split(".")[2] + ".0/24"
# Resolve DNS Name
$DNSName = (Resolve-DnsName $ServerDNSName)
if ($DNSName)
{
# Clear Values
$Control = 0;
foreach ($DNSRecord in $DNSName)
{
# Get Reverse DNS Name
$DNSIPAddress = $DNSRecord.IPAddress
if ($DNSIPAddress -eq $ServerIPAddress)
{
$Control = 1;
}
}
if ($Control -eq "0")
{
$Output = $ServerIPAddress + ";" + $ServerDNSSubnet + ";" + $ServerDNSName + ";" + $DNSIPAddress
Add-Content -Value $Output -Path PTRError.txt
Write-Warning $Output
}
}
}
}
You should run this script on your Windows Server 2012/R2 DNS server with elevated privileges.
|