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
从url抓取参数并将其放到powershell变量中_Powershell - Fatal编程技术网

从url抓取参数并将其放到powershell变量中

从url抓取参数并将其放到powershell变量中,powershell,Powershell,我有一个powershell侦听器在我的windows box上运行。Powershell库中的代码: “客户端”使用以下示例url调用侦听器: 使用Powershell,我会: invoke-webrequest -Uri "http://127.0.0.1:8888/Test?id='1234'&param2='@@$$'&param3='This is a test!'" 我不知道如何将参数从url中删除到powershell中同名的变量。我需要将参数带到powershe

我有一个powershell侦听器在我的windows box上运行。Powershell库中的代码:

“客户端”使用以下示例url调用侦听器: 使用Powershell,我会:

invoke-webrequest -Uri "http://127.0.0.1:8888/Test?id='1234'&param2='@@$$'&param3='This is a test!'"
我不知道如何将参数从url中删除到powershell中同名的变量。我需要将参数带到powershellvariables,以便简单地回显它们。这是我丢失的最后一部分。参数用&分隔,参数名称区分大小写

更详细地说,url中的id应该位于名为$id、值为1234的powershell变量中。变量可以包含空格、特殊字符、数字和字母。它们区分大小写。parametervalue可以是“我的名字是“Anna”!我的宠物名字是“Bello.”,所有的“脏”字符都是“%$”!{[()


有人能告诉我解决这个问题的正确方法吗?

有效的url中,$字符应该转义到
%24
Url转义形式中的另一个“脏”字符%是
%25
这意味着示例url无效,应为:

$url=”http://127.0.0.1:8888/Test?id='1234'¶m2='@%24%24'¶m3='这是一个测试!''“

那么下面的方法就行了

$url = "http://127.0.0.1:8888/Test?id='1234'&param2='@@%24%24'&param3='This is a test!'"

if ($url -is [uri]) {
    $url = $url.ToString()
}
# test if the url has a query string
if ($url.IndexOf('?') -ge 0) {
    # get the part of the url after the question mark to get the query string
    $query = ($url -split '\?')[1]    
    # or use: $query = $url.Substring($url.IndexOf('?') + 1)

    # remove possible fragment part of the query string
    $query = $query.Split('#')[0]

    # detect variable names and their values in the query string
    foreach ($q in ($query -split '&')) {
        $kv = $($q + '=') -split '='
        $varName  = [uri]::UnescapeDataString($kv[0]).Trim()
        $varValue = [uri]::UnescapeDataString($kv[1])
        New-Variable -Name $varname -Value $varValue -Force
    }
}
else {
    Write-Warning "No query string found as part of the given URL"
}
通过将新创建的变量写入控制台来证明这一点

Write-Host "`$id = $id"
Write-Host "`$param2 = $param2"
Write-Host "`$param3 = $param3"
在本例中,哪个将打印

然而,我个人不想创建这样的变量,因为有覆盖现有变量的风险。 我认为最好将它们存储在如下哈希中:

# detect variable names and their values in the query string
# and store them in a Hashtable
$queryHash = @{}
foreach ($q in ($query -split '&')) {
    $kv = $($q + '=') -split '='
    $name = [uri]::UnescapeDataString($kv[0]).Trim()
    $queryHash[$name] = [uri]::UnescapeDataString($kv[1])
}
$queryHash
哪个输出


在有效的url中,$字符应该转义到
%24
Url转义形式中的另一个“脏”字符%是
%25
这意味着示例url无效,应为:

$url=”http://127.0.0.1:8888/Test?id='1234'¶m2='@%24%24'¶m3='这是一个测试!''“

那么下面的方法就行了

$url = "http://127.0.0.1:8888/Test?id='1234'&param2='@@%24%24'&param3='This is a test!'"

if ($url -is [uri]) {
    $url = $url.ToString()
}
# test if the url has a query string
if ($url.IndexOf('?') -ge 0) {
    # get the part of the url after the question mark to get the query string
    $query = ($url -split '\?')[1]    
    # or use: $query = $url.Substring($url.IndexOf('?') + 1)

    # remove possible fragment part of the query string
    $query = $query.Split('#')[0]

    # detect variable names and their values in the query string
    foreach ($q in ($query -split '&')) {
        $kv = $($q + '=') -split '='
        $varName  = [uri]::UnescapeDataString($kv[0]).Trim()
        $varValue = [uri]::UnescapeDataString($kv[1])
        New-Variable -Name $varname -Value $varValue -Force
    }
}
else {
    Write-Warning "No query string found as part of the given URL"
}
通过将新创建的变量写入控制台来证明这一点

Write-Host "`$id = $id"
Write-Host "`$param2 = $param2"
Write-Host "`$param3 = $param3"
在本例中,哪个将打印

然而,我个人不想创建这样的变量,因为有覆盖现有变量的风险。 我认为最好将它们存储在如下哈希中:

# detect variable names and their values in the query string
# and store them in a Hashtable
$queryHash = @{}
foreach ($q in ($query -split '&')) {
    $kv = $($q + '=') -split '='
    $name = [uri]::UnescapeDataString($kv[0]).Trim()
    $queryHash[$name] = [uri]::UnescapeDataString($kv[1])
}
$queryHash
哪个输出


几乎可以工作。除了以下几点之外:$id没有填充(空),$param2的输出是$param2='@@*'而不是'@$$'。如果我运行两次$param2输出是$param2='@$param3,末尾没有单引号。[Code]Write Host“$id=$id”Write Host”$param2=$param2“Write Host”`$param3=$param3=$param3“$id=$param2='@$param3$param3='这是一个测试!'[/CODE]我已经编辑了我的答案。第一个答案有一个输入错误,但主要问题是示例url无效。请同时查看注释。几乎有效。除了以下内容:$id未填充(空),并且$param2的输出是$param2='@@*',而不是'@$$'。如果我运行两次$param2,则$param2的输出是$param2='@$param3,末尾没有单引号。[Code]Write Host“$id=$id”Write Host”$param2=$param2”Write Host“`$param3=$param3”$id=$param2='@$param3$param3='这是一个测试!'[/Code]我已经编辑了我的答案。第一个答案中有一个输入错误,但主要问题是示例url无效。请参阅注释。