我想知道是否有人知道有一种方法可以让powershell脚本在运行之前检查自身的更新。
我有一个要分派到多台计算机的脚本,我不想每次更改脚本时都要将其重新部署到每台计算机上。我想让它检查一个特定的位置,看看是否有更新的版本(如果需要的话,自我更新)。
我似乎想不出一个办法来做这件事。如果有人能帮上忙,请告诉我。谢谢。
发布于 2013-04-04 04:53:06
好吧,一种方法可能是创建一个简单的批处理文件来运行您的实际脚本,该批处理文件的第一行可能是检查更新文件夹中是否存在ps1。如果有,它可以先将其复制下来,然后启动powershell脚本
例如:每当有更新时,就将'Mypowershellscript.ps1‘脚本放在c:\temp\update\ folder
中
让我们假设您的脚本将从
c:\temp\myscriptfolder\
然后您可以创建批处理文件,如下所示
if NOT exist C:\temp\update\mypowershelscript.ps1 goto :end
copy /Y c:\temp\update\MyPowerShellScript.ps1 c:\temp\MyScriptFolder\
:END
%systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -noprofile -file "c:\temp\myscriptfolder\mypowershellscript.ps1"
发布于 2017-12-30 23:30:04
这是我拼凑的一个函数。将可能包含较新版本的文件的路径传递给它。这将自我更新,然后使用传递给原始脚本的任何参数重新运行。在过程的早期执行此操作,其他函数结果将会丢失。我通常会检查网络是否正常,并且可以看到包含较新文件的共享,然后运行以下命令:
function Update-Myself
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true,
Position = 0)]
[string]$SourcePath
)
#Check that the file we're comparing against exists
if (Test-Path $SourcePath)
{
#The path of THIS script
$CurrentScript = $MyInvocation.ScriptName
if (!($SourcePath -eq $CurrentScript ))
{
if ($(Get-Item $SourcePath).LastWriteTimeUtc -gt $(Get-Item $CurrentScript ).LastWriteTimeUtc)
{
write-host "Updating..."
Copy-Item $SourcePath $CurrentScript
#If the script was updated, run it with orginal parameters
&$CurrentScript $script:args
exit
}
}
}
write-host "No update required"
}
Update-Myself "\\path\to\newest\release\of\file.ps1"
https://stackoverflow.com/questions/15797469
复制相似问题