开始过程在ArgumentList中传递哈希表 [英] Start-Process passing a hashtable in the 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
这篇关于开始过程在ArgumentList中传递哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!