Powershell:移除個人版Skype8.X

前言:個人版Skype安裝起來看似一個很普通的應用程式,新增移除程式裡面看的到,機碼裡面找的到,但是如果想透過一些Powershell的一些標準方式來佈署大量移除又沒有辦法,例如Get-WmiObject Win32_Product篩選出Skype後移除,卻又完全Get不到Skype有裝在裡面的痕跡。嘗試使用Get-AppxPackage卻也完全找不到Skype。所以只好透過一些Powershell的判斷來執行反安裝程式並且自動帶入管理員帳號密碼。
下面的Script包含了寫入Log與檢查機碼,比較適合公司使用。

GlobalVar.txt>此文字檔只放下面兩行即可
FSRV_A=\\11.22.33.44
FSRV_B=\\11.22.33.55
REM RemoveSkypeHome.bat
echo off
for /f "delims== tokens=1,2" %%G in (%~dp0GlobalVar.txt) do set %%G=%%H
REM 全域變數,檔案伺服器位置
IF EXIST C:\Tools GOTO :CopyPs1 
mkdir c:\tools
:CopyPs1 
cls
copy "%FSRV_A%\update\RemoveSkypeHome*.ps1" "c:\tools\"
REM 複製所需的Powershell到本機
copy "%FSRV_A%\update\GlobalVar.txt" "c:\tools"
REM 複製縮需的全域變數檔案到本機
powershell c:\tools\RemoveSkypeHome.ps1
REM 執行Powershell
pause
erase c:\tools\RemoveSkypeHome*.ps1 /f
erase c:\tools\GlobalVar.txt /f
REM 刪除剛剛所複製過來的檔案
#RemoveSkypeHome.ps1
Remove-Variable * -ErrorAction SilentlyContinue#清除所有變數
Get-Content $PSScriptRoot\GlobalVar.txt | Foreach-Object{
    $var = $_.Split('=')
    New-Variable -Name $var[0] -Value $var[1]
 }
#以上為全域參數判斷檔案伺服器的IP位置,內有變數FSRV_A和FSRV_B值
$UserN = "$env:USERNAME"
$LogFile = "Record_RmSkypeHome"
$LogPath = "$FSRV_A\share\IT\CSV_Log\"
Function LogWrite
{
    Param([string]$logstring)
    [String]$Logfile = $LogPath+$LogFile+".CSV" 
    Add-content $Logfile "$(Get-date -Format 'yyyy-MM-dd HH:mm:ss') $logstring" 
}
#以上為Log寫入功能

Function Test-RegistryValue {
    param(
        [Alias("PSPath")]
        [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [String]$Path
        ,
        [Parameter(Position = 1, Mandatory = $true)]
        [String]$Name
        ,
        [Switch]$PassThru
    ) 

    process {
        if (Test-Path $Path) {
            $Key = Get-Item -LiteralPath $Path
            if ($Key.GetValue($Name, $null) -ne $null) {
                if ($PassThru) {
                    Get-ItemProperty $Path $Name
                } else {
                    $true
                }
            } else {
                $false
            }
        } else {
            $false
        }
    }
}
#以上為檢查註冊機碼Function
$CHKSkyHomeRun = Test-RegistryValue -PSPath "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "Skype for Desktop"
$CHKSkyHome = Test-Path -path "C:\Program Files (x86)\Microsoft\Skype for Desktop\unins000.exe"
IF(!($CHKSkyHome))#如果反安裝檔案位置不存在
{
    if(!($CHKSkyHomeRun))#如果Skype啟動機碼不存在
    {
        $Type = "Skype not installed"
    }
    ELSE
    {
        Remove-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "Skype for Desktop" -ErrorAction SilentlyContinue#移除Skype啟動機碼(通常是移除軟體後沒有正常移除登入後啟動造成的)
        $Type = "Skype not install. Startup reg is removed"
    }
    
    LogWrite ",$UserN,$env:COMPUTERNAME,$Type"#寫入Log
    Exit
}

$Username = "abc\Admin_XXXXX"#管理員權限帳號
$Password = "XXXXXXXXXX"#管理員權限密碼
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass 
$Sub = "C:\tools\RemoveSkypeHome_Sub.ps1"

Remove-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "Skype for Desktop" -ErrorAction SilentlyContinue#移除Skype啟動機碼
(Start-Process powershell -ArgumentList "-ExecutionPolicy ByPass -file $Sub" -WorkingDirectory "$FSRV_A\update\" -Credential $Cred -PassThru).WaitForExit()
$Type = "Skype removed"#執行反安裝Powershell
LogWrite ",$UserN,$env:COMPUTERNAME,$Type"
#RemoveSkypeHome_Sub.ps1#執行反安裝程式
Start-Process "C:\Program Files (x86)\Microsoft\Skype for Desktop\unins000.exe"

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *