Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 启动进程,在ArgumentList中传递哈希表_Powershell_Arguments_Parameter Passing_Start Process - Fatal编程技术网

Powershell 启动进程,在ArgumentList中传递哈希表

Powershell 启动进程,在ArgumentList中传递哈希表,powershell,arguments,parameter-passing,start-process,Powershell,Arguments,Parameter Passing,Start Process,考虑以下情况: 内容MyScript.ps1: Param ( [String]$CountryCode, [String]$FilesPath, [String]$KeepassDatabase, [String]$KeepassKeyFile, [String]$EventLog = 'HCScripts', [String]$EventSource, [HashTable]$CitrixFarm = @{'Server1' = '6.

考虑以下情况:

内容
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
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
调用方.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
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]
的方法。怎么可能加上那个论点呢。或者在新的PowerShell会话中,使用提升的权限将其传递到由
Start Process
调用的脚本


省略参数
$CitrixFarm
时,一切正常。因此,传递
哈希表时确实存在问题,您应该以PowerShell对象表示法传递哈希表,就像从PowerShell窗口运行脚本一样

如何构造字符串取决于您

你可以

  • 使用字符串模板
  • 使用快速脏调用“@$((转换为Json$CitrixFarm-Compress)-替换“:”,“=”)”
  • 转换哈希表对象
  • 下面是你想要达到的目标

    $Arguments = @()
    ...
    $Arguments += "-CitrixFarm @{'Server1' = '6.5'}"
    
    $StartParams = @{
        Credential   = $Credentials
        ArgumentList = "-File ""$ScriptPath"" -verb runas" + $Arguments
        WindowStyle  = 'Hidden'
    }
    Start-Process powershell @StartParams
    

    来源:

    你想用
    -动词runas
    实现什么?@TheIncorrigible1
    -动词runas
    请求提升。@Bill\u Stewart我知道是的,但他需要吗?如果不需要,他为什么要尝试启动不同的powershell进程?您需要将哈希表转换回其powershell对象表示法。命令行上的
    $CitrixFarm
    参数的参数应该是什么样子?