[one-users] How config Windows vm template?
André Monteiro
andremonteiro at ua.pt
Fri Aug 5 06:40:20 PDT 2011
Hello,
I've been working in a windows script to contextualize
VMs.<http://www.pastie.org/2324611By>
<http://www.pastie.org/2324611By>
By your experience, what's the best way to run the script automatically when
creating a new instance? I can create it manually as a CD-ROM and run the
windows7.one with the drive attached, but is there an automatic way?
################### Windows context configuring script ###################
########### Created by andremonteiro at ua.pt and tsbatista at ua.pt ###########
[string]$computerName = "$env:computername"
[string]$ConnectionString = "WinNT://$computerName"
[string]$ipaddress = ""
function getContext($file) {
$context = @{}
switch -regex -file $file {
'(.+)="(.+)"' {
$name,$value = $matches[1..2]
$context[$name] = $value
}
}
return $context
}
function addLocalUser($context) {
# Create new user
$username = $context["username"]
$ADSI = [adsi]$ConnectionString
$user = $ADSI.Create("user",$userName)
$user.setPassword($context["user_password"])
$user.SetInfo()
# Add user to local Administrators
$groups = "Administrators", "Administradores"
foreach ($grp in $groups) {
if([ADSI]::Exists("WinNT://$computerName/$grp,group")) {
$group = [ADSI] "WinNT://$computerName/$grp,group"
$group.Add("WinNT://$computerName/$username")
}
}
}
function getIp($mac) {
$octet = $mac.Split(":")
[String] $ip = ""
$ip += [convert]::toint32($octet[2],16)
$ip += "."+[convert]::toint32($octet[3],16)
$ip += "."+[convert]::toint32($octet[4],16)
$ip += "."+[convert]::toint32($octet[5],16)
$ipaddress = $ip
return $ip
}
function getGateway($mac) {
$octet = $mac.Split(":")
[String] $ip = ""
$ip += [convert]::toint32($octet[2],16)
$ip += "."+[convert]::toint32($octet[3],16)
$ip += "."+[convert]::toint32($octet[4],16)
$ip += ".1"
return $ip
}
function configureNetwork($context) {
$Nics = Get-WMIObject Win32_NetworkAdapterConfiguration | where
{$_.IPEnabled -eq "TRUE" -and ($_.MACAddress)}
foreach ($nic in $Nics) {
[String]$mac = $nic.MACAddress
[String]$ip = getIp($mac)
[String]$gw = getGateway($mac)
$nic.ReleaseDHCPLease()
$nic.EnableStatic($ip , "255.255.255.0")
$nic.SetGateways($gw)
$nic.SetDNSServerSearchOrder($gw)
$nic.SetDynamicDNSRegistration("FALSE")
}
}
function renameComputer($context) {
$ComputerInfo = Get-WmiObject -Class Win32_ComputerSystem
$ComputerInfo.rename($context["HOSTNAME"]+$ipaddress.Split(".")[3])
}
function enableRemoteDesktop()
{
#Get RDP parameters
#(Get-WmiObject -Class "Win32_TerminalServiceSetting" -Namespace
root\cimv2\terminalservices).AllowTsConnections
#Set RDP state
#(Get-WmiObject -Class "Win32_TerminalServiceSetting" -Namespace
root\cimv2\terminalservices).SetAllowTsConnections(1)
#Get RDP Required Authentication
#(Get-WmiObject -class "Win32_TSGeneralSetting" -Namespace
root\cimv2\terminalservices -Filter
"TerminalName='RDP-tcp'").UserAuthenticationRequired
#Set RDP Required Authentication
#(Get-WmiObject -class "Win32_TSGeneralSetting" -Namespace
root\cimv2\terminalservices -Filter
"TerminalName='RDP-tcp'").SetUserAuthenticationRequired(1)
$Terminal = (Get-WmiObject -Class "Win32_TerminalServiceSetting"
-Namespace root\cimv2\terminalservices).SetAllowTsConnections(1)
return $Terminal
}
# Create folder Context if unexistant
if( -not(Test-Path "c:\context\")) {
New-Item "c:\context\" -type directory
}
# Execute the script
if( -not(Test-Path "c:\context\contextualized") -and(Test-Path
"D:\context.sh")) {
$context = @{}
$context = getContext('D:\context.sh')
Set-ExecutionPolicy unrestricted -force # not needed if already done
once on the VM
addLocalUser($context)
configureNetwork($context)
renameComputer($context)
enableRemoteDesktop()
echo "contextualized" |Out-File ("c:\context\contextualized")
restart-computer
}
# remember to disable password complexity requirements in computer
# management while rpeparing the cloud image!
# Remember to run "Set-ExecutionPolicy bypass -force" before this!
--
André Monteiro
*Carlos Martín Sánchez* cmartin at opennebula.org
<users%40lists.opennebula.org?Subject=Re%3A%20%5Bone-users%5D%20How%20config%20Windows%20vm%20template%3F&In-Reply-To=%3CBANLkTimxR8Z3MbY_PJjPNqGNafegwYs4wQ%40mail.gmail.com%3E>
*Thu May 12 02:22:09 PDT 2011*
- Previous message: [one-users] How config Windows vm template?
<http://lists.opennebula.org/pipermail/users-opennebula.org/2011-May/005192.html>
- Next message: [one-users] having a problem when shut down a VM
<http://lists.opennebula.org/pipermail/users-opennebula.org/2011-May/005193.html>
- *Messages sorted by:* [ date
]<http://lists.opennebula.org/pipermail/users-opennebula.org/2011-May/date.html#5282>
[
thread ]<http://lists.opennebula.org/pipermail/users-opennebula.org/2011-May/thread.html#5282>
[
subject ]<http://lists.opennebula.org/pipermail/users-opennebula.org/2011-May/subject.html#5282>
[
author ]<http://lists.opennebula.org/pipermail/users-opennebula.org/2011-May/author.html#5282>
------------------------------
Hi there,
The contextualization offered by OpenNebula is creating that cdrom and
context.sh script, but it's up to the guest OS to read that file and
configure itself.
We only provide linux scripts; you would have to create similar ones. I'm
not sure how difficult that can be since I don't have experience with
Windows scripting.
If you only need to configure the right IP for your VM, then you can try to
configure a DHCP server in your network (it can be placed in a VM in the
same VNet). This has been discussed in the list, hopefully these threads
will help:
[1] http://www.mail-archive.com/users@lists.opennebula.org/msg01143.html
[2] http://www.mail-archive.com/users@lists.opennebula.org/msg01395.html
Regards
--
Carlos Martín, MSc
Project Major Contributor
OpenNebula - The Open Source Toolkit for Cloud
Computingwww.OpenNebula.org <http://www.opennebula.org/> | cmartin at
opennebula.org <http://lists.opennebula.org/listinfo.cgi/users-opennebula.org>
2011/5/6 ddd <xrchina2008 at 163.com
<http://lists.opennebula.org/listinfo.cgi/users-opennebula.org>>
>* hi,all*>* now,my winxp vm is running.but,config's ip and hostname is not*>* right.*>* now,windows vm's cd-rom is existing context.sh.*>* This is my vm config file:*>* NAME=WinVista*>* CPU=1*>* MEMORY=1024*>* OS=[boot=hd]*>* DISK =[source="/root/images/winxp_test.img", clone=yes,*>* target=hda, readonly=no]*>* NIC=[network ="Small network"]*>* CONTEXT=[*>* hostname = "$NAME$VMID",*>* ip_public = "$NIC[IP, NETWORK=\"Small network\"]",*>* target ="hdb"*>* ]*>* GRAPHICS =[type ="vnc",listen ="127.0.0.1", port = "1"]*>* windows template need config?how?*>* Please help me.*>* ---Rong Xiong*>**>**>**>**>**>**>**>**>**>* _______________________________________________*>* Users mailing list*>* Users at lists.opennebula.org <http://lists.opennebula.org/listinfo.cgi/users-opennebula.org>*>* http://lists.opennebula.org/listinfo.cgi/users-opennebula.org*>**>**
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.opennebula.org/pipermail/users-opennebula.org/attachments/20110805/ffb56c0b/attachment-0001.htm>
More information about the Users
mailing list