IF and Else statement

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?

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

Code

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

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?

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