Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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 奇怪的字符串问题_Powershell - Fatal编程技术网

Powershell 奇怪的字符串问题

Powershell 奇怪的字符串问题,powershell,Powershell,我有一个很奇怪的问题,很难描述,所以请容忍我 我有一个变量$BestHost,它只包含windows服务器的名称。我将此变量传递给我编写的名为Get CurrentSite的模块,该模块执行一些测试以检查服务器所在的物理位置 在测试中,我手动设置 PS C:\Windows\system32> $Besthost = "WK-VPS-009" PS C:\Windows\system32> Get-CurrentSite -CHost $Besthost WK 并且模块调用fin

我有一个很奇怪的问题,很难描述,所以请容忍我

我有一个变量$BestHost,它只包含windows服务器的名称。我将此变量传递给我编写的名为Get CurrentSite的模块,该模块执行一些测试以检查服务器所在的物理位置

在测试中,我手动设置

PS C:\Windows\system32> $Besthost = "WK-VPS-009"

PS C:\Windows\system32> Get-CurrentSite -CHost $Besthost
WK
并且模块调用fine,给出所需的结果。服务器处于站点工作状态

在较大的脚本中,$BestHost由API调用定义,但结果是相同的。上面写着

PS C:\Windows\system32> $BestHost = ($BestHost = Invoke-WebRequest -URI "http://hypervision.host.net/api/v1/vps/best_host?exclude=$currentsiteswap,$VPShost").Content

   $BestHost= $BestHost -replace '"',"" #this just removes additional "

PS C:\Windows\system32> $BestHost
WK-VPS-009
但是,当我这次尝试将此变量与Get CurrentSite一起使用时,我得到了错误

PS C:\Windows\system32> Get-CurrentSite -CHost $BestHost
Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings.
At C:\Users\frank\Documents\WindowsPowerShell\Modules\Get-CurrentSite\Get-CurrentSite.psm1:10 char:12
+ $HostAdd = Invoke-Command -ComputerName $CHost { get-NetIPAddress}
这是Invoke命令的一个错误,我首先使用Get CurrentSite来确定站点位置

我的第一个想法是变量类型,所以

这是手动设置时的变量:

PS C:\Windows\system32> $BestHost.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                    
-------- -------- ----                                     --------                                                                                                                                    
True     True     String                                   System.Object     
这是由API调用设置的:

PS C:\Windows\system32> $BestHost.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                    
-------- -------- ----                                     --------                                                                                                                                    
True     True     String                                   System.Object                                                                                                                               
对我来说,它们看起来是包含完全相同数据的相同变量,一个有效,一个无效。我很困惑

任何帮助都将不胜感激

编辑:进一步信息

我只是把这两个结果放在两个不同的变量中,比较它们,得到以下结果

PS C:\Windows\system32> $BestHost
WK-VPS-009

PS C:\Windows\system32> $BestHost1
WK-VPS-009
PS C:\Windows\system32> $BestHost1.Equals($BestHost)
False

PS C:\Windows\system32> ($BestHost.GetEnumerator() | % ToInt32 $null | % ToString X4) -join '-'
0057-004B-002D-0056-0050-0053-002D-0030-0030-0039

PS C:\Windows\system32> ($BestHost1.GetEnumerator() | % ToInt32 $null | % ToString X4) -join '-'
0057-004B-002D-0056-0050-0053-002D-0030-0030-0039-000A-000A

看起来您的API函数以某种方式向字符串输出(000A)添加了两个换行符 在处理之前,通过向代码中添加以下内容来清理这些换行:

  $besthost = $besthost -Replace '\x0A','' 
您可以使用正则表达式(regexp)将操作与双引号(“,ascii代码x22”)的清除结合起来:


比较两种情况下的
($BestHost.GetEnumerator()|%ToInt32$null |%ToString X4)-连接“-
。好的,我会试试看,我在主要问题的末尾添加了一些信息,这也可能有助于
$BestHost1
中有两个额外的换行符。如果
.Trim()
它们。进行了修剪后,它们仍然略有不同,但仍然会出现相同的错误。字符串中有我看不到的特殊字符,通过枚举可以看到的字符,然后将其删除?是的,会调用它们。
 $besthost = $besthost -Replace '[\x0A\x22]',''