-1

I'm trying to create a function with the parameters to create the VM this is where I'm at.

I don't get any errors but nothing executes.

Function Create-VM {
param (
    [string] $SRV1 = "Windows7",    
    [string] $SRAM = "512",
    [string] $SRV1VHD = "41943040000",
    [string] $VMLOC = "C:\users\Public\documents\hyper-v\virtual hard disks",
    [string] $Network1 = "Local Area Connection – Virtual Switch")

    # Create Virtual Machines
    MD $VMLoc –erroraction silentlycontinue
    new-vm $SRV1 -path $VMLoc
    new-vhd -vhdpaths $VMLoc\$SRV1 -size $SRV1VHD
    add-vmdisk -vm $SRV1 -controllerid 0 -lun 0 -path $VMLoc\$SRV1
    get-vm $SRV1 | add-vmdrive -controllerid 1 -lun 0 -dvd
    get-vm $SRV1 | set-vmmemory -memoryinbytes $SRAM
    get-vm $SRV1 | add-vmnic -virtualswitch $Network1


}
Musa
  • 553
  • 1
  • 7
  • 19
  • What is wrong? What have you tried? Did you remember to invoke `Create-VM` after executing the script above(which only creates the function)? What happends if you try to run line by live (set variables and run line by line of the function)? – Frode F. Mar 19 '14 at 22:41
  • How (and when) are you calling `Create-VM`? The function won't execute itself. – Ansgar Wiechers Mar 19 '14 at 22:53
  • @FrodeF. Yes. After I run the script, I run "Create-VM" in the ISE terminal and I get "PS C:\Users\Administrator\Documents> Create-VM The term 'New-VM' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling o f the name, or if a path was included, verify that the path is correct and try again." – Musa Mar 20 '14 at 18:54
  • You need to import the module in Powershell 2.0. See my answer – Frode F. Mar 20 '14 at 19:00

1 Answers1

0

I seems like you're using PowerShell 2.0. In 2.0, you need to import modules to use the commands. If you update to 3.0 or 4.0(recommended), this would happend automatically as long as you have the module installed. This looks like the HyperV module from CodePlex, so try this:

Function Create-VM {
param (
    [string] $SRV1 = "Windows7",    
    [string] $SRAM = "512",
    [string] $SRV1VHD = "41943040000",
    [string] $VMLOC = "C:\users\Public\documents\hyper-v\virtual hard disks",
    [string] $Network1 = "Local Area Connection – Virtual Switch")

    #Import HyperV module
    Import-Module HyperV

    # Create Virtual Machines
    MD $VMLoc –erroraction silentlycontinue
    new-vm $SRV1 -path $VMLoc
    new-vhd -vhdpaths $VMLoc\$SRV1 -size $SRV1VHD
    add-vmdisk -vm $SRV1 -controllerid 0 -lun 0 -path $VMLoc\$SRV1
    get-vm $SRV1 | add-vmdrive -controllerid 1 -lun 0 -dvd
    get-vm $SRV1 | set-vmmemory -memoryinbytes $SRAM
    get-vm $SRV1 | add-vmnic -virtualswitch $Network1

}

Create-VM
Frode F.
  • 52,376
  • 9
  • 98
  • 114