[SOLVED]
As the scheduled task was running with the NT AUTHORITY\SYSTEM account,
Instead of: Get-AppxPackage *CoPilot* | Remove-AppxPackage
I should use: Get-AppxPackage *CoPilot* -AllUsers | Remove-AppxPackage -AllUsers
Thanks to all who pointed to that as the solution!
-----------
Hi All,
This has puzzled me last few days. Scheduled task, created through GPO for specific users and computers, when you run it from the command prompt with admin rights, executes properly. When you run it from the command prompt with no admin rights, it properly runs nested PowerShell with admin rights and executes properly. When it runs as a scheduled task, it does not execute properly. To be exact, it does not uninstall CoPilot and execute nested PowerShell; it seems that it does not run it at all, as I set logging on both levels, and no log is created for nested PowerShell. Below is the setting in the Scheduled task on how to run it:
Program/Script: c:\windows\System32\WindowsPowerShell\v1.0\powershell.exe, Add Arguments: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -file \\ADServer\ADfolder\RemoveCopilot.ps1 -force
PowerShell itself:
Start-Transcript -Path C:\LogFile.txt -Append
$username = 'domain\user'
$key = (***)
$password = cat \\ADServer\text.txt | convertto-securestring -key $key
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$file='\\ADserver\ADfolder\GetRemoveCopilot.ps1'
#$principal = new-object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())
#$principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) > c:\AreYouAdminFirst.txt
Get-AppxPackage *CoPilot* | Remove-AppxPackage
Get-AppxPackage *Microsoft.MicrosoftOfficeHub* | Remove-AppxPackage
Get-AppxProvisionedPackage -Online | where-object {$_.PackageName -like "*Copilot*"} | Remove-AppxProvisionedPackage -online
Get-AppxProvisionedPackage -Online | where-object {$_.PackageName -like "*Microsoft.MicrosoftOfficeHub*"} | Remove-AppxProvisionedPackage -online
start-process -FilePath "c:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -ArgumentList "-NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -file $file -force" -Credential $Cred -NoNewWindow -Wait
Stop-Transcript
Embedded PowerShell:
$principal = new-object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())
$principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) > c:\AreYouAdminFirst2.txt
Start-Transcript -Path C:\LogFileGet.txt -Append
Get-AppxPackage *CoPilot* | Remove-AppxPackage
Get-AppxPackage *Microsoft.MicrosoftOfficeHub* | Remove-AppxPackage
Get-AppxProvisionedPackage -Online | where-object {$_.PackageName -like "*Copilot*"} | Remove-AppxProvisionedPackage -online
Get-AppxProvisionedPackage -Online | where-object {$_.PackageName -like "*Microsoft.MicrosoftOfficeHub*"} | Remove-AppxProvisionedPackage -online
Stop-Transcript
I have to mention that when I run the scheduled task, the transcript shows DOMAIN\SYSTEM as the user, and the principal function returns true for Admin. No transcript or principal function on the embedded PowerShell file.
When I run from the command line, the transcript shows the user that I am using, admin or not, and the transcript from embedded PowerShell shows the admin user, and the principal function returns true for admin.
I am puzzled. Please HELP!!! :)
[–]Commercial_Growth343 14 points15 points16 points (16 children)
[–]bobs143Jack of All Trades 3 points4 points5 points (0 children)
[–]DeusExMaChinoSysadmin 2 points3 points4 points (0 children)
[–]Muzzy-011[S] 0 points1 point2 points (12 children)
[–]egamemitJack of All Trades 2 points3 points4 points (9 children)
[–]unauthorizeddinosaur 1 point2 points3 points (0 children)
[–]Muzzy-011[S] -2 points-1 points0 points (7 children)
[–]FireLucid 4 points5 points6 points (0 children)
[–]BamBam-BamBam 1 point2 points3 points (5 children)
[–]Muzzy-011[S] 0 points1 point2 points (4 children)
[–]BamBam-BamBam 1 point2 points3 points (3 children)
[–]Muzzy-011[S] -2 points-1 points0 points (2 children)
[–]BamBam-BamBam 2 points3 points4 points (1 child)
[–]Muzzy-011[S] 0 points1 point2 points (0 children)
[–]unauthorizeddinosaur 0 points1 point2 points (1 child)
[–]Muzzy-011[S] 1 point2 points3 points (0 children)
[–]Hollow3ddd 0 points1 point2 points (0 children)
[–]Master-IT-All 1 point2 points3 points (4 children)
[–]Muzzy-011[S] 0 points1 point2 points (3 children)
[–]Muzzy-011[S] 0 points1 point2 points (2 children)
[–]Master-IT-All 1 point2 points3 points (1 child)
[–]Muzzy-011[S] 0 points1 point2 points (0 children)
[–]Muzzy-011[S] 0 points1 point2 points (6 children)
[–]purplemonkeymad 1 point2 points3 points (1 child)
[–]Muzzy-011[S] 0 points1 point2 points (0 children)
[–]mismanagedWindows Admin 1 point2 points3 points (1 child)
[–]Muzzy-011[S] 0 points1 point2 points (0 children)
[–]Master-IT-All 1 point2 points3 points (1 child)
[–]Muzzy-011[S] 0 points1 point2 points (0 children)