本地或远程执行带有通用参数的 powershell 脚本 [英] Local or remote execution of powershell script with generic parameters

查看:44
本文介绍了本地或远程执行带有通用参数的 powershell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发团队中,我希望有相同的测试脚本由开发人员在本地执行或由我们的测试平台远程执行.

In a development team, I would like to have the same test scripts to be executed locally by a developper or remotely by our test platform.

这是我想用作每个脚本的前提

Here is what I would like to use as premises for each script

# Test local/remote execution by reading C:\ directory
param(
    [switch] $verbose,
    [switch] $remote,
    [string] $ip,
    [string] $user,
    [string] $password
    #Add here script specific parameters
)

Write-Host "Command invokation incoming parameter count : " $psboundparameters.count

if ($remote) {
    $Params = @{}
    $RemoteParams = @{}
    $pass = ConvertTo-SecureString -String $password -AsPlainText -Force 

    $Params.Credential = new-object -TypeName System.management.automation.PSCredential -argumentlist $user, $pass
    $Params.ComputerName = $ip
    $Params.FilePath = $MyInvocation.MyCommand.Name
    $null = $psboundparameters.Remove('remote')
    $null = $psboundparameters.Remove('ip')
    $null = $psboundparameters.Remove('user')
    $null = $psboundparameters.Remove('password')

    foreach($psbp in $PSBoundParameters.GetEnumerator())
    {
        $RemoteParams.$($psbp.Key)=$psbp.Value
    }
    Write-Host $RemoteParams
    Invoke-Command @Params @Using:RemoteParams
    Exit 
}

Write-Host "Command execution incoming parameters count : "    $psboundparameters.count

# Here goes the test 
Get-ChildItem C:\

但是,当我执行此操作时,出现以下错误:

However, when I execute this, I got the following error:

Invoke-Command : A positional parameter cannot be found that accepts argument '$null'.

看来 @Using:RemoteParams 不是这样做的正确方法,但我在这里很迷茫.提前致谢

It seems that @Using:RemoteParams is not the correct way of doing this, but I'm quite lost here. Thanks in advance

推荐答案

以下是我对使用命名参数进行本地和远程执行的问题的看法:

Here's my take on the problem of being able to do both local and remote execution using named parameters:

$IP = '192.168.0.1'
$User = 'Test User'
$Password = 'P@ssW0rd!' 

$params = @{
IP = $IP
User = $User
Password = $Password
}

$command = 'new-something'

$ScriptBlock = [Scriptblock]::Create("$command $(&{$args} @Params)")

从参数的哈希表开始,使用局部变量,然后使用:

Start with a hash table of parameters, using local varibles, then use this:

[Scriptblock]::Create("$command $(&{$args} @Params)")

创建命令的脚本块,参数内联并且值已经扩展.现在该脚本块已准备好在本地运行(通过使用 & 或点源调用),或使用 Invoke-Command 远程运行.

to create a script block of the command, with the parameters inline and the values already expanded. Now that script block is ready to be run locally (either by invocation with & or dot-sourcing), or remotely using Invoke-Command.

$ScriptBlock
new-something -IP: 192.168.0.1 -User: Test User -Password: P@ssW0rd!

无需使用 $Using:-argumentlist 进行范围界定.

No scoping with $Using: or -argumentlist required.

这是一个使用脚本而不是单个命令的示例:

Here's an example using a script rather than a single command:

$path = 'c:\windows'
$filter = '*.xml'

$Params = 
@{
   Path = $path
   Filter = $filter
  }

$command = @'
{
  Param (
    [String]$path,
    [String]$Filter
   )

 Get-childitem -Path $path -Filter $filter
}
'@

$ScriptBlock = [Scriptblock]::Create(".$command $(&{$args} @Params)")

在本地运行:

 Invoke-Command $ScriptBlock

或者只是:

 .$ScriptBlock

远程运行:

 Invoke-Command -Scriptblock $ScriptBlock -ComputerName Server1

这篇关于本地或远程执行带有通用参数的 powershell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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