SOLVED

Function in a function not running

%3CLINGO-SUB%20id%3D%22lingo-sub-1236144%22%20slang%3D%22en-US%22%3EFunction%20in%20a%20function%20not%20running%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1236144%22%20slang%3D%22en-US%22%3E%3CP%3EHI%2C%20I%20am%20still%20experimenting%20with%20function%20and%20I%20have%20a%20question%20regarding%20a%20strange%20behavior.%3C%2FP%3E%3CP%3ELet's%20say%20that%20I%20have%20the%20following%20code%3A%3C%2FP%3E%3CP%3Efunction%20GetDatastore%3C%2FP%3E%3CP%3E%7B%3C%2FP%3E%3CP%3E%24Datastore%20%3D%20Get-Datastore%3C%2FP%3E%3CP%3E%24DatastoreName%20%3D%20%24Datastore.name%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Efunction%20ClearVariable%3C%2FP%3E%3CP%3E%7B%3C%2FP%3E%3CP%3EClear-Variable%20Datastorename%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Efunction%20RunAllFunction%3C%2FP%3E%3CP%3E%7B%3C%2FP%3E%3CP%3EGetDatastore%3C%2FP%3E%3CP%3E%24Test1%20%3D%20%22sometext%22%3C%2FP%3E%3CP%3E%24test2%20%3D%20%22sometext%22%3C%2FP%3E%3CP%3E%24test3%20%3D%26nbsp%3B%26nbsp%3B%24DatastoreName%3C%2FP%3E%3CP%3E%24Output%20%3D%20Write-Output%20-InputObject%20(%24test1%2C%24test2%2C%24test3)%3C%2FP%3E%3CP%3E%24Output%20%7C%20Out-File%20C%3A%5CTemp%5CFile.txt%3C%2FP%3E%3CP%3EClearVariable%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20after%20running%20the%20script%2C%20there%20is%20no%20trace%20of%20the%20output%20of%20%24test3%20in%20the%20file.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20also%20tried%20copying%20the%20script%20in%20another%20Powershell%20ISE%20and%20running%20the%20function%20GetDatastore%20manually%20(selecting%20the%20all%20lines%20related%20to%20the%20function)%20and%20then%20running%20the%20function%20and%20there%20was%20no%20result.%20But%2C%20when%20selecting%20only%20only%20the%20variable%20lines%20(%24Datastore%20%3D%20Get-Datastore%20and%26nbsp%3B%24DatastoreName%20%3D%20%24Datastore.name)%2C%20the%20result%20was%20there.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20idea%2C%20sorry%20if%20I%20am%20not%20clear%20enough!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMathieu%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1236144%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1236500%22%20slang%3D%22en-US%22%3ERe%3A%20Function%20in%20a%20function%20not%20running%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1236500%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F17422%22%20target%3D%22_blank%22%3E%40Mathieu%20Desjardins%3C%2FA%3E%26nbsp%3BHello%2C%20I%20would%20think%20about%20these%202%20options.%26nbsp%3B%3CBR%20%2F%3ELet%20me%20know%20if%20you%20have%20further%20questions.%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%23Option%201%3A%20Using%20a%20global%20scope%3C%2FP%3E%3CP%3E%24global%3ADatastoreName%20%3D%20%24null%26nbsp%3B%23%26nbsp%3Bdeclared%26nbsp%3Bat%26nbsp%3Btop%26nbsp%3Band%26nbsp%3Bused%26nbsp%3Banywhere%26nbsp%3Bin%26nbsp%3Byour%26nbsp%3Bscript%3C%2FP%3E%3CP%3E%3CBR%20%2F%3Efunction%20GetDatastore%3C%2FP%3E%3CP%3E%7B%3C%2FP%3E%3CP%3E%24Datastore%20%3D%20Get-Datastore%3C%2FP%3E%3CP%3E%24global%3ADatastoreName%20%3D%20%24Datastore.name%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOption%202%3A%20return%20the%20value%20of%20the%20desired%20variable%20outside%20the%20function%20to%20use%20it%20as%20needed%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Efunction%20GetDatastore%3C%2FP%3E%3CP%3E%7B%3C%2FP%3E%3CP%3E%24Datastore%20%3D%20Get-Datastore%3C%2FP%3E%3CP%3E%24DatastoreName%20%3D%20%24Datastore.name%3C%2FP%3E%3CP%3Ereturn%20%24DatastoreName%20%23required%20data%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Efunction%20RunAllFunction%3C%2FP%3E%3CP%3E%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24Test1%20%3D%20%22sometext%22%3C%2FP%3E%3CP%3E%24test2%20%3D%20%22sometext%22%3C%2FP%3E%3CP%3E%24test3%20%3D%26nbsp%3B%26nbsp%3BGetDatastore%20%23So%20now%20the%20output%20will%20be%20saved%20to%20%24test3%20variable%20and%20your%20outoutfile%20should%20be%20good.%26nbsp%3B%3C%2FP%3E%3CP%3E%24Output%20%3D%20Write-Output%20-InputObject%20(%24test1%2C%24test2%2C%24test3)%3C%2FP%3E%3CP%3E%24Output%20%7C%20Out-File%20C%3A%5CTemp%5CFile.txt%3C%2FP%3E%3CP%3EClearVariable%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3EErick%20Moreno%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1238511%22%20slang%3D%22en-US%22%3ERe%3A%20Function%20in%20a%20function%20not%20running%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1238511%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F89674%22%20target%3D%22_blank%22%3E%40Erick%20A.%20Moreno%20R.%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20Global%20variable%20worked%20like%20a%20charm!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20this%20answer%20trigger%20another%20question%20for%20my%20understanding%3A%3C%2FP%3E%3CP%3EHow%20is%20a%20normal%20variable%20is%20not%20working%20vs%20a%20global%20one%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1238739%22%20slang%3D%22en-US%22%3ERe%3A%20Function%20in%20a%20function%20not%20running%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1238739%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F17422%22%20target%3D%22_blank%22%3E%40Mathieu%20Desjardins%3C%2FA%3E%26nbsp%3Bcause%20the%20%22normal%20one%22%20is%20inside%20a%20function%20so%20each%20time%20you%20run%20a%20function%20everything%20inside%20will%20be%20destroyed%20after%20the%20run%20unless%20it%20is%20explicitly%20saved.%26nbsp%3B%3CBR%20%2F%3EYou%20can%20test%20this%20initializing%20a%20var%20at%20the%20beginning%20on%20the%20script%20and%20then%20using%20the%20same%20var%20name%20inside%20the%20function%20and%20writing%20the%20value%20at%20the%20end%20of%20the%20script%20(outside%20the%20function)%20and%20see%20how%20the%20initialized%20value%20is%20the%20result%20no%20matter%20what%20you%20did%20inside%20the%20function%20so%20everything%20inside%20a%20function%20is%20like%20a%20different%20workspace.%20I%20hope%20that%20explains%20why%20you%20are%20getting%20that%20behavior.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3EErick%20Moreno%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

