Start-Process 在 ArgumentList 中传递哈希表 [英] Start-Process passing a hashtable in the ArgumentList
问题描述
考虑以下情况:
内容 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
这篇关于Start-Process 在 ArgumentList 中传递哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!