Powershell variable - string

I have a Powershell function that successfully uninstalls VMware tools remotely.  If I try to pass in a value, the function stops working.  I believe this has something to do with perhaps the string literal not being passed in properly.

Here's the function and how I'm calling it.  If I change $Applications = @("VMware Tools") to something like $Applications = @($Application) and pass in $application, it doesn't work.  Is this related to the double quotes?  It is searching the list of installed apps for VMware Tools, and runs the uninstaller.

function Uninstall-VMApplication
{
    Param (        
		[Parameter(Position=0, Mandatory=$true)]
        $VM,

        [Parameter(Position=1, Mandatory=$true)]
        $Applications,

        [Parameter(Position=2, Mandatory=$true)]
        $Credential
    )
    Invoke-Command -ComputerName $VM.ComputerName -Credential $Credential -ScriptBlock {
        $package = @("VMware Tools")
            foreach($package in $Applications){
            $app = Get-WmiObject -Class Win32_Product | Where-Object {
            $_.Name -match "$Application"
            }
         $app.Uninstall()
        }
        Restart-Computer -ComputerName $VM -Force
    }
}

Uninstall-VMApplication -VM $VM -Applications $Applications -Credential $Credential

Open in new window


If I replace:

$package = @("VMware Tools")
with
$package = @($Applications)

The function fails because it cannot find "VMware Tools" installed.
JWeb AdminAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

becraigCommented:
Why are you not just assigning the string value ?

$Applications = "VMware Tools"

instead of an array

Are you planning to have multiple values for the $Applications variable ?
0
JWeb AdminAuthor Commented:
Because I want to re-use the function for other projects, and I don't want to limit myself to just VMware Tools - I want to pass in a value or multiple values for $Applications.
0
becraigCommented:
I think you had some things mixed up, here is what I think you want to do

1) Create an array $Applications = @("app1", "app2", "app3")
2) loop thru this (foreach $package in $applications) {}
3) Run an uninstall of the app that matches package

function Uninstall-VMApplication
{
    Param (        
		[Parameter(Position=0, Mandatory=$true)]
        $VM,

        [Parameter(Position=1, Mandatory=$true)]
        $Applications,

        [Parameter(Position=2, Mandatory=$true)]
        $Credential
    )
    Invoke-Command -ComputerName $VM.ComputerName -Credential $Credential -ScriptBlock {
       $Applications = @("VMware Tools")
            foreach($package in $Applications){
            $app = Get-WmiObject -Class Win32_Product | Where-Object {
            $_.Name -match $package
            }
         $app.Uninstall()
        }
        Restart-Computer -ComputerName $VM -Force
    }
}

Uninstall-VMApplication -VM $VM -Applications $Applications -Credential $Credential

Open in new window

0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

JWeb AdminAuthor Commented:
I'm setting up $Applications this way:

$Applications = "VMware Tools"

and then calling the function below that:

    Uninstall-VMApplication -VM $VM -Applications $Applications -Credential $Credential


I believe I am not creating the array correctly as you noted - so I should do this instead?

$Applications = @("app1")
Uninstall-VMApplication -VM $VM -Applications $Applications -Credential $Credential
0
becraigCommented:
yup
you define the array then call the array as with the function then do the loop and then you do the foreach loop on the array.

One big problem also you need to pass the variable as an argument on the invoke-command or it will not know what the variable value is

e.g.   invoke-command { } -Argumentlist $Applications

That will unblock you.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JWeb AdminAuthor Commented:
Great - thank you!!!
0
footechCommented:
When using Invoke-Command, if the scriptblock needs to use variables that are defined outside of the scriptblock (i.e. in the parent script, then you need to use the -ArgumentList parameter and modify your scriptblock to accept those arguments.  Here I'm using the same variable name inside and outside the scriptblock, but they don't have be the same.  The Params are matched based on position, not name, so if you're passing multiple arguments, the order is important.
Invoke-Command -ComputerName $VM.ComputerName -Credential $Credential -ScriptBlock {
       Param ($Applications)
            foreach($package in $Applications){
            $app = Get-WmiObject -Class Win32_Product | Where-Object {
            $_.Name -match $package
            }
         $app.Uninstall()
        }
        Restart-Computer -ComputerName $VM -Force
    } -ArgumentList $Applications

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.