PowerShell - 删除项目和复制项目

时间:2016-08-24 19:01:22

标签: powershell

我的脚本开头有一个版本检查功能,用于检查文件共享上的.exe版本,并将其与用户桌面上的.exe进行比较。如果版本不同,它将删除当前桌面.exe并将其替换为文件共享上的.exe。

但是,从.exe运行时它不会替换它,但是当我从PowerShell Studio或ISE运行它时它会工作。我正在使用PowerShell Studio将其编译到.exe文件中。这是我的代码:

function global:VersionCheck
{
    # Get filepath of updated program
    $updatedprogram = (Get-Item \\fileshare\example\DeviceHealth\DeviceHealthV2.exe).VersionInfo | select -ExpandProperty FileVersion
    # Get version of current script on device
    $outdateprogram = (Get-Item c:\Users\$env:USERNAME\desktop\DeviceHealthV2.exe).VersionInfo | select -ExpandProperty FileVersion

    if ($updatedprogram -ne $outdateprogram)
    {
        [System.Windows.Forms.MessageBox]::Show("Program will be updated", "Out of Date")
        Start-Sleep -Seconds 2
        Remove-Item -Path "c:\Users\$env:USERNAME\desktop\DeviceHealthV2.exe" -Force
        Start-Sleep -Seconds 2
        Copy-Item -Path "\\fileshare\example\DeviceHealth\DeviceHealthV2.exe" -Destination "c:\Users\$env:USERNAME\desktop" -Force
    }
    else { }
}

VersionCheck

当我将其作为EXE文件运行时,如何确定它无法正常工作?

1 个答案:

答案 0 :(得分:0)

我找到了明显的解决方案。我刚刚为updater创建了一个单独的脚本,并将我的脚本更改为:

function global:VersionCheck
{
    # Get filepath of updated program
    $updatedprogram = (Get-Item \\fileshare\test\DeviceHealth\DeviceHealthV2.exe).VersionInfo | select -ExpandProperty FileVersion
    # Get version of current script on device
    $outdateprogram = (Get-Item c:\Users\$env:USERNAME\desktop\DeviceHealthV2.exe).VersionInfo | select -ExpandProperty FileVersion

    if ($updatedprogram -ne $outdateprogram)
    {
        [System.Windows.Forms.MessageBox]::Show("Program will be updated", "Out of Date")
        Start-Process "\\fileshare\test\scripts\Martin\PMCS\DeviceHealthUpdate.exe" -WindowStyle Hidden
    }
    else { }
}

VersionCheck

在更新脚本中我放了:

Get-Process DeviceHealthV2 | Foreach-Object { $_.CloseMainWindow() | Out-Null } | stop-process –force
Remove-Item -Path "c:\Users\$env:USERNAME\desktop\DeviceHealthV2.exe" -Force
Start-Sleep -Seconds 1
Copy-Item -Path "\\fileshare\ask\DeviceHealth\DeviceHealthV2.exe" -Destination "c:\Users\$env:USERNAME\desktop" -Force
Start-Sleep -Seconds 1
Start-Process "\\fileshare\ask\DeviceHealth\DeviceHealthV2.exe"
Start-Sleep -Seconds 1
[System.Windows.Forms.MessageBox]::Show("Update Successful", "Complete")

我测试了它并且效果很好。