Question Impossible de convertir la valeur en System.Int32

Plus d'informations
il y a 10 ans 2 mois #21239 par serval13
Bonjour ,


j'ai un message erreur du type


Impossible de convertir la valeur « 1 562 » en type « System.Int32 ». Erreur : « Input string was not in a correct format. »


est ce que quuelqu'un aurait une idée ?



voici mon code

[code:1]
$vm = @($cluster|Get-VM)
$objCluster.ClusterName = $cluster.Name
$objCluster.NumHost = `
$cluster.ExtensionData.Summary.NumHosts
$objCluster.NumVM = $vm.Count
$objCluster.NumDatastore = `
$cluster.ExtensionData.Datastore.Count
$objCluster.NumNetwork = `
$cluster.ExtensionData.Network.Count
$objCluster.AssignedCpu = ($vm | Where {
$_.PowerState -eq \"PoweredOn\"} |
Measure-Object -Property NumCpu -Sum).Sum
$objCluster.NumCores = `
$cluster.ExtensionData.Summary.NumCpuCores
$objCluster.vCpuPerCore = “{0 :n0}” -f
($objCluster.AssignedCpu / $objCluster.NumCores)
$objCluster.TotalCpuGhz = “{0 :n0}” -f
($cluster.ExtensionData.Summary.TotalCpu / 1000)
$objCluster.TotalMemGB = “{0:n0}” -f
($cluster.ExtensionData.Summary.TotalMemory / 1GB )
$objCluster.AssignedMemGB = “{0 :n0}” -f (( $vm|
Where {$_.PowerState -eq “PoweredOn”} |
Measure-Object -Property MemoryMB -Sum ).Sum /1024)
$objCluster.MemUsagePct = \"(0 : p2)\" -f
($objCluster.AssignedMemGB / $objCluster.TotalMemGB )
$objCluster
}
}[/code:1]<br><br>Message édité par: Arnaud, à: 16/03/16 23:33

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 10 ans 4 jours #21594 par Arnaud Petitjean
Bonjour,

Cette erreur se produit tout simplement car tu as un blanc dans ta chaine de caractères. Du coup PowerShell ne sait pas comment réaliser la conversion.

Voilà ce que je te propose :
[code:1]
PS &gt; [int]'1 230'
Cannot convert value \&quot;1 230\&quot; to type \&quot;System.Int32\&quot;. Error: \&quot;Input string was not in a correct format.\&quot;
At line:1 char:1
+ [int]('1 230') -replace ' ',''
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:«») [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger


PS &gt; [int]('1 230'.replace(' ',''))
1230
[/code:1]<br><br>Message édité par: Arnaud, à: 16/03/16 23:32

MVP PowerShell et créateur de ce magnifique forum :-)
Auteur de 6 livres PowerShell aux éditions ENI
Fondateur de la société Start-Scripting
Besoin d'une formation PowerShell ?

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.045 secondes
Propulsé par Kunena