且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在本地执行Powershell脚本以在远程计算机上运行

更新时间:2021-12-20 22:42:54

我在项目中使用过一次.我需要在APP服务器上创建网站并且必须从INT服务器运行的地方.因此,我将站点设置脚本保留在App服务器上,并从INT服务器远程运行.

I have used this once in a project. Where i needed to create website on APP server and had to run from a INT server. So I kept the site setup script on App server and ran remotely from INT server.

 $script = 
 {
 $Filetoexe = $args[0]
 invoke-expression "$Filetoexe"
 }
 Invoke-Command -ComputerName <Computername> -credential <credentials> -ScriptBlock $script -Args "D:\sitesetup.ps1"

保存并运行.最后一行"Invoke-Command"将被执行,它将访问您提供的远程计算机并执行脚本块.

save this and run this. The last line "Invoke-Command" will get executed and it will access the remote computer that you have given and will execute the script block .

脚本块采用在最后一行中传递的参数,并执行脚本块中的文件.

The script block takes the argument passed in the last line and executes the file in the script block.

您需要在远程系统上运行的所有功能都必须在脚本块中完成.

What ever function you need to run on the remote system must be done with in the script block.

希望这会有所帮助....

Hope it is helpful....