How can I return values from a function to use later in my script?

0
11
Asked By CuriousCoder93 On

I'm working on a script to check the registry for an app's uninstall information and send the relevant details to a Teams channel. When my function verifies that the application is installed, I want to pass back some properties, like DisplayVersion, InstallDate, and PSPath, to use in the second part of the script. How can I achieve this? Here's my current code structure that checks for Google Chrome:

```PowerShell
$AppName = "Google Chrome"

function CheckApp {
# Logic to check registry paths
}

$Part2 = CheckApp
# Logic to use the returned values
```
I'm unsure how to pass multiple values from CheckApp to the next part of the script when it finds the app. Any suggestions?

4 Answers

Answered By TechWiz88 On

To return additional information from your function, modify it to return a PSCustomObject instead of just a boolean. That way, when you find the app, you can return all needed properties in one go. Here's an example of how to do that:

```PowerShell
function CheckApp {
# Define paths to check
foreach ($path in $paths) {
$items = Get-ItemProperty $path
foreach ($item in $items) {
if ($item.DisplayName -like "*$AppName*") {
return $item # Return the entire item object
}
}
}
return $false
}
```
This way, when you call CheckApp, you'll get the item's details directly into `$Part2` that you can use in your script!

Answered By PowershellPirate On

Instead of returning just true or false, you can have your function return the entire registry item when found. So when you call `CheckApp`, it will give you access to `DisplayVersion`, `InstallDate`, and `PSPath` all at once, like this:

```PowerShell
$Part2 = CheckApp
if ($Part2) {
# Now you can access $Part2.DisplayVersion, $Part2.InstallDate, etc.
}
```

Answered By DevGuru07 On

You could also separate the functionalities. Have `CheckApp` just check for the app and return the relevant details, and then create another function to handle the API call. This way, your code stays clean and easy to manage. Here’s a quick outline:

```PowerShell
function NotifyTeams {
# Logic to send the notification
}

function CheckApp {
$appDetails = @() # Array to store details
# ... Check logic
return $appDetails # Return the details
}
```
Then you can call `NotifyTeams` with the details after confirming the app is installed!

Answered By ScriptSavant On

You might be overcomplicating it a bit with functions. If you want a quick solution without needing a full function, you can just directly get properties from the items you check. Here's a streamlined version of your code for clarity:

```PowerShell
$items = Get-ItemProperty -Path
foreach ($item in $items) {
if ($item.DisplayName -like "*$AppName*") {
# At this point, you have access to all properties
Send-TeamsMessage -AppDetails $item
}
}
```

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.