Posted in Windows Powershell, Windows Server | No Comment | 1,904 views | 21/06/2013 16:55
You can search a VM name in your Hyper-V host list like this:
$VMName = "Server Name"
$Servers = Get-Content C:\servers.txt
foreach ($Server in $Servers)
{
$VMs = Get-WMIObject -Class Msvm_ComputerSystem -Namespace "root\virtualization" -ComputerName $Server
foreach ($VM in $VMs)
{
if ($VM.ElementName -eq $VMName)
{
Write-Host $Server
}
}
} |
$VMName = "Server Name"
$Servers = Get-Content C:\servers.txt
foreach ($Server in $Servers)
{
$VMs = Get-WMIObject -Class Msvm_ComputerSystem -Namespace "root\virtualization" -ComputerName $Server
foreach ($VM in $VMs)
{
if ($VM.ElementName -eq $VMName)
{
Write-Host $Server
}
}
}
It uses WMI so both works on Windows Server 2008 and Windows Server 2012.
Posted in Windows Powershell, Windows Server | No Comment | 9,338 views | 21/06/2013 16:34
You may get this error when you try to open Failover Cluster Manager (Windows Server 2012):
Failover Cluster Manager failed while managing one or more cluster. The error was ‘An error occurred trying to display the cluster information.’. For more information see the Failover Cluster Manager Diagnostic channel.
You can fix this problem by this command via CMD:
1
2
| cd C:\Windows\System32\wbem
mofcomp .\ClusWMI.mof |
cd C:\Windows\System32\wbem
mofcomp .\ClusWMI.mof
If you have SCCM agent on your server, then that might cause this error. Try to update your SCCM agent to latest version.
Posted in Windows Powershell | No Comment | 2,047 views | 21/06/2013 15:54
You can resolve ip addresses of many servers via Powershell.
1
2
3
4
5
6
7
8
9
10
11
12
| $Servers = Get-Content C:\Servers.txt
foreach ($Server in $Servers)
{
$IPAddress = $null
$IPAddress = [System.Net.Dns]::GetHostAddresses("$Server").IPAddressToString
Add-Content -Path C:\ServerName.txt -Value $Server
if ($IPAddress -eq $null)
{
$IPAddress = "Server IP cannot resolve."
}
Add-Content -Path C:\ServerIP.txt -Value $IPAddress
} |
$Servers = Get-Content C:\Servers.txt
foreach ($Server in $Servers)
{
$IPAddress = $null
$IPAddress = [System.Net.Dns]::GetHostAddresses("$Server").IPAddressToString
Add-Content -Path C:\ServerName.txt -Value $Server
if ($IPAddress -eq $null)
{
$IPAddress = "Server IP cannot resolve."
}
Add-Content -Path C:\ServerIP.txt -Value $IPAddress
}
It creates two different txt file, so you can merge them in an excel file.
Posted in Windows Powershell, Windows Server | 2 Comments | 2,253 views | 21/06/2013 09:49
You can check ip addresses of server names like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| $Servers = Get-Content C:\Servers.txt
foreach ($Server in $Servers)
{
$IPAddress = [System.Net.Dns]::GetHostAddresses("$Server").IPAddressToString
If ($IPAddress -like "172.29*")
{
Write-Host "Test Server"
Add-Content -Path C:\testservers.txt -Value $Server
}
else
{
Write-Host "Prod Server"
Add-Content -Path C:\prodservers.txt -Value $Server
}
} |
$Servers = Get-Content C:\Servers.txt
foreach ($Server in $Servers)
{
$IPAddress = [System.Net.Dns]::GetHostAddresses("$Server").IPAddressToString
If ($IPAddress -like "172.29*")
{
Write-Host "Test Server"
Add-Content -Path C:\testservers.txt -Value $Server
}
else
{
Write-Host "Prod Server"
Add-Content -Path C:\prodservers.txt -Value $Server
}
}
As you see, i split them into 2 different groups.
Posted in Windows Powershell, Windows Server | No Comment | 2,866 views | 19/06/2013 10:29
You can use this script to get virtual machines which has Virtual HBA support.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| $Servers = Get-Content C:\hosts.txt
foreach ($Server in $Servers)
{
$VMs = Get-VM -ComputerName $Server
Write-Host $Server
foreach ($VM in $VMs)
{
$TestFC = $VM | Select -expand FibreChannelHostBusAdapters
if ($TestFC)
{
$Value = $VM.Name + "," + $VM.ComputerName
Add-Content -Value $Value -Path C:\Servers.txt
}
}
} |
$Servers = Get-Content C:\hosts.txt
foreach ($Server in $Servers)
{
$VMs = Get-VM -ComputerName $Server
Write-Host $Server
foreach ($VM in $VMs)
{
$TestFC = $VM | Select -expand FibreChannelHostBusAdapters
if ($TestFC)
{
$Value = $VM.Name + "," + $VM.ComputerName
Add-Content -Value $Value -Path C:\Servers.txt
}
}
}
I’ve added all my Hyper-V hostnames into C:\hosts.txt to use this script.
Posted in Windows Powershell, Windows Server | 1 Comment | 3,603 views | 18/06/2013 13:39
This is an example to show you how to get hardware information of AD clients via PowerShell.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| $Servers = Get-ADComputer -Filter {Name -like "C*"}
Foreach ($Server in $Servers)
{
$Hostname = $Server.Name
$ServerInfo = (Get-WmiObject -Computername $Hostname Win32_ComputerSystem)
$Manufacturer = $ServerInfo.Manufacturer
$Model = $ServerInfo.Model
Write-Host "Name: $Hostname"
Write-Host "Manufacturer: $Manufacturer"
Write-Host "Model: $Model"
Write-Host " "
$Content = "$Hostname;$Manufacturer;$Model"
Add-Content -Value $Content -Path "C:\ServerInfo.txt"
} |
$Servers = Get-ADComputer -Filter {Name -like "C*"}
Foreach ($Server in $Servers)
{
$Hostname = $Server.Name
$ServerInfo = (Get-WmiObject -Computername $Hostname Win32_ComputerSystem)
$Manufacturer = $ServerInfo.Manufacturer
$Model = $ServerInfo.Model
Write-Host "Name: $Hostname"
Write-Host "Manufacturer: $Manufacturer"
Write-Host "Model: $Model"
Write-Host " "
$Content = "$Hostname;$Manufacturer;$Model"
Add-Content -Value $Content -Path "C:\ServerInfo.txt"
}
I’ve used filter to get specific computers.
Posted in Windows Powershell, Windows Server | No Comment | 1,497 views | 18/06/2013 10:10
You can get your VMs list, Hyper-V hostname, clustername and csv paths like this:
1
2
3
4
5
6
7
8
9
10
11
| $VMs = Get-VM
foreach ($VM in $VMs)
{
$VMName = $VM.Name
$VMHostname = $VM.ComputerName
$VMHostCluster = (Get-Cluster).Name
$VMHardDisk = $VM.ConfigurationLocation
$VMHardDiskLocation = $VMHardDisk.Split("\")[2]
$Value = $VMName + "," + $VMHostname + "," + $VMHostCluster + "," + $VMHardDiskLocation
Add-Content -Value $Value -Path "VMList.txt"
} |
$VMs = Get-VM
foreach ($VM in $VMs)
{
$VMName = $VM.Name
$VMHostname = $VM.ComputerName
$VMHostCluster = (Get-Cluster).Name
$VMHardDisk = $VM.ConfigurationLocation
$VMHardDiskLocation = $VMHardDisk.Split("\")[2]
$Value = $VMName + "," + $VMHostname + "," + $VMHostCluster + "," + $VMHardDiskLocation
Add-Content -Value $Value -Path "VMList.txt"
}
You need to run this script on all Hyper-V hosts.
|