[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Powershell variable - string

Posted on 2014-08-28
7
Medium Priority
?
220 Views
Last Modified: 2014-08-28
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.
0
Comment
Question by:JWeb Admin
  • 3
  • 3
7 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40291600
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
 

Author Comment

by:JWeb Admin
ID: 40291602
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
 
LVL 29

Expert Comment

by:becraig
ID: 40291613
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
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 

Author Comment

by:JWeb Admin
ID: 40291618
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
 
LVL 29

Accepted Solution

by:
becraig earned 2000 total points
ID: 40291623
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
 

Author Comment

by:JWeb Admin
ID: 40291637
Great - thank you!!!
0
 
LVL 41

Expert Comment

by:footech
ID: 40291639
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question