Hey all,
I have this script part way done. My questions are:
1. I want to find out the nubmber of "Winxp-X" are currently deployed then I want to add more. I don't want to use a CSV or anyother format to inmport from. Yes i'm that lazy. For some reason i can't figure it out!
2. On the line that askes "What esx host do you want to use" i would like to just hit "1" for esx1 vs typing out the name of the host.
Can the above questions be done? if so HELP! I have been working on this for days. Thanks!
Yes i a Connect-VIServer -Server blah blah blah... Just removed.
{
{
$freespaceCalc = @{ Name = "FreespaceGB"; Expression = { ::Round($_.FreeSpaceMB * 1MB / 1GB ) } }
$capacityCalc = @{ Name = "CapacityGB"; Expression = { ::Round($_.CapacityMB * 1MB / 1GB) } }
User enters how manny agents are requred
$agentTotal = Read-Host "Enter how many WinXp Agents you need."
Get-Datastore | Select Name, $freespaceCalc, $capacityCalc |Sort-Object Name | ft -Force -AutoSize| Out-Default
$datastore = Read-Host "What DataStore do you want to use?"
Get-VMHost | Select-Object Name | Sort Name |ft -Force -AutoSize | Out-Default
$esxHost = Read-Host "What Esx-Host Do you want to use?"
$XP = Get-VM -Name WinXP-*
$totalXPCount = @($XP).Length
$totalXP = (1 + $totalXPCount)
for($adt = ($totalXP + $agentTotal);$totalXP -lt $adt; $totalXP++){
if($totalXPCount -eq $adt)
{
break
}
$machineName = "WinXP-$totalXPCount"
$template = "XP-32 Template"
$osspec = Get-OSCustomizationSpec XP
foreach ($vm in $machineName)
{$vm = New-VM -Name $vm -Template $template -Host $esxHost -Datastore $datastore -OSCustomizationSpec $osspec |start-vm }
}
The variable $hostIndex contains the index in array $hostList of the selected host.
This is the corrected script.
Note that the forum SW has problems with square brackets so you better use the attached file.
# Current "** Create Win XP **"; $freespaceCalc = @{ Name = "FreespaceGB"; Expression = { [Math]::Round($_.FreeSpaceMB * 1MB / 1GB ) } } $capacityCalc = @{ Name = "CapacityGB"; Expression = { [Math]::Round($_.CapacityMB * 1MB / 1GB) } } # User enters how manny agents are requred $agentTotal = [int](Read-Host "Enter how many WinXp Agents you need.") $totalXPCount = (Get-VM -Name WinXP-* | Measure-Object).Count if($totalXPCount -lt $agentTotal){ for($adt = $totalXPCount + 1; $adt -le $agentTotal; $adt++){ Get-Datastore | Select Name, $freespaceCalc, $capacityCalc |Sort-Object Name | ft -Force -AutoSize| Out-Default $datastore = Read-Host "What DataStore do you want to use?" $hostList = Get-VmHost | select Name | Sort-Object $i = 1 $hostList | % {Write-Host $i ":" $_.Name; $i++} $hostIndex = Read-Host "Enter a number (1 -" $hostList.Count ")" Write-Host "You selected host" $hostIndex "with hostname" $hostList[$hostIndex - 1].Name $machineName = "WinXP-$adt" $template = "XP-32 Template" $osspec = Get-OSCustomizationSpec XP foreach ($vm in $machineName){ $vm = New-VM -Name $machineName -Template $template -Host $hostList[$hostIndex - 1].Name -Datastore $datastore -OSCustomizationSpec $osspec |start-vm } } }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
1) The following script snippet will run through the loop the number of missing guests times.
$agentTotal = [int](Read-Host "Enter how many WinXp Agents you need.") $totalXPCount = (Get-VM -Name WinXP-* | Measure-Object).Count if($totalXPCount -lt $agentTotal){ # Add missing XP guests for($adt = $totalXPCount + 1; $adt -le $agentTotal; $adt++){ Write-Host "Adding guest" $adt # Execute the New-Vm part } }
2) The following script snippet displays the hostnames prefixed with a sequential number.
With the number, in variable $hostIndex, you can access the selected host in the array $hostList
$hostList = Get-VmHost | select Name | Sort-Object $i = 1 $hostList | %{Write-Host $i ":" $_.Name; $i++} $hostIndex = Read-Host "Enter a number (1 -" $hostList.Count ")" Write-Host "You selected host" $hostIndex "with hostname" $hostList[$hostIndex - 1].Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi,
I did notice that you said "I don't want to use CSV" but did you see this document?
--
Wil
_____________________________________________________
VI-Toolkit & scripts wiki at http://www.vi-toolkit.com
LucD,
Your post is just want i needed. But now i get the following:
New-VM : 8/12/2009 4:32:00 PM New-VM Could not find VMHost with name '1'.
At C:\Documents and Settings\Administrator\Desktop\Scripts\Agent-Deploy.ps1:92 char:19
+ {$vm = New-VM <<<< -Name $vm -Template $template -Host $hostIndex -Datastore $datastore -OSCustomizationSpec $osspec |start-vm }
New-VM : Value cannot be found for the mandatory parameter VMHost
At C:\Documents and Settings\Administrator\Desktop\Scripts\Agent-Deploy.ps1:92 char:19
+ {$vm = New-VM <<<< -Name $vm -Template $template -Host $hostIndex -Datastore $datastore -OSCustomizationSpec $osspec |start-vm }
What did I do wrong?
Thankx!
Current "** Create Win XP **";
$freespaceCalc = @{ Name = "FreespaceGB"; Expression = { ::Round($_.FreeSpaceMB * 1MB / 1GB ) } }
$capacityCalc = @{ Name = "CapacityGB"; Expression = { ::Round($_.CapacityMB * 1MB / 1GB) } }
User enters how manny agents are requred
$agentTotal = (Read-Host "Enter how many WinXp Agents you need.")
$totalXPCount = (Get-VM -Name WinXP-* | Measure-Object).Count
if($totalXPCount -lt $agentTotal){
Add missing XP guests
for($adt = $totalXPCount + 1; $adt -le $agentTotal; $adt++){
Execute the New-Vm part
}
}
Get-Datastore | Select Name, $freespaceCalc, $capacityCalc |Sort-Object Name | ft -Force -AutoSize| Out-Default
$datastore = Read-Host "What DataStore do you want to use?"
$hostList = Get-VmHost | select Name | Sort-Object
$i = 1
$hostList | %{Write-Host $i ":" $_.Name; $i++}
$hostIndex = Read-Host "Enter a number (1 -" $hostList.Count ")"
Write-Host "You selected host" $hostIndex "with hostname" $hostList[$hostIndex - 1].Name
$XP = Get-VM -Name WinXP-*
$totalXPCount = @($XP).Length
$totalXP = ($totalXPCount + 1)
for($adt = ($totalXP + $agentTotal);$totalXP -lt $adt; $totalXP++){
if($totalXPCount -eq $adt)
{
break
}
$machineName = "WinXP-$totalXPCount"
$template = "XP-32 Template"
$osspec = Get-OSCustomizationSpec XP
foreach ($vm in $machineName)
{$vm = New-VM -Name $machineName -Template $template -Host $hostIndex -Datastore $datastore -OSCustomizationSpec $osspec |start-vm }
}
Disconnect-VIServer -Server $DefaultVIServer -Confirm:$false
The variable $hostIndex contains the index in array $hostList of the selected host.
This is the corrected script.
Note that the forum SW has problems with square brackets so you better use the attached file.
# Current "** Create Win XP **"; $freespaceCalc = @{ Name = "FreespaceGB"; Expression = { [Math]::Round($_.FreeSpaceMB * 1MB / 1GB ) } } $capacityCalc = @{ Name = "CapacityGB"; Expression = { [Math]::Round($_.CapacityMB * 1MB / 1GB) } } # User enters how manny agents are requred $agentTotal = [int](Read-Host "Enter how many WinXp Agents you need.") $totalXPCount = (Get-VM -Name WinXP-* | Measure-Object).Count if($totalXPCount -lt $agentTotal){ for($adt = $totalXPCount + 1; $adt -le $agentTotal; $adt++){ Get-Datastore | Select Name, $freespaceCalc, $capacityCalc |Sort-Object Name | ft -Force -AutoSize| Out-Default $datastore = Read-Host "What DataStore do you want to use?" $hostList = Get-VmHost | select Name | Sort-Object $i = 1 $hostList | % {Write-Host $i ":" $_.Name; $i++} $hostIndex = Read-Host "Enter a number (1 -" $hostList.Count ")" Write-Host "You selected host" $hostIndex "with hostname" $hostList[$hostIndex - 1].Name $machineName = "WinXP-$adt" $template = "XP-32 Template" $osspec = Get-OSCustomizationSpec XP foreach ($vm in $machineName){ $vm = New-VM -Name $machineName -Template $template -Host $hostList[$hostIndex - 1].Name -Datastore $datastore -OSCustomizationSpec $osspec |start-vm } } }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Thank you again! This worked great! I have been jammed at work. Sorry for the delay. Thank you again very much!
J
This might help you figure out how to use a CSV file for input.