Answered by:
Powershell Uninstall/Install Operation Trough Domain

Question
-
Hello Guys,
i would need some help, with the operation that i've to do these days. I would like to uninstall a program on various computer inside a domain and install a different version. I though about to use pwshell to to this kind of operation
[CmdletBinding()]
param (
[Parameter(Mandatory = $True,
ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True)]
[string]$ProductName,
[Parameter(Mandatory = $False,
ValueFromPipeline = $False,
ValueFromPipelineByPropertyName = $False)]
[string]$MsiName
)
begin {
$WorkingDir = $MyInvocation.MyCommand.Path | Split-Path -Parent
$SccmClientWmiNamespace = 'root\cimv2\sms'
if ($MsiName) {
$PackageMsiFilePath = "$WorkingDir\$MsiName"
}
}
process {
try {
$Query = "SELECT * FROM SMS_InstalledSoftware WHERE ARPDisplayName LIKE '%$ProductName%'"
Write-Verbose "Querying computer for the existence of $ProductName..."
$InstalledSoftware = Get-WmiObject -Namespace $SccmClientWmiNamespace -Query $Query
if ($InstalledSoftware) {
$InstalledSoftware | foreach {
Write-Verbose "$ProductName ($($_.SoftwareCode)) found installed..."
Write-Verbose "Ensuring there's a valid uninstall string found..."
if ($_.UninstallString.Trim() -match 'msiexec.exe /x{(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}}') {
Write-Verbose "Valid uninstall string found for $ProductName..."
Write-Verbose "Ensuring a locally cached copy of the $ProductName MSI exists..."
if (Test-Path $_.LocalPackage) {
Write-Verbose "Install package $($_.LocalPackage) exists on the file system. Using locally cached copy for uninstall..."
$UninstallSyntax = "$($_.UninstallString) /qn"
} else {
Write-Verbose "Install package $($_.LocalPackage) not found on file system..."
if (!$PackageMsiFilePath) {
Write-Verbose "No MSIName specified as param. Searching current directory for MSI..."
$LocalMsis = Get-ChildItem $WorkingDir -Filter '*.msi' | Select -first 1
if (!$LocalMsis) {
throw 'No MSIs found to support uninstall'
} else {
$PackageMsiFilePath = $LocalMsis.FullName
}
} elseif (!(Test-Path $PackageMsiFilePath)) {
throw 'Package MSI needed but MSI specified does not exist in current directory'
} else {
##TODO: Check MSI's product name and version to ensure match
#Write-Verbose "Checking $PackageMsiFilePath for matching product name and version..."
#$MsiProperties = Get-Msi $PackageMsiFilePath
#if (($MsiProperties['ProductName'] -notlike '*$ProductName*') -or ($MsiProperties['ProductVersion'] -ne $_.ProductVersion)) {
# throw 'Package MSI is not the same product as being uninstalled'
#}
}
$UninstallSyntax = "/x `"$PackageMsiFilePath`" /qn"
}
$MsiExecArgs = $UninstallSyntax.ToLower().TrimStart('msiexec.exe ')
Write-Verbose "Beginning uninstall using syntax: msiexec.exe $MsiExecArgs"
Start-Process msiexec.exe -ArgumentList $MsiExecArgs -Wait -NoNewWindow
} else {
Write-Warning "Invalid uninstall string found for $ProductName..."
}
}
} else {
Write-Warning "$ProductName seems to already be uninstalled"
}
} catch {
Write-Error $_.Exception.Message
}
}
end {
}The Remove.ps1 works perfectly, but i tried with console change some isntruction in order to get this done
$pipe = 'flr-xxxxx-lx', 'flr-yyyyyy-lx'
$pipe | foreach {.\Remove-Software.ps1 - ProductName 'Adobe Reader'}
However i Get a General Error .
C:\psscripts\Remove-Software.ps1 :
At line:1 char:17
+ $pipe | foreach{.\Remove-Software.ps1 -ProductName 'edoc Capture'}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Remove-Software.ps1
C:\psscripts\Remove-Software.ps1 :
At line:1 char:17
+ $pipe | foreach{.\Remove-Software.ps1 -ProductName 'edoc Capture'}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Remove-Software.ps1Well, do you have any idea?
Second, no i can't use GPO in case will be your first though.
Thanks!
Francesco - Timer IT support
Thursday, January 11, 2018 9:44 AM
Answers
-
Hi Francesco,
Would you want to pass $pipe to $MsiName? If yes, you may need to set the ValueFromPipeline and ValueFromPipelineByPropertyName to $true for $MsiName parameter.
If you need further help, please feel free to let us know.
Best Regards,
AlbertPlease remember to mark the replies as an answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com- Proposed as answer by Albert LingMicrosoft contingent staff Monday, January 29, 2018 10:11 AM
- Marked as answer by Francesco Cao Friday, July 12, 2019 9:11 PM
Friday, January 12, 2018 5:49 AM
All replies
-
Hello Francesco,
Could you please us the "Insert Code Block" functionality to post your code?
Best,
TobyThursday, January 11, 2018 11:11 AM -
take a look and try also
Get-Package *YourSoftware* | uninstall-package
https://docs.microsoft.com/en-us/powershell/module/packagemanagement/uninstall-package?view=powershell-5.1
Chris
Thursday, January 11, 2018 5:55 PM -
Hi Francesco,
Would you want to pass $pipe to $MsiName? If yes, you may need to set the ValueFromPipeline and ValueFromPipelineByPropertyName to $true for $MsiName parameter.
If you need further help, please feel free to let us know.
Best Regards,
AlbertPlease remember to mark the replies as an answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com- Proposed as answer by Albert LingMicrosoft contingent staff Monday, January 29, 2018 10:11 AM
- Marked as answer by Francesco Cao Friday, July 12, 2019 9:11 PM
Friday, January 12, 2018 5:49 AM -
Hi,
Just want to confirm the current situations. Have you tried the method provided before?
If you already tried them or the issue remains after trying them, please don’t hesitate to tell me. I will do more research and try my best to give you helpful suggestions.
Best Regards,
AlbertPlease remember to mark the replies as an answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.comTuesday, January 16, 2018 2:30 AM -
Hi,
I am checking how the issue is going, if you still have any questions, please feel free to contact us.
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Appreciate for your feedback.
Best Regards,
AlbertPlease remember to mark the replies as an answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.comThursday, January 18, 2018 6:08 AM