我正在提取端点上炭黑的CLI密码。我有一个片段,可从服务器提取密码并将其放入变量中。
稍后,我需要将此包含密码的变量传递给命令,但是该命令没有正确传递(命令无关)。
我用系统中的实际值对一个变量进行了硬编码,然后将其运行到我的命令中,它可以很好地工作。
因此,简而言之,从服务器片段中提取的密码不等于硬编码变量,但它们看起来相同并且都是字符串。
关于如何将提取的密码转换为正确格式的任何提示?
我检查了gettype()。fullname,它们都是相同的格式。
$resource = "SEOMRESORUCE" + $Computer
$url = "SOMEEML SOURCE"
$configlist = "C:\Temp\configlistwithdeleteditems.xml"
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
# download the CL file
Write-Host "Finding CLI Password"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $configlist)
$webResponsey = [System.Net.WebRequest]::Create($resource)
$webResponsey.Method = "GET"
$webResponsey.ContentType='application/json; charset=utf-8'
$webResponsey.Headers.Add("X-Auth-Token", "SOMETOKEN")
$Response= $webResponsey.GetResponse()
$ResponseStream = $Response.GetResponseStream()
$ReadStream = New-Object System.IO.StreamReader $ResponseStream
$jsonresult = $ReadStream.ReadToEnd()
$saveme = $jsonresult | Out-String
$cliPassword = $saveme -replace '.*CLIPassword":"([^"]*)".*', '$1'
Write-Host "CLI password is " $cliPassword #this provides the same value as $clipassword2
$cliPassword2 = 'TAWR-SOWR-POYS-YAEZ'
$cliPassword -eq $cliPassword2 #this equals False for whatever reason
答案 0 :(得分:0)
您只需修改您的正则表达式即可获得所需的结果:
$cliPassword = $saveme -replace '(?s).*CLIPassword":"([^"]*)".*', '$1'
您需要单行修饰符((?s)
),因为从json到字符串的转换将包括换行和/或回车符。默认情况下,正则表达式.
字符与换行符不匹配。如果没有单行修饰符,则-Replace
运算符将只匹配带有CLIPassword文本的行。不匹配的行仍将输出。