VMware Cloud Community
hum_mike
Contributor
Contributor
Jump to solution

Problems getting Variables out of Functions

If possible, could someone explain to me how function produce variables. Here is what I'm trying to do. I have a CSV file that I am importing. I then want to run function for each line in the CSV and produce variabled to be used in the script.

For Example,

I have a function that looks to see what windows os version and will assign a license key based on that variable.

Function Determine_Win_License_Key {

#$erroractionpreference = "SilentlyContinue"

$CSV_Guest_OS = $_.Guest_OS

switch ($CSV_Guest_OS)

{

2003x32 {$GuestOSLicense = "License Key 1"; break}

2003x64 {$GuestOSLicense = "License Key 2"; break}

2008x64 {$GuestOSLicense = "License Key 3"; break}

2008r2x64 {$GuestOSLicense = "License Key 4"; break}

}

}

Based on this function, I would like to set the variable of $GuestOSLicense to use later.

The script body looks something like this:

Import-Csv csv.csv | %{

Write-host "Determining License Key"

Determine_Win_License_Key

Write-host "The License Key being used is:" $GuestOSLicense

}

I would expect to see what the license key is but all I get on the output is

"The license key being used is: "

Does anybody know or understand why the function is running but not saving the variable generated from the function?

I have several other functions as part of this script, and all of them do not return the variable.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That's because you don't return anything in the function.

One way of doing this is

Function Determine_Win_License_Key {
   param($OS)
   #$erroractionpreference = "SilentlyContinue"
   switch ($OS)
   {
      2003x32 {$GuestOSLicense = "License Key 1"; break}
      2003x64 {$GuestOSLicense = "License Key 2"; break}
      2008x64 {$GuestOSLicense = "License Key 3"; break}
      2008r2x64 {$GuestOSLicense = "License Key 4"; break}
   }
   $GuestOSLicense
}

Import-Csv csv.csv | %{
   Write-host "Determining License Key"
   Determine_Win_License_Key -OS $_.Guest_OS
   Write-host "The License Key being used is:" $GuestOSLicense
}

It's also better to pass a parameter to a function, instead of relying on variables, in the function, that are defined/initialised outside the function.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

That's because you don't return anything in the function.

One way of doing this is

Function Determine_Win_License_Key {
   param($OS)
   #$erroractionpreference = "SilentlyContinue"
   switch ($OS)
   {
      2003x32 {$GuestOSLicense = "License Key 1"; break}
      2003x64 {$GuestOSLicense = "License Key 2"; break}
      2008x64 {$GuestOSLicense = "License Key 3"; break}
      2008r2x64 {$GuestOSLicense = "License Key 4"; break}
   }
   $GuestOSLicense
}

Import-Csv csv.csv | %{
   Write-host "Determining License Key"
   Determine_Win_License_Key -OS $_.Guest_OS
   Write-host "The License Key being used is:" $GuestOSLicense
}

It's also better to pass a parameter to a function, instead of relying on variables, in the function, that are defined/initialised outside the function.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
avlieshout
VMware Employee
VMware Employee
Jump to solution

By default a variable is defined with a local scope. This means that a variable that is defined within a function is only available inside the function.

As soon as you exit the function the variable is unknown.

Although the variable in the main section has the same label it is infact a different variable. And since it isn't initialized anywhere it will return $null

The quick and dirty way is to initialize the variable with a global scope: $global:GuestOSLicense.

This way the variable is also known outside the function.

But as Luc already mentioned. It is better to let a function return a value.

So your function should end with just the variable

$GuestOSLicense
}

Like in Luc's example.

When you call the function. make sure that you capture the function output value in a variable if you'll need it later.

$GuestOSLicenseValue = Determine_Win_License_Key

This is something that is missing from Luc's example.

Use the main section like this

Import-Csv csv.csv | %{
   Write-host "Determining License Key"
   $GuestOSLicense = Determine_Win_License_Key -OS $_.Guest_OS
   Write-host "The License Key being used is:" $GuestOSLicense
}

or even like this

Import-Csv csv.csv | %{
   Write-host "Determining License Key"
   Write-host "The License Key being used is: $(Determine_Win_License_Key -OS $_.Guest_OS)"

-


Arnim van Lieshout

Blogging: http://www.van-lieshout.com

Twitter: http://www.twitter.com/avlieshout

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
hum_mike
Contributor
Contributor
Jump to solution

Thank you to the both of you for the help. My scripts are now working.

0 Kudos