开始过程在ArgumentList中传递哈希表 [英] Start-Process passing a hashtable in the ArgumentList

查看:164
本文介绍了开始过程在ArgumentList中传递哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下情况:

内容MyScript.ps1:

Content MyScript.ps1:

Param (
    [String]$CountryCode,
    [String]$FilesPath,
    [String]$KeepassDatabase,
    [String]$KeepassKeyFile,
    [String]$EventLog = 'HCScripts',
    [String]$EventSource,
    [HashTable]$CitrixFarm = @{'Server1' = '6.5'}
)

$CountryCode
$FilesPath
$KeepassDatabase
$KeepassKeyFile
$EventLog
$EventSource
$CitrixFarm

Caller.ps1的内容:

Content of the Caller.ps1:

Param (
    $FilesPath = ".\MyScript.ps1",
    $EvenntLog = 'Test',
    $CountryCode = 'BNL',
    $KeepasDatabase,
    $KeepasKeyFile
)

$Arguments = @()
$Arguments += "-EventSource ""$AppName"""
$Arguments += "-EventLog ""$EventLog"""
$Arguments += "-FilesPath ""$((Get-Item $FilesPath).FullName)"""
$Arguments += "-CountryCode ""$CountryCode"""
$Arguments += "-KeepassDatabase ""$((Get-Item $KeepasDatabase).FullName)"""
$Arguments += "-KeepassKeyFile ""$((Get-Item $KeepasKeyFile).FullName)"""
$Arguments += "-CitrixFarm $CitrixFarm"

$StartParams = @{
    Credential   = $Credentials
    ArgumentList = "-File ""$ScriptPath"" -verb runas" + $Arguments
    WindowStyle  = 'Hidden'
}
Start-Process powershell @StartParams

我们似乎找不到为参数$CitrixFarm传递[HashTable]的方法.如何添加该参数.还是将其传递给Start-Process调用的具有较高权限的脚本并在新的PowerShell会话中?

We can't seem to find a way to pass in the [HashTable] for the argument $CitrixFarm. How is it possible to add that argument. or pass it on to the script called by Start-Process with elevated permissions and in a new PowerShell session?

省略参数$CitrixFarm时,一切正常.所以问题确实出在通过HashTable.

When omitting the parameter $CitrixFarm all is working fine. So the problem really is with passing the HashTable.

推荐答案

您应该以PowerShell对象表示形式传递哈希表,就像从PowerShell窗口运行脚本一样.

You should pass the hashtable in PowerShell object notation, just as you would if running the script from a PowerShell window.

如何构造字符串取决于您.

How you construct the string is up to you.

您可以

  • 使用字符串模板
  • 使用快速拨号"@$((ConvertTo-Json $CitrixFarm -Compress) -replace ':','=')"
  • 使用一个函数来转换哈希表对象.
  • use a string template
  • use a quick-and-dirty call "@$((ConvertTo-Json $CitrixFarm -Compress) -replace ':','=')"
  • use a function to convert the hashtable object.

以下实际上是您要实现的目标.

Below is effectively what you are trying to achieve.

$Arguments = @()
...
$Arguments += "-CitrixFarm @{'Server1' = '6.5'}"

$StartParams = @{
    Credential   = $Credentials
    ArgumentList = "-File ""$ScriptPath"" -verb runas" + $Arguments
    WindowStyle  = 'Hidden'
}
Start-Process powershell @StartParams

来源:哈希表的一种更好的ToString()方法

这篇关于开始过程在ArgumentList中传递哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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