HI, I am still experimenting with function and I have a question regarding a strange behavior.

Let's say that I have the following code:

function GetDatastore

{

$Datastore = Get-Datastore

$DatastoreName = $Datastore.name

}

 

function ClearVariable

{

Clear-Variable Datastorename

}

 

function RunAllFunction

{

GetDatastore

$Test1 = "sometext"

$test2 = "sometext"

$test3 =  $DatastoreName

$Output = Write-Output -InputObject ($test1,$test2,$test3)

$Output | Out-File C:\Temp\File.txt

ClearVariable

}

 

Then after running the script, there is no trace of the output of $test3 in the file.

 

I also tried copying the script in another Powershell ISE and running the function GetDatastore manually (selecting the all lines related to the function) and then running the function and there was no result. But, when selecting only only the variable lines ($Datastore = Get-Datastore and $DatastoreName = $Datastore.name), the result was there.

 

Any idea, sorry if I am not clear enough!

 

Mathieu

3 Replies
Highlighted
Best Response confirmed by Mathieu Desjardins (Occasional Contributor)
Solution

@Mathieu Desjardins Hello, I would think about these 2 options. 
Let me know if you have further questions. 


#Option 1: Using a global scope

$global:DatastoreName = $null # declared at top and used anywhere in your script


function GetDatastore

{

$Datastore = Get-Datastore

$global:DatastoreName = $Datastore.name

}

 

Option 2: return the value of the desired variable outside the function to use it as needed 

 

function GetDatastore

{

$Datastore = Get-Datastore

$DatastoreName = $Datastore.name

return $DatastoreName #required data

}

 

function RunAllFunction

{

 

$Test1 = "sometext"

$test2 = "sometext"

$test3 =  GetDatastore #So now the output will be saved to $test3 variable and your outoutfile should be good. 

$Output = Write-Output -InputObject ($test1,$test2,$test3)

$Output | Out-File C:\Temp\File.txt

ClearVariable

}

 

Regards

Erick Moreno

Highlighted

@Erick A. Moreno R. 

 

The Global variable worked like a charm!

 

But this answer trigger another question for my understanding:

How is a normal variable is not working vs a global one?

 

Thank you!

Highlighted

@Mathieu Desjardins cause the "normal one" is inside a function so each time you run a function everything inside will be destroyed after the run unless it is explicitly saved. 
You can test this initializing a var at the beginning on the script and then using the same var name inside the function and writing the value at the end of the script (outside the function) and see how the initialized value is the result no matter what you did inside the function so everything inside a function is like a different workspace. I hope that explains why you are getting that behavior.

 

Regards

Erick Moreno