从没有临时文件的 URL 运行 Powershell 脚本 [英] Run Powershell script from URL without temporary file

查看:54
本文介绍了从没有临时文件的 URL 运行 Powershell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个 PowerShell 脚本,该脚本运行存储在 URL 中的 PowerShell 脚本,传递自定义参数并转发提供给此 PowerShell 的任何参数.具体来说,我可以做到:

I want to write a PowerShell script that runs a PowerShell script stored at a URL, passing both custom arguments and forwarding any arguments that are given to this PowerShell. Concretely, I can do:

$VAR=Invoke-WebRequest http://example.com/powershell.ps1
$TEMP=New-TemporaryFile
$FILE=$TEMP.FullName + ".ps1"
$VAR.Content | Out-File $FILE
& $FILE somearguments $args

理想情况下,我希望在不使用临时文件的情况下执行此操作,但是 powershell -content - 似乎也不允许传递参数.有没有办法避免临时文件?

I'd ideally like to do that without using a temporary file, however powershell -content - doesn't seem to allow also passing arguments. Is there any way to avoid the temporary file?

推荐答案

您可以将内容转换为脚本块,然后将其作为带参数的命令调用

You can convert content to script block and then invoke it as command with arguments

$Script = Invoke-WebRequest 'http://example.com/powershell.ps1'
$ScriptBlock = [Scriptblock]::Create($Script.Content)
Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList ($args + @('someargument'))

这篇关于从没有临时文件的 URL 运行 Powershell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