$xml = [xml] '<node>foo</node>'
function foo2 { return "foo2" }
# all of these fail with the message:
# **Cannot set "foo" because only strings can be used as values to set XmlNode properties.**
$xml.node = foo2
$xml.node = foo2 -as [string] # because of this issue[1]
$xml.node = (foo2)
# these work
$xml.node = (foo2).tostring()
$xml.node = (foo2) -as [string]
$xml.node = [string] (foo2)
# yet, these two statements return the same value
(foo2).gettype()
(foo2).tostring().gettype()
1: PowerShell functions return behaviorPourquoi le mot-clé "retour" de Powershell provoque-t-il des erreurs de type?
Fonctionne au moins pour moi. Aucun message d'erreur ici, ni dans Powershell 1.0 sur Vista, ni dans Powershell 2.0 dans Windows 7 RC. – Joey
Fonctionne pour moi aussi. Powershell 1 et 2 sur XP. – zdan
Je suis tombé sur le script où j'avais mis cette solution de contournement, et j'ai saisi l'occasion pour comprendre ce qui a causé l'erreur. C'est la déclaration "retour". –