Page 1 of 1

[SOLVED] How to run a PowerShell script?

Published: Dec 11, 2017 - 2:57 PM
by olaplanche
Hello,

I need to run a PowerShell script from a package (to remove unnecessary apps from Windows 10 after an upgrade).
I've found several forum posts about this, but they all handle it differently!

Is there a cleaner solution?

For your information:
- WAPT version installed: 1.3.13
- Server OS: Debian Jessie
- Administration/package creation machine OS: Windows 10

Thank you

Re: Running a PowerShell script?

Published: Dec 13, 2017 - 12:31
by sfonteneau
use the run_powershell function

For example :

Code: Select all

run_powershell('Get-AppxPackage -Name Microsoft.Windows.Cortana -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}')
It's the cleanest

Re: Running a PowerShell script?

Published: Dec 13, 2017 - 4:06 PM
by olaplanche
Hello and thank you for the reply.

I am currently testing with the following package:

Code: Select all

# -*- coding: utf-8 -*-
from setuphelpers import *

uninstallkey = []

def install():
    run_powershell('Get-AppXPackage -User Administrateur | where-object {$_.name –notlike “*store*”} | Remove-AppxPackage')
    run_powershell('Get-appxprovisionedpackage –online | where-object {$_.packagename –notlike “*store*”} | Remove-AppxProvisionedPackage -online')
    run_powershell('Get-AppxPackage -AllUsers | where-object {$_.name –notlike “*store*”} | Remove-AppxPackage')
Only the middle command seems to work (no issues with a new user account). I can't find anything about the `run_powershell` function; is it possible to get feedback like in the PowerShell console? The Wapt console isn't reporting any errors, yet the apps aren't being removed from the administrator account!

THANKS

Re: Running a PowerShell script?

Published: Dec 14, 2017 - 1:51 PM
by olaplanche
Additional information:

I am using the x64 version of powershell and this could be due to limitations of it for the system account.

Re: Running a PowerShell script?

Published: October 18, 2018 - 1:07 PM
by olaplanche
Good morning,

I'm bringing this problem up again, as I still haven't solved it.
Quite a few things have changed in the meantime, namely:

- Installed WAPT version: 1.6.2.7
- Server OS: Debian Stetch
- Operating system of the administration/package creation machine: Windows 10

Next, I use a new script during my MDT deployment; here it is:

