I ended up downloading a copy of Bash for Windows then invoking a context script like this

====================================================================

cd c:/

if [ -f c:/install_complete.txt ]; then
   exit 1
fi

source d:/context.sh

echo "Setting IP to $IP"
echo "Setting GATEWAY TO $GATEWAY"
echo "Setting NETMASK to $NETMASK"
echo "$COMPUTERNAME"

c:/windows/system32/netsh interface ip set address "Local Area Connection 2" static $IP $NETMASK $GATEWAY 1 2>&1 > c:/install_complete.txt


c:/windows/system32/netsh interface show interface 2>&1 > c:/install_complete.txt

shutdown /f /r /t 0

======================================================================

I then added a batch script that I invoke at every boot that executes that bash script. Here is how to have scripts run at boot

http://www.tutorial5.com/content/view/157/47/


Script you posted. What kind of script is it :-) ? It may be perhaps better than my Bash script.

Hope it helps,

Vladimir

On Fri, 5 Aug 2011, André Monteiro wrote:

 Hello,
 
I've been working in a windows script to contextualize VMs.
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 andremonte...@ua.pt and tsbati...@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
Thu May 12 02:22:09 PDT 2011

 *  Previous message: [one-users] How config Windows vm template?
 *  Next message: [one-users] having a problem when shut down a VM
 *  Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

_________________________________________________________________________________________________________________________________________

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 Computing
www.OpenNebula.org <http://www.opennebula.org/> | cmartin at opennebula.org


2011/5/6 ddd <xrchina2008 at 163.com>

>  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
>
>


_______________________________________________
Users mailing list
Users@lists.opennebula.org
http://lists.opennebula.org/listinfo.cgi/users-opennebula.org

Reply via email to