PowerShell远程执行脚本是如何引用外部参数
来源:互联网 发布:道家经典软件 编辑:程序博客网 时间:2024/06/05 07:50
最近在写Jenkins job,其中我想用PowerShell远程执行一段脚本,并且在这段脚本中使用外部变量,是了很多方法都不行,后来参考一下两篇文章终于解决了:
http://ss64.com/ps/invoke-command.html
http://blog.csdn.net/itanders/article/details/9073351
下面来讲讲我的方法:
首先有一个外部变量,该变量是在执行远程脚本之前初始化的:
$variables = Get-Content -Path Bundles.txt$variables
然后启动一个远程会话:
$user = $env:USER_NAME$password = $env:PASSWORDEnable-PSRemoting -Force$secPassword = ConvertTo-SecureString $password -AsPlainText –Force$mycreds = New-Object system.Management.Automation.PSCredential($user, $secPassword)$s = New-PSSession -computerName $DestinationServer -credential $mycreds
最后在远程机器上执行代码:
Invoke-Command -Session $s -ArgumentList $variables -Scriptblock { param($variables)$DeployedBundles = "" if($variables -ne $null -and $variables.Length -gt 0){ $DeployedBundles = $variables.Split("=")[1].Split(",") } #Other codes...}
看到了吗? 关键点在于Invoke-Command命令引用了一个参数 -ArgumentList,同时在ScriptBlock里面使用param调用该参数?
哈哈,就这么简单问题解决了。。。
0 0
- PowerShell远程执行脚本是如何引用外部参数
- 如何用计划任务执行powershell脚本
- 使用Powershell 远程执行
- POWERSHELL脚本执行权限
- POWERSHELL脚本执行权限
- PowerShell如何依靠全局错误处理并执行脚本
- PowerShell 如何 远程连接
- powershell 远程执行命令失败
- C#中执行PowerShell 脚本
- C#中执行PowerShell 脚本
- Mysql 执行外部脚本
- Unity后台是如何执行脚本的
- MySQL引用外部sql脚本
- ssh远程执行脚本
- 执行远程服务器脚本
- linux远程执行脚本
- ssh 远程执行脚本
- saltstack 远程执行脚本
- Android 编程下两种方式注册广播的区别
- QT 发布带有QPrinter的程序
- <c:forEach>标签,需要与el表达式联合使用
- xmlns命名空间
- Java常用的几种缓存开源框架
- PowerShell远程执行脚本是如何引用外部参数
- Unity3D使用NGUI制作小地图
- Win7下面安装hadoop2.x插件及Win7/Linux运行MapReduce程序
- jquery html5 实现placeholder兼容password ie6
- Hbase的基本操作
- listView的普通使用
- Count and Say
- PHP OPENSSL 函数手册翻译备查 -- openssl_cipher_iv_length
- JSP application和session