Code: Select all

    # Functions
    function Write-LogEntry {
        param(
            [parameter(Mandatory=$true, HelpMessage="Value added to the RemovedApps.log file.")]
            [ValidateNotNullOrEmpty()]
            [string]$Value,
            [parameter(Mandatory=$false, HelpMessage="Name of the log file that the entry will written to.")]
            [ValidateNotNullOrEmpty()]
            [string]$FileName = "RemovedApps.log"
        )
        # Determine log file location
        $LogFilePath = Join-Path -Path $env:windir -ChildPath "Temp\$($FileName)"
        # Add value to log file
        try {
            Add-Content -Value $Value -LiteralPath $LogFilePath -ErrorAction Stop
        }
        catch [System.Exception] {
            Write-Warning -Message "Unable to append log entry to RemovedApps.log file"
        }
    }
    # Get a list of all apps
    Write-LogEntry -Value "Starting built-in AppxPackage and AppxProvisioningPackage removal process"
    $AppArrayList = Get-AppxPackage -PackageTypeFilter Bundle -AllUsers | Select-Object -Property Name, PackageFullName | Sort-Object -Property Name
    # White list of appx packages to keep installed
    $WhiteListedApps = @(
        "Microsoft.DesktopAppInstaller", 
        "Microsoft.WindowsCalculator", 
        "Microsoft.WindowsStore"
    )
    # Loop through the list of appx packages
    foreach ($App in $AppArrayList) {
        # If application name not in appx package white list, remove AppxPackage and AppxProvisioningPackage
        if (($App.Name -in $WhiteListedApps)) {
            Write-LogEntry -Value "Skipping excluded application package: $($App.Name)"
        }
        else {
            # Gather package names
            $AppPackageFullName = Get-AppxPackage -Name $App.Name | Select-Object -ExpandProperty PackageFullName
            $AppProvisioningPackageName = Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -like $App.Name } | Select-Object -ExpandProperty PackageName
            # Attempt to remove AppxPackage
            if ($AppPackageFullName -ne $null) {
                try {
                    Write-LogEntry -Value "Removing application package: $($App.Name)"
                    Remove-AppxPackage -Package $AppPackageFullName -ErrorAction Stop | Out-Null
                }
                catch [System.Exception] {
                    Write-LogEntry -Value "Removing AppxPackage failed: $($_.Exception.Message)"
                }
            }
            else {
                Write-LogEntry -Value "Unable to locate AppxPackage for app: $($App.Name)"
            }
            # Attempt to remove AppxProvisioningPackage
            if ($AppProvisioningPackageName -ne $null) {
                try {
                    Write-LogEntry -Value "Removing application provisioning package: $($AppProvisioningPackageName)"
                    Remove-AppxProvisionedPackage -PackageName $AppProvisioningPackageName -Online -ErrorAction Stop | Out-Null
                }
                catch [System.Exception] {
                    Write-LogEntry -Value "Removing AppxProvisioningPackage failed: $($_.Exception.Message)"
                }
            }
            else {
                Write-LogEntry -Value "Unable to locate AppxProvisioningPackage for app: $($App.Name)"
            }
        }
    }
    # White list of Features On Demand V2 packages
    Write-LogEntry -Value "Starting Features on Demand V2 removal process"
    $WhiteListOnDemand = "NetFX3|Language|Browser.InternetExplorer"
    # Get Features On Demand that should be removed
    $OnDemandFeatures = Get-WindowsCapability -Online | Where-Object { $_.Name -notmatch $WhiteListOnDemand -and $_.State -like "Installed"} | Select-Object -ExpandProperty Name
    foreach ($Feature in $OnDemandFeatures) {
        try {
            Write-LogEntry -Value "Removing Feature on Demand V2 package: $($Feature)"
            Get-WindowsCapability -Online -ErrorAction Stop | Where-Object { $_.Name -like $Feature } | Remove-WindowsCapability -Online -ErrorAction Stop | Out-Null
        }
        catch [System.Exception] {
            Write-LogEntry -Value "Removing Feature on Demand V2 package failed: $($_.Exception.Message)"
        }
    }
    # Complete
    Write-LogEntry -Value "Completed built-in AppxPackage and AppxProvisioningPackage removal process"
I would like to use it in a wapt package, but I don't see how...
Can someone point me in the right direction?

THANKS

Re: Running a PowerShell script?

Published: Dec 19, 2018 - 10:12
by olaplanche
I solved my problem with the following code:

Code: Select all

# -*- coding: utf-8 -*-
from setuphelpers import *
#import subprocess

uninstallkey = []

def install():
    run('powershell.exe -NoProfile -NonInteractive -File RemoveBuiltinApps.ps1')
You need to enable PowerShell script execution on the affected machines or add the "-executionpolicy bypass" parameter

Re: [SOLVED] Run a PowerShell script?

Published: October 15, 2019 - 11:45 AM
by jeancharles
Hello,

A bit late, but wouldn't it be better to modify the powershell command to add "-executionpolicy bypass" directly into the command?

Re: [SOLVED] Run a PowerShell script?

Published: October 15, 2019 - 12:00 PM
by olaplanche
Good morning,

That's exactly what I indicated in my sentence ;) :
You need to enable PowerShell script execution on the affected machines or add the "-executionpolicy bypass" parameter.

Re: [SOLVED] Run a PowerShell script?

Published: October 15, 2019 - 12:04 PM
by jeancharles
Ah yes, indeed, reading about it, that's it, :)
I'm trying to go too fast and I'm forgetting half the information along the way!

Thank you!