Setting Varaibles In A Function

Hello Powershell friends,

I am trying to set a bunch of varaibles, and I want these variables to be set at a specific time (as they set up log file paths which contain time and date stamps).

So here is my function:

Function Set-Variables {  

#----------------------------------------------------------
#STATIC VARIABLES
#----------------------------------------------------------
$log             = "c:\scripts\log\ADImportLog$(get-date -f dd-MM-yyyy-HH-mm-ss).log"
$failed          = "c:\scripts\log\FailedImportAccountsList$(get-date -f dd-MM-yyyy-HH-mm-ss).log" # This will be used to create a list of failed accounts, useful for making re-attempts
$movelog         = "c:\scripts\log\movelog$(get-date -f dd-MM-yyyy-HH-mm-ss).log"
$date            = Get-Date
$addn            = (Get-ADDomain).DistinguishedName
$dnsroot         = (Get-ADDomain).DNSRoot
$i               = 2
$tempOU          = "OU=TempImport,OU=Users,DC=corp,DC=,DC=co,DC=uk"  #Accounts are initially created here, change as necessary.  Once validated, they are moved to correct OU
$UPNSuffix       = ""
$MEU             = "c:\scripts\log\MEU$(get-date -f dd-MM-yyyy-HH-mm-ss).log" 
$MEUfail         = "c:\scripts\log\MEUfailed$(get-date -f dd-MM-yyyy-HH-mm-ss).log"
$MEUfail1        = "c:\scripts\log\MEUNotFound$(get-date -f dd-MM-yyyy-HH-mm-ss).log"
$movefail        = "c:\scripts\log\MoveFail$(get-date -f dd-MM-yyyy-HH-mm-ss).log"
$MailFRD         = "c:\scripts\log\EnforceLog_FRD_Details$(get-date -f dd-MM-yyyy-HH-mm-ss).log"
$365lic          = "c:\scripts\log\365License$(get-date -f dd-MM-yyyy-HH-mm-ss).log"
$365Enable       = "c:\scripts\log\365Enable$(get-date -f dd-MM-yyyy-HH-mm-ss).log"
$365EnableF      = "c:\scripts\log\365EnableFailed$(get-date -f dd-MM-yyyy-HH-mm-ss).log"
$AzureCSV        = "c:\scripts\log\AzureImport$(get-date -f dd-MM-yyyy-HH-mm-ss).log"
$AzureURI        = "" #Add in URI path to BLOB here
$AzureToken      = "" #Add in SAS token here
$AzureRootFolder = "/"
$AzureImport     = "c:\scripts\log\AzureImport$(get-date -f dd-MM-yyyy-HH-mm-ss).log"
$Rename          = "c:\scripts\log\Rename$(get-date -f dd-MM-yyyy-HH-mm-ss).log"
$ConvertShared   = "c:\scripts\log\ConvertShared$(get-date -f dd-MM-yyyy-HH-mm-ss).log"

}

I simply call this within other functions like so:

 Set-Variables 

However for some reason nothing happens, and no variables are set.

Something simple I expect if someone could kindly enlighten me

Many Thanks

You should read the help for scopes:

Get-Help about_Scopes
if you set the variables in a function they are by default only available inside this function.

Ah thanks, thought it might be related to that, will read up on that cheers

I would suggest that you’re going about it slightly wrong, from an approach perspective. Your function shouldn’t be setting variables - you’re going to end up setting script-scope or global-scope variables, which is messy and a poor practice.

Instead, your function should return an object, whose properties contain the values you need.

function Get-MyPaths {
 $props = @{FirstThingIWant = "Something\Something\$(Get-Date)"
            OtherThing = "Else\Else\$(Get-Date)"
          }
 New-Object -Type PSObject -Prop $props
}

“Get” would be the correct verb. Usage:

$data = Get-MyPaths
Do-Something -With $data.FirstThingIWant
Do-SomethingElse -Input $data.OtherThing

That’s a much more PowerShell-native approach.