Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Help passing variables with GetNewClosure

$
0
0

I'm having trouble passing a variable to the remote workstation in a script. I would like the script to uninstall any program you enter for $program. This is what does work:

$computers = Get-Content 'C:\computerlist.txt'
foreach ($computer in $computers) {
Invoke-Command -ComputerName $computer -ScriptBlock { $app = Get-WmiObject -Class Win32_Product | Where-Object { 
    $_.Name -match "Java" 
}
$app.Uninstall()
}}


Now when I try to add in the $variable, I get errors:

$computers = Get-Content 'C:\computerlist.txt' $program = 'java' foreach ($computer in $computers) { Invoke-Command -ComputerName $computer -ScriptBlock {$app = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -match "$program" } $app.Uninstall() }}


I get the error:

Method invocation failed because [System.Object[]] doesn't contain a method named 'Uninstall'.+ CategoryInfo          : InvalidOperation: (Uninstall:String) [], RuntimeException+ FullyQualifiedErrorId : MethodNotFound+ PSComputerName        : computername

I know that the $program is not being passed to the remote computer and I need to use GetNewClosure, but I am having trouble getting this to work.





Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>