schtritoff / hyperv-vm-provisioning

Quick provisioning of Linux VM using Hyper-V on Windows
74 stars 28 forks source link

Got error when trying to run New-HyperVCloudImageVM.ps1 #15

Closed sysxtreme closed 1 month ago

sysxtreme commented 3 months ago

1

Command:

.\New-HyperVCloudImageVM.ps1 -VMProcessorCount 2 -VMMemoryStartupBytes 2GB -VHDSizeBytes 60GB -VMName "ubuntu-1" -ImageVersion "20.04" -VMGeneration 2 -ShowSerialConsoleWindow

Output:

New-HyperVCloudImageVM.ps1: Cannot process argument transformation on parameter 'Encoding'. 'Byte' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. (Parameter 'name')

2

So I tried to change the Byte enconding to UTF-8 and got new error:

Command:

.\New-HyperVCloudImageVM.ps1 -VMProcessorCount 2 -VMMemoryStartupBytes 2GB -VHDSizeBytes 60GB -VMName "ubuntu-1" -ImageVersion "20.04" -VMGeneration 2 -ShowSerialConsoleWindow

Output:

100% completedata iso for VM provisioning - 0% complete
 Done.
New-HyperVCloudImageVM.ps1: You cannot call a method on a null-valued expression.

I've tried different SO versions, same error.

sysxtreme commented 3 months ago

I ran it in verbose mode, and put some new Write-Verbose lines to find where the code is throwing error. I got this:

manage_etc_hosts: true
manage_resolv_conf: true

resolv_conf:
  # cloudflare dns, src: https://1.1.1.1/dns/  nameservers: ['1.1.1.1', '1.0.0.1']
  searchdomains:
    - domain.local
  domain: domain.local

power_state:
  mode: reboot
  message: Provisioning finished, will reboot ...
  timeout: 15
VERBOSE:
Creating metadata iso for VM provisioning - VERBOSE: Filename: C:\ProgramData\Microsoft\Windows\Virtual Hard Disks\ubuntu-1-metadata.iso
100% complete
VERBOSE: Metadata iso written
 Done.
VERBOSE: Creating cache folder
VERBOSE: Get timestamp
VERBOSE: Requested HTTP/1.1 GET with 0-byte payload
VERBOSE: Received HTTP/1.1 response of content type text/plain of unknown size
New-HyperVCloudImageVM.ps1: You cannot call a method on a null-valued expression.

Also, I have changed this lines:

Set-Content "$($tempPath)\Bits\meta-data" ([byte[]][char[]] "$metadata") -Encoding Bytes
...
Set-Content "$($tempPath)\Bits\network-config" ([byte[]][char[]] "$networkconfig") -Encoding Bytes
...
Set-Content "$($tempPath)\Bits\user-data" ([byte[]][char[]] "$userdata") -Encoding Bytes

to this:

Set-Content "$($tempPath)\Bits\meta-data" ([byte[]][char[]] "$metadata") -Encoding "UTF-8"
...
Set-Content "$($tempPath)\Bits\network-config" ([byte[]][char[]] "$networkconfig") -Encoding "UTF-8"
...
Set-Content "$($tempPath)\Bits\user-data" ([byte[]][char[]] "$userdata") -Encoding "UTF-8"
sysxtreme commented 3 months ago

It seems to be an issue related to the PowerShell version. I was using PowerShell 7.4.4 and got the error, but running the script on PowerShell version 5.1, system's default, it works fine.

samstagern commented 2 months ago

I will add a check for the powershell version if my other pull request gets accepted. This would at least give a better error message.