PowerShell - 删除项目和复制项目
问题描述:
我有一个版本检查功能,在我的脚本开始应该检查文件共享上的.exe版本,并将其与用户桌面上的.exe进行比较。如果版本不同,它将删除当前的桌面.exe并将其替换为文件共享上的.exe。但是,从.exe运行时它不会取代它,但是当它从PowerShell Studio或ISE运行时它将工作。我正在使用PowerShell Studio将其编译到.exe中。这里是我的代码:PowerShell - 删除项目和复制项目
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运行它?谢谢。
答
我发现了我明显的解决方案。我刚刚创建了一个单独的脚本的更新和改变了我的脚本:
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")
我测试了它和它的伟大工程。感谢大家的建议!
如何运行可执行文件?由用户还是服务?无论任何运行有权访问该共享? – Matt
它由用户从桌面以管理员身份运行。它可以访问该共享,因为它可以访问其他内容。 –
是否正在替换正在运行的相同可执行文件?用户是否启动DeviceHealthV2.exe,并试图使其删除/替换自己? – TheMadTechnician