![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Objects returned from functions?? Heya, I have one function that is modifying XMLElement and then returns this element to main function. But somehow (probably some behavior of PowerShell I am not aware of) is changing type of that object... Function (it's pretty big): Write-Host "In Function " $Container.GetType() Return $Container Main script: $Container = Load-Container($(Join-Path $Job.FullName $Container.Name)) Write-Host "After function " $Container.GetType() To me it makes complete sense, however it doesn't work as expected ![]() This is output: In Function System.Xml.XmlElement After function System.Object[] I read at Keith's blog about returning values: http://keithhill.spaces.live.com/blo...?wa=wsignin1.0 How are you using functions in that case???? As a workaround, I changed my code as follows: Main script: Load-Container($(Join-Path $Job.FullName $Container.Name)) Write-Host "After function " $Container.GetType() Function: Set-Variable -Scope 1 -Name Container -Value $Container #Return [xml.xmlelement]$Container This works fine, however I don't really like it. Any better idea how to handle such functions? I know that Write-Host doesn't modify return from function, but I will rather stick with my currect solution, which is less error prone. Martin |
| | #2 (permalink) | ||||||||||||
| Guest | Re: Objects returned from functions?? Hmmmm, I was wrong, it doesn't work: Set-Variable : Exception retrieving string: "Cannot find an overload for "XmlNode" and the argument count: "3"." At C:\Links\HDDs\DevHDD\WorkingProjects\S4Maintenance\Runtime\CoreFunctions.ps1:96 char:15 + Set-Variable <<<< -Scope 1 -Name Container -Value $Container -Force "Martin Zugec" <martin.zugec@xxxxxx> wrote in message news:eq9ynwc3IHA.4500@xxxxxx
| ||||||||||||
| | #3 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Objects returned from functions?? Appears to be bug: WORKS $Script:Container = $Container DOESN'T WORK Set-Variable -Scope 1 -Name Container -Value $Container -Force DOESN'T WORK Set-Variable -Scope Script -Name Container -Value $Container -Force It appears as bug in Set-Variable, or am I wrong?? Martin "Martin Zugec" <martin.zugec@xxxxxx> wrote in message news:%23F42t2c3IHA.1428@xxxxxx
| ||||||||||||||||||||||||
| | #4 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: Objects returned from functions?? Hi Martin, Please run your function and let the return values display on the console. Paste it here or check to see whether or not there is some unexpected object in the output. Powershell don't need to return object manually, it will do that by itself. But this behavior sometimes causes some bugs. Best regards, Tao Ma "Martin Zugec" <martin.zugec@xxxxxx> ??????:uRNN09c3IHA.2348@xxxxxx
| ||||||||||||||||||||||||||||||||||||
| | #5 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: Objects returned from functions?? Two examples: FUNCTION FAILED [CriticalError] Container LogoffUsers is missing container.xml file FUNCTION OK [Information] Container location set to C:\Links\HDDs\DevHDD\WorkingProjects\S4Maintenance\Examples\Jobs\OnDemandMainten ance\RebootServer [Information] Container name: Reboot server [Information] Container description: All servers that enters this container are automatically rebooted [Information] Is terminator: false [Information] Is static: false [Information] Objects timeout: 30 [Information] Default pipe after timeout: Failed [Information] Required maintenance version: 1 [Information] Inherited from object: Reboot server with version 1 [Information] Loading target containers [Information] Found 3 containers that RebootServer depends on All Information\CriticalError are displayed using Write-Host... As I said, I checked that object that is returned is correct type inside function (I do ..GetType() one line above Return $Container), however when returned it is immediately changed to System.Object Right now it works for me. Question is how to you handle complex function (when you cannot be sure that Return $Object will return $Object) and whether that problem I encountered is caused by bug in Set-Variable. Martin "Tao Ma" <feng_eden@xxxxxx> wrote in message news:uFqG7Rd3IHA.3348@xxxxxx
| ||||||||||||||||||||||||||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: Objects returned from functions?? Hmmmmmm, I have a strange feeling that "return" is in fact only "break function" and not return in terms of normal .NET programming Martin "Martin Zugec" <martin.zugec@xxxxxx> wrote in message news:%23lob5fd3IHA.5060@xxxxxx
| ||||||||||||||||||||||||||||||||||||
| | #7 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: Objects returned from functions?? Hi Martin, The following function will return a object array which contains 1,2,3: function () { 1 Write-Host '!' 2 Write-Host '!' return 3 } I think your function return more things than your expected. So I suspect that your function returns some unexpected object. I cannot reproduce Set-Variable bugs in PowerShell v1 and v2 ctp2. Tao Ma "Martin Zugec" <martin.zugec@xxxxxx> ???? news:%23lob5fd3IHA.5060@xxxxxx
| ||||||||||||||||||||||||||||||||||||
| | #8 (permalink) | ||||||||||||
| Guest | Re: Objects returned from functions?? This is true. There are often many places where things get added that you don't realise i.e most collections when you add something to them return an integer index myarraylist.add("something") \myarraylist.add("something") will actually return a 1 and 2 and in those cases you don't want this going into the pipeline so you have to explicitly consume the result i.e [void]myarraylist.add("something") Tao Ma wrote:
| ||||||||||||
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| my accents over e's has returned! | LauraR | Vista mail | 0 | 02-12-2008 07:35 PM |
| Returned email | Jarhed marine | Vista mail | 2 | 12-05-2007 05:06 PM |
| Creating objects with variable names in functions | Frank | PowerShell | 4 | 04-14-2007 06:26 PM |
| Bug with empty array returned from functions? | Keith Hill [MVP] | PowerShell | 4 | 01-03-2007 08:27 PM |