Home > database >  "System.Int64". Error: "Input string was not in a correct format
"System.Int64". Error: "Input string was not in a correct format

Time:01-27

So I use a xml file to store configuration for creating virtual machines and my powershell creates the VM but I get this error and tried to fix it with [int64] before my variable but no luck. I can read the data from the xml file but it sees it as a string and I need it to be an int64.

In my xml file I put: 4096MB

In my powershell script: new-vm -ComputerName $Using:Hostname -name $Using:VMName -MemoryStartupBytes [int64]$Using:MemoryStartup

Error: Cannot bind parameter 'MemoryStartupBytes'. Cannot convert value "[int64]4096MB" to type "System.Int64". Error: "Input string was not in a correct format." CategoryInfo : InvalidArgument: (:) [New-VM], ParentContainsErrorRecordException FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.HyperV.PowerShell.Commands.NewVM

CodePudding user response:

I can't seem to find any explicit way to perform this type of casting in Powershell, as it appears to use native implicit functions to convert memory values to integers. However, an implicit cast by attempting a numeric operation seems to do the trick:

"512MB"/1
536870912

Therefore:

new-vm -ComputerName $Using:Hostname -name $Using:VMName -MemoryStartupBytes [int64]$($MemoryStartup/1)

There are lots more details relevant to your question in How to convert to UInt64 from a string in Powershell? String-to-number conversion. Do hop over there to give the superior source an upvote.

CodePudding user response:

Found a solution:

XML file: 4GB

Powershell file: new-vm -ComputerName $Hostname -name $VMName -MemoryStartupBytes (Invoke-Expression $MemoryStartup) -Generation 2 -Path $LocationPath

By using the Invoke-Expression it works like a charm

  •  Tags:  
  • Related