none
Powershell脚本问题 RRS feed

  • 问题

  • 技术支持你们好,注释一下下面Powershell脚本,有些地方太深奥不太明白,谢谢

    region Configuration

    # 日志保存位置
    $Logfile = "\\1\source\resource\StartUp.txt"

    # 日志输出等级(值 0-2,默认值 1)
    $LogLevel = 1

    $WeChatVersion = "2.7.1.88"

    $BandiZipVersion = "6.25.0.1"

    #endregion

    #region Functions

    function WriteLog ($level, $text)
    {
        Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') $env:COMPUTERNAME: $text"
        
        if ($level -le $LogLevel)
        {
            Write-Output "$(Get-date -Format 'yyyy-MM-dd HH:mm:ss'),$env:COMPUTERNAME,$text" | Out-File -FilePath $Logfile -Append -Encoding unicode
        }
    }

    #endregion


    #region Main

    WriteLog 2 "$($(Get-WmiObject Win32_OperatingSystem).Caption) ($env:PROCESSOR_ARCHITECTURE)"

    if ($env:PROCESSOR_ARCHITECTURE -eq "x86")
    {
        $DestDir = $env:ProgramFiles
        $DestReg = "HKLM:\Software"
    }
    elseif ($env:PROCESSOR_ARCHITECTURE -eq "AMD64")
    {
        $DestDir = ${env:ProgramFiles(x86)}
        $DestReg = "HKLM:\Software\Wow6432Node"
    }
    else
    {
        WriteLog 1 "The OS platform is unidentified."
        Exit
    }

    #Update WeChat

    if (Test-Path "C:\Program Files (x86)\Tencent\WeChat\WeChatWin.dll")
    {
        $fileVersion = (Get-Item "C:\Program Files (x86)\Tencent\WeChat\WeChatWin.dll").VersionInfo.FileVersion
        if ($fileVersion -lt $WeChatVersion)
        {
            WriteLog 1 "Start update WeChat: $fileVersion -> $WeChatVersion"
            Start-Process "\\1\source\Software\WeChatSetup.exe" -ArgumentList "/S" -NoNewWindow -Wait

            $fileVersion = (Get-Item "C:\Program Files (x86)\Tencent\WeChat\WeChatWin.dll").VersionInfo.FileVersion
            if ($fileVersion -ne $WeChatVersion)
            {
                WriteLog 1 "Error to update WeChat: $fileVersion"
            }
            else
            {
                WriteLog 1 "Finish to update WeChat: $fileVersion"
            }
        }
        else
            {
                WriteLog 1 "Found WeChat: $fileVersion"
            }
    }
    else
    {
        WriteLog 1 "Not Found WeChat"
    }

    #Update BandiZip

    if (Test-Path "C:\Program Files\Bandizip\Bandizip.exe")
    {
        $fileVersion = (Get-Item "C:\Program Files\Bandizip\Bandizip.exe").VersionInfo.FileVersion
        if ($fileVersion -lt $BandiZipVersion)
        {
            WriteLog 1 "Start update BandiZip: $fileVersion -> $BandiZipVersion"
            Start-Process "\\1\source\Software\BANDIZIP-SETUP.exe" -ArgumentList "/S" -NoNewWindow -Wait

            $fileVersion = (Get-Item "C:\Program Files\Bandizip\Bandizip.exe").VersionInfo.FileVersion
            if ($fileVersion -ne $BandiZipVersion)
            {
                WriteLog 1 "Error to update BandiZip: $fileVersion"
            }
            else
            {
                WriteLog 1 "Finish to update BandiZip: $fileVersion"
            }
        }
        else
            {
                WriteLog 1 "Found BandiZip: $fileVersion"
            }
    }
    else
    {
        WriteLog 1 "Not Found BandiZip"
    }


    #endregion


    • 已编辑 45度半 2020年7月6日 3:21
    2020年7月6日 3:13

答案

  • 这个脚本不确定是否是完整的升级脚本且与日志有关,主要有两个升级的,一个是wechat,应该是微信,另一个是BandiZip。

    在脚本中有标记如果升级成功,会提示什么,如果升级失败,会提示什么。

    并且,在脚本中已经有一定的注解,在符号#后面的一般为对于该段落的注释。


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    2020年7月6日 8:17

全部回复