Why Is My Compare-Object Command Not Working Inside a Function?

0
0
Asked By TechieTurtle94 On

I'm having trouble with the Compare-Object command within a function in my PowerShell script. When I run the line that sets $deletevars outside of the function, it works perfectly fine. However, inside the function, it returns nothing as if Compare-Object can't operate there. Here's a snippet of my function:

```powershell
Function clean-vars {
write-host "Sysvars = $sysvars"
$sessionvars = (get-variable).name
write-host "Sessionvars = $sessionvars"
$deletevars = compare-object -ReferenceObject $sessionvars -DifferenceObject $sysvars
write-host "Deletevars = $deletevars"
foreach ($var in $deletevars) {
if ($var.SideIndicator -eq "<=") {
Remove-Variable -Name $var.InputObject -ErrorAction SilentlyContinue
}
}
}
```

For this to work, $sysvars has to be set before running the script. I'm wondering why the Compare-Object command is not functioning as expected within the function's scope?

0 Answers

There is no answer to this question yet. If you know the answer or can offer some help, please use the form below.

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.