Posted in
Windows Powershell |
No Comment | 2,543 views | 22/05/2013 14:17
This is an example script for PoSHServer to query cluster names via HTTP get request.
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
| # Check Remote PowerShell Session
$CheckPSSession = Get-PSSession -ComputerName S0134CloudVMM04
if (!$CheckPSSession)
{
#$SecurePassword = ConvertTo-SecureString "Password_Here" -AsPlainText -Force
#$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "Domain\Username", $SecurePassword
#$Session = New-PSSession -Computername VMMHost2012 -Credential $Credentials
$Session = New-PSSession -Computername VMMHost2012
}
else
{
$Session = $CheckPSSession
}
# Get Virtual Machine Name
$VMName = $PoSHQuery.VM
# Get Virtual Machine Details Function
Function Get-InterVMDetails
{
Param ($Session, $VMName)
Invoke-Command -Session $Session -ArgumentList $VMName -ScriptBlock {
param($VMName)
$CheckModule = Get-Module -Name virtualmachinemanager -EA SilentlyContinue
if (!$CheckModule)
{
Import-Module -Name virtualmachinemanager -EA SilentlyContinue
}
$VMDetails = Get-SCVirtualMachine "$VMName"
$VMHost = $VMDetails.VMHost
$VMHostDetails = Get-SCVMHost "$VMHost"
$VMHostCluster = $VMHostDetails.HostCluster
$VMHostCluster.Name
}
}
# Get VM Details
if ($VMName)
{
$VMDetails = Get-InterVMDetails -VMName $VMName -Session $Session
}
# Output Web Interface
@"
$($VMDetails)
"@ |
# Check Remote PowerShell Session
$CheckPSSession = Get-PSSession -ComputerName S0134CloudVMM04
if (!$CheckPSSession)
{
#$SecurePassword = ConvertTo-SecureString "Password_Here" -AsPlainText -Force
#$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "Domain\Username", $SecurePassword
#$Session = New-PSSession -Computername VMMHost2012 -Credential $Credentials
$Session = New-PSSession -Computername VMMHost2012
}
else
{
$Session = $CheckPSSession
}
# Get Virtual Machine Name
$VMName = $PoSHQuery.VM
# Get Virtual Machine Details Function
Function Get-InterVMDetails
{
Param ($Session, $VMName)
Invoke-Command -Session $Session -ArgumentList $VMName -ScriptBlock {
param($VMName)
$CheckModule = Get-Module -Name virtualmachinemanager -EA SilentlyContinue
if (!$CheckModule)
{
Import-Module -Name virtualmachinemanager -EA SilentlyContinue
}
$VMDetails = Get-SCVirtualMachine "$VMName"
$VMHost = $VMDetails.VMHost
$VMHostDetails = Get-SCVMHost "$VMHost"
$VMHostCluster = $VMHostDetails.HostCluster
$VMHostCluster.Name
}
}
# Get VM Details
if ($VMName)
{
$VMDetails = Get-InterVMDetails -VMName $VMName -Session $Session
}
# Output Web Interface
@"
$($VMDetails)
"@
Save this script as “getclustername.ps1”. Put it into PoSHServer homedirectory. After that you can query like this:
http://localhost:8080/getclustername.ps1?vmname=My_VM_Name |
http://localhost:8080/getclustername.ps1?vmname=My_VM_Name
You can query any information like this via PowerShell remoting.