IF and Else statement

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Wei-Yen Tan Wei-Yen Tan 7 months, 3 weeks ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #34710
    Profile photo of Nicolai Magnussen
    Nicolai Magnussen
    Participant

    Hi Guys.
    Now I'm really starting to learn the capabilities of PowerShell.
    I'am writing a script.

    I think I'm onto something.
    As you can see I'am trying to run the script on the hyperv01, could I do it With Invoke-Command?

    #34711
    Profile photo of Nicolai Magnussen
    Nicolai Magnussen
    Participant

    I tried to paste the script to github.
    But here you og

    New-PSSession -ComputerName HyperV01
    2 @(
    3 # Created by Nicolai Magnussen | MCP
    4 $VMLocation = "\\hyperv01\c$\ClusterStorage"
    5 $VMNetwork = Read-Host 'Velg, LAN eller WAN'
    6 $VNName = Read-Host 'Skriv inn navn'
    7 $VMDiskSize = Read-Host 'Skriv inn disk størrelse i GB, Eks 200GB'
    8 $VMMemory = Read-Host 'Skriv inn RAM størrelse i MB, eks 16384MB'
    9 @)
    10

    11 # Create A procuction machine
    12 $VMMemory = 16384MB
    13 $VMDiskSize = 300GB
    14 New-VM -Name $VMName -Generation 2 -BootDevice CD -MemoryStartupBytes $VMMemory -SwitchName $VMNetwork -Path $VMLocation -NoVHD -Verbose
    15 New-VHD -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName.vhdx" -SizeBytes $VMDiskSize -Verbose
    16 Add-VMHardDiskDrive -VMName $VMName -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName.vhdx" -Verbose

    #34712
    Profile photo of Nicolai Magnussen
    Nicolai Magnussen
    Participant

    Code

    #34717
    Profile photo of Steve Flowers
    Steve Flowers
    Participant

    Yes, you could perform the above code with an Invoke-Command script block as long as WinRM is configured properly.

    #34739
    Profile photo of Nicolai Magnussen
    Nicolai Magnussen
    Participant

    Yes, I figured that out.
    But how should I do the code.

    New-PSSession -ComputerName HyperV01
    { 2
    3 # Created by Nicolai Magnussen | MCP
    4 $VMLocation = "\\hyperv01\c$\ClusterStorage"
    5 $VMNetwork = Read-Host 'Velg, LAN eller WAN'
    6 $VNName = Read-Host 'Skriv inn navn'
    7 $VMDiskSize = Read-Host 'Skriv inn disk størrelse i GB, Eks 200GB'
    8 $VMMemory = Read-Host 'Skriv inn RAM størrelse i MB, eks 16384MB'
    9
    10

    11 # Create A procuction machine
    12 $VMMemory = 16384MB
    13 $VMDiskSize = 300GB
    14 New-VM -Name $VMName -Generation 2 -BootDevice CD -MemoryStartupBytes $VMMemory -SwitchName $VMNetwork -Path $VMLocation -NoVHD -Verbose
    15 New-VHD -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName.vhdx" -SizeBytes $VMDiskSize -Verbose
    16 Add-VMHardDiskDrive -VMName $VMName -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName.vhdx" -Verbose
    }

    Is this enough?

    #34815
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Why not try it and have the $VMMemory in Variables so you can customise the size to test?

    try invoke-command -computername computername {code here}

    you can look up the help andd examples

    You have multiple commands so i am not so sure it will

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.