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
的内容:
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 会话中调用的脚本?
当省略参数 $CitrixFarm
时一切正常。所以问题真的是传递 HashTable
.
您应该以 PowerShell 对象表示法传递哈希表,就像您 运行 来自 PowerShell window.
的脚本一样
如何构造字符串由您决定。
你可以
- 使用字符串模板
- 使用快速调用
"@$((ConvertTo-Json $CitrixFarm -Compress) -replace ':','=')"
- use a function 转换哈希表对象。
以下实际上是您要实现的目标。
$Arguments = @()
...
$Arguments += "-CitrixFarm @{'Server1' = '6.5'}"
$StartParams = @{
Credential = $Credentials
ArgumentList = "-File ""$ScriptPath"" -verb runas" + $Arguments
WindowStyle = 'Hidden'
}
Start-Process powershell @StartParams
考虑以下情况:
内容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
的内容:
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 会话中调用的脚本?
当省略参数 $CitrixFarm
时一切正常。所以问题真的是传递 HashTable
.
您应该以 PowerShell 对象表示法传递哈希表,就像您 运行 来自 PowerShell window.
的脚本一样如何构造字符串由您决定。
你可以
- 使用字符串模板
- 使用快速调用
"@$((ConvertTo-Json $CitrixFarm -Compress) -replace ':','=')"
- use a function 转换哈希表对象。
以下实际上是您要实现的目标。
$Arguments = @()
...
$Arguments += "-CitrixFarm @{'Server1' = '6.5'}"
$StartParams = @{
Credential = $Credentials
ArgumentList = "-File ""$ScriptPath"" -verb runas" + $Arguments
WindowStyle = 'Hidden'
}
Start-Process powershell @StartParams