Remove AppV package using Administrator credentials - powershell

I am trying to create a script that removes a certain AppV package. The problem I am having is the AppV package is installed under the local user which does not have admin access so I get this message:
"Permission denied"
However, when I run the script as administrator and give my credentials, the AppV package does not show up. Here is the section of code I am using to list and remove the package.
# Remove AppV version of inProcess
$allAppV = Get-AppvClientPackage
If ($allAppV.Count -ge 0) {
$i = 1
#This part lists the packages
Write-Host "List AppV packages"
ForEach($Package in $allAppV) {
Write-Host `t $i - $Package.Name
$i +=
1
}
# Select which package to remove
$NumbertoRemove = Read-Host
"Which one would you like to remove? Type 0 if none"
If ($NumbertoRemove -eq 0) {
Write-Host "Not removing any App-V Client Package"
$Global:More = $False
}
else {
If ($NumbertoRemove -le $allAppV.Count) {
$NumbertoRemove -= 1
Write-Host "Removing package" $allAppV[$NumbertoRemove].Name
$PackageToRemove = $allAppV[$NumbertoRemove]
If ($PackageToRemove.IsPublishedGlobally)
{Unpublish-AppvClientPackage $allAppV[$NumbertoRemove]}
# I need to provide admin credentials for this step
Remove-AppvClientPackage $allAppV[$NumbertoRemove]
Write-Host "AppV Client Package has been removed"
This is what it looks like when I run the script as the local user. If I enter 1 it will try to remover inProcess but gets permission denied error.
If I run as administrator it looks the same except it lists no packages. I imagine because the script is running as administrator so it is listing packages installed under the admin account which their are none.
I need to either run the script as administrator but list the Appv packages of the local user or provide credentials for the remove-AppvClientPackages step. It would be preferable to prompt for credentials to remove the package. Thanks

Use the Get-AppvClientPackage cmdlet with the -All switch, this will list all AppV packages on the computer regardless of the current user. You will still need to run the cmdlet with administrative credentials to uninstall the package.
$allAppV = Get-AppvClientPackage -All

Related

Powershell Script to reinstall Microsoft teams, Not able to find teams install

Currently I am trying to write a power shell script that will uninstall then install Microsoft Teams.
I have never written a power shell script before and I am having trouble having the script get the initial teams installation so I can uninstall it.
This is what I have written so far, I saw two ways of finding the teams install online and neither is able to find it so I am kinda lost, any help would be much appreciated.
(I know both are commented out I just did it like this for formatting in this question.)
Write-Host "-------------------------------------`n"
# Prompt for credentials
$credential = Get-Credential
$username = $credential.Username
$password = $credential.GetNetworkCredential().Password
Write-Host "Finding teams`n"
# Find teams 1
#$teamsapp = Get-AppxPackage -Name Microsoft.Teams
# Find teams 2
#$teamsapp = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -eq "Microsoft Teams" }
# Check if installed
if ($teamsapp) {
Write-Host "Microsoft Teams is installed."
} else {
Write-Host "Microsoft Teams is not installed."
}
`
Teams is a bit tricky because it installs per user, not per computer. Assuming you're running the script under the user's account, you can check the following registry location using Get-ChildItem:
Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Teams
This code worked for me:
Get-ChildItem -Path 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' | Where-Object { $_.Name -like '*Teams' }
You should be able to use the "QuietUninstallString" property of the result to get the command needed to uninstall Teams.
As a side note, consider looking into the Teams Machine Wide Installer for deploying teams. It installs to the computer and runs at logon for each user to detect if Teams is installed to their AppData folder. If not, it installs it automatically. This lets you avoid having to run as the user or loop through all the users AppData folder to manipulate user apps.

SCCM HKCU Detection Issue

I am deploying a package install via SCCM however I have the detection method to run the following powershell script to detect a HKCU key for whoever is currently logged on. SCCM installs the app as 'SYSTEM' account and I have not chosen to 'install for user' as this prompts our users with a UAC dialog box which i am trying to avoid.
Even though it seems like the following code has worked, when i manually alter the registry key to something other than whats specified, Software Center still shows the app as 'installed' even though in theory it shouldnt as the key has been manually amended.
What am i doing wrong here?
$key = 'HKCU:\SOFTWARE\MCF\Kofax'
$valueName = 'Cert'
$valueData = '1.0'
if ((Get-ItemPropertyValue $key $valueName) -eq $valueData) {
"installed"
} else {
"NOT installed"
}
You can"t have an output if the software is NOT found. Every output in a detection via PowerShell counts as "Software detected".
Just delete "NOT Installed" from your script and it should work.

PowerShell script for software installation using chocolatey

enter code here
# Step 1) install Chocolatey when needed
if (-not (Test-Path -Path "$env:ProgramData\Chocolatey\choco.exe" -PathType Leaf)) {
# from https://chocolatey.org/install
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
}
# Step 2) define the array of packages you are offering
$Packages = 'googlechrome','firefox','codeblocks','windbg','nasm',
'explorersuite','pestudio','vscode','sysinternals','python'
# Step 3) define the Show-Menu function
function Show-Menu {
Clear-Host
Write-Host "**********************************************"
Write-Host "LIST OF SOFTWARES"
# write the options using the array of packages
for ($i = 0; $i -lt $Packages.Count; $i++) {
# {0,2} means right align with spaces to max 2 characters
Write-Host ('{0,2}. {1}' -f ($i + 1), $Packages[$i])
}
Write-Host " q. Exit the script"
Write-Host "*************************************************"
Write-Host
}
# Step 4) enter an endless loop you only exit if the user enters 'q'
while ($true) {
Show-Menu
$UserInput = Read-Host "Enter the software number to be installed"
# test if the user wants to quit and if so, break the loop
if ($UserInput -eq 'q') { break }
# test if the user entered a number between 1 and the total number of packages (inclusive)
if ([int]::TryParse($UserInput,[ref]$null) -and 1..$Packages.Count -contains [int]$UserInput) {
# here you install the chosen package using the array index number (= user input number minus 1)
$packageIndex = [int]$UserInput - 1
Write-Host "Installing $($Packages[$packageIndex])"
choco install $Packages[$packageIndex] -y
}
else {
$availableOptions = 1..$Packages.Count -join ','
Write-Host "Error in selection, choose $availableOptions or q" -ForegroundColor Red
}
$null = Read-Host "Press Enter to continue"
}
Problem Statement:
I am writing power shell script using chocolatey for software downloading and installing automatically. My script will display a menu with the list of software's whenever users enter the number the corresponding software will download and install.
When i run the script the executable files are not showing in control panel and desktop icon is also not creating but i am getting the message in power-shell terminal that the software is installed but i am not able to see that application.
I need to put some conditions in my script they are:
If the software is already installed then the software should not be downloaded
When the software is installed it has to shown in control panel and desktop icon should be created.
If any upgrade is their like for googlechrome and firefox generally up gradation takes place then the script has to upgrade.
Please help me and edit my script if anything is wrong
Thanks in advance
Referencing this answer, check to see if the relevant software is installed by checking the following registry keys:
$InstalledSoftware = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*
$InstalledSoftware += Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*
If it's installed, you can either skip the installation altogether, or...
You can get the package managed under Chocolatey by "installing" the package anyways, but provide the -n (--skippowershell) parameter:
choco install -n packageName
This will download the package metadata but skip the actual PowerShell code (chocolateyInstall.ps1, which is embedded into all Chocolatey packages). This is the piece which runs MSIEXEC, an EXE installer, extracts zip archives for packages without a proper installer, etc. This has the benefit of allowing future updates to be manageable by Chocolatey without reinstalling the software which already exists.
Note: There may be some nuance surrounding specific packages you'll have to figure out, but for the majority of what is available as a Chocolatey package this approach will work.

Trying to create process on remote PC from network share

I have this Powershell:
Try
{
#Start the installer remotely
$process = ([WMICLASS]"\\$comp\ROOT\CIMV2:Win32_Process").Create($InstallString)
if ( $process.ReturnValue -eq 0 )
{
$logstr = $comp + ": spawned process " + $process.ProcessId
Write-Host -ForegroundColor GREEN $logstr
}
else
{
Write-Host -ForegroundColor RED "${comp}: failed to create process ${InstallString}"
Continue
}
}
Catch
{
Write-Host -ForegroundColor RED "${comp}: error: $_.Exception.Message"
Continue
}
Where $comp is a valid PC name and InstallString is \\SERVERNAME\ShareFolder\setup.exe.
Within that ShareFolder is an installer and its files. I expect I can run the setup like this to remote install, but it is not working. It's falling into the else:
COMPUTERNAME: failed to create process \\SERVERNAME\ShareFolder\setup.exe
What am I missing? I can access this path outside Powershell.
This works on a local path. Do I need some special syntax in Powershell to point to this share?
This code works if the exe (well, a different installer) is located on COMPUTERNAME and the code is still executed remotely.
It could be a permission problem. According to this link: Creating Processes Remotely
A process created remotely can run under any account if the account has the Execute Method and Remote Enable permissions for root\cimv2. The Execute Method and Remote Enable permissions are set in WMI Control in the Control Panel.

Artifactory restore nuget packages with authentication from powershell

Our team started using Artifactory a few weeks ago and one of the challenges we are currently trying to work through is authentication. Currently we have every developer setup in Artifactory with an account login, we have a build.ps1 file that we use to restore our nuget packages but we're trying to automate this process.
Here is what our powershell script looks like. But it is not prompting the user to enter their username and password, unless they add it as parameters to the .\build.ps1 command line but the issue there is that the password is being entered in plain text.
"Updating NuGet Dependencies"
$nuget = "$rootDir\.nuget\NuGet.exe"
if ($nuser)
{
Invoke-Expression "$nuget Sources Update -Name 'Artifactory' -UserName $user -Password $pass"
}
Invoke-Expression "$nuget install -NonInteractive -DisableParallel"
if ($lastexitcode -ne 0) {
Write-Host -foreground red "Error occurred downloading dependencies."
exit 1
}
dir . | where { $_.psiscontainer } | where { (test-path (join-path $_.fullname "packages.config")) } | foreach {
Invoke-Expression "$nuget install '$($_.fullname)\packages.config' -NonInteractive -DisableParallel"
if ($lastexitcode -ne 0) {
Write-Host -foreground red "Error occurred downloading project dependencies."
exit 1
}
}
This article on handling passwords and credentials might prove useful to you:
Working with Passwords, Secure Strings and Credentials in Windows PowerShell
I had some problems with setting the credentials.
My solutions was to upgrade nuget.exe (v4.3.0 from v3.x.x) and using the Add Sources with the username/ password, with the ApiKey from the Artifactory as the password.