-NoNewLine 在 Powershell 中没有按预期工作

-NoNewLine Not working as expected in Powershell

我正在使用 powershell 连接一个键,但 Writehost 在使用 -NoNewLine 时给我一个回车分隔符和 3 行而不是一行。

我尝试了 -NoNewLine 以及各种其他代码集,例如

write-host -replace "[`r`n](-join(

承载 eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEw ejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05C cUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t PeIoyImIORI59k0Ee_Ay4jlSpiPT_FEdlln_ZWfcEDCPQ

我也一直在搜索 Stack overflow,每个人都说没有换行应该是答案

$assoc1 ="Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEw" 
$assoc2 ="ejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05C" 
$assoc3 ="cUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t"


$apikey = ($assoc1,$assoc2,$assoc3)


write-host $apikey  -NoNewLine

我希望这是 运行 结果是一行

"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEwejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05CcUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t"

相反,结果集给了我 3 行代码而不是 1 行。这是对 powershell 的限制吗?

enter image description here

你可以加入他们,这样...

$assoc1,$assoc2,$assoc3 -join ''

虽然这将在屏幕上显示为 3 行,但由于屏幕格式,它不是。

($assoc1 ="Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEw" ).Length
($assoc2 ="ejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05C" ).Length
($assoc3 ="cUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t").Length

($apikey = $assoc1,$assoc2,$assoc3 -join '').Length

# Results 
220
220
220
660

$apikey.Count
1

$apikey | clip

如果您将此粘贴到记事本中,而不打开 'Word Wrap',您会看到它是一行。

Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5VTTBSRFE1UVROQk9FRTJNREJEUWtGR1JEazFNRGN4TlRReU9FUXpOalZFT0RZeVJEQXpSZyJ9.eyJpc3MiOiJodHRwczovL2lkZW50aXR5LmF1dGgwLWRldi5zcy5hd3MuYmNpZGFoby5jb20vIiwic3ViIjoidHFYQjEwejVUT0ZvZ05rNDAzQmZYZkhrTkJxSVJ0UENAY2xpZW50cyIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkuYXV0aDAtZGV2LnNzLmF3cy5iY2lkYWhvLmNvbS9hcGkvdjIvIiwiaWF0IjoxNTUzNTI2MDk0LCJleHAiOjE1NTM2MTI0OTQsImF6cCI6InRxWEIxMHo1VE9Gb2dOazQwM0JmWGZIa05CcUlSdFBDIiwic2NvcGUiOiJyZWFkOmNsaWVudF9ncmFudHMgY3JlYXRlOmNsaWVudF9ncmFudHMgZGVsZXRlOmNsaWVudF9ncmFudHMgdXBkYXRlOmNsaWVudF9ncmFudHMgcmVhZDp1c2VycyB1cGRhdGU6dXNlcnMgZGVsZXRlOnVzZXJzIGNyZWF0ZTp1c2VycyByZWFkOnVzZXJzX2FwcF9t

如果您希望 不带分隔符 连接 3 个 字符串,即直接连接,使用-join 运算符的一元形式:

$apikey = -join $assoc1, $assoc2, $assoc3

如果你想输出单个字符串没有尾随换行符:

  • 仅打印到控制台,您可以使用Write-Host -NoNewline $apikey

    • 请注意,作为控制台中显示的伪像,您的单行字符串可能仍会跨多行显示,具体取决于window / 你控制台的缓冲区宽度 window.
  • 要保存到文件,请使用 Set-Content -NoNewlineOut-File -NoNewline (PSv5+)。