如何运行基于联机的PowerShell脚本

时间:2016-02-16 17:22:06

标签: powershell scripting

我有一个存储在这里的Powershell脚本:

https://gitlab.example.example.co.uk/example/example/raw/master/shrink-diskpart.ps1

我想通过这个gitlab中的计划任务在许多服务器上运行它,这样我就可以对脚本进行单一更改,它将在所有服务器上运行最新的一个。

任何人都可以建议这是否可行,如果它是如何做到的?

由于

1 个答案:

答案 0 :(得分:0)

您可以将Invoke-WebRequest与-OutFile一起使用来下载文件,然后执行它。您可以将文件作为.txt存储在Web服务器上,这样就不必添加MIME类型。

$scriptUrl = "http://localhost/test.txt"
$destination = "c:\temp\test.txt"
$scriptName = "c:\temp\test.ps1"

Invoke-WebRequest $scriptUrl -OutFile $destination

# if the file was downloaded, delete the old script file and rename the new
# file
if(test-path $destination){
    remove-item $scriptName
    Rename-Item $destination $scriptName
}

&$scriptName

支持http://www.powershellatoms.com/basic/download-file-website-powershell/