简体   繁体   English

使用ISE或Powershell.exe时的字符串长度不同

[英]String Length different when using ISE or Powershell.exe

I have written a small user Interface with PowerShell in order to create checksums comfortable. 我使用PowerShell编写了一个小的用户界面,以创建舒适的校验和。 At the moment I see a difference between using the ISE and the powershell itself. 目前,我看到使用ISE和Powershell本身之间的区别。

When running the PowerShell script within the ISE I get perfect results. 在ISE中运行PowerShell脚本时,我得到了完美的结果。 This is the code I am using (Just a snippet): 这是我正在使用的代码(只是一个片段):

$aaa = (Get-FileHash -Algorithm SHA512 -Path $str_filepath |
       Select-Object -Property hash |
       Format-Table -HideTableHeaders |
       Out-string).TrimEnd().TrimStart()

So I create a checksum (SHA512) from a file. 所以我从文件创建一个校验和(SHA512)。 These are the results for one file as example: 这些是一个文件的结果,例如:

Running the script in ISE: 在ISE中运行脚本:

0518E6DF62AB7B8D7A238039262C7A0E9F1F457D514EDE2BB8B3F4719340EF4B61053EC85ED30D07688B447DBC756F3A7455D7E0C84C7BCF62A8884E4715C8A0

Running the script in PowerShell: 在PowerShell中运行脚本:

0518E6DF62AB7B8D7A238039262C7A0E9F1F457D514EDE2BB8B3F4719340EF4B61053EC85ED30D07688B447DBC756F3A7455D7E0C84C7BCF62A8...

As you can see the string is shortend when using PowerShell. 如您所见,使用PowerShell时字符串会缩短。 More confusing is that the shortening is not consistent. 更令人困惑的是,缩短不是一致的。 At home on my Windows 7 machine the string is even shorter then on my Windows 8.1 System at work. 在家里的Windows 7计算机上,字符串比在工作的Windows 8.1系统上的字符串还要短。 I know that there are some differences between ISE and PowerShell when running scripts regarding to styles. 我知道运行有关样式的脚本时,ISE和PowerShell之间存在一些差异。 But shorter strings... Hmm. 但是较短的弦...嗯。

So now the question. 现在是问题了。 Does anyone of you have expierenced that difference between ISE and Powershell regarding to String length limitations? 在字符串长度限制方面,您有没有遇到过ISE和Powershell之间的区别? And if so. 如果是这样。 Does have anyone an answer for me how I can script it that there will be no different string results? 有没有人为我提供答案,我该如何编写脚本以确保不会出现不同的字符串结果?

Do not use Format-* cmdlets if you need to process your data further. 如果您需要进一步处理数据, Format-*不要使用Format-* cmdlet。 Those cmdlets are only for displaying data to a user. 这些cmdlet 用于向用户显示数据。 Instead expand the Hash property of the object Get-FileHash returns, either like this: 而是扩展对象Get-FileHash返回的Hash属性,如下所示:

$str_checksum_sha512 = (Get-FileHash -Algorithm SHA512 -Path $str_filepath).Hash

or like this: 或像这样:

$str_checksum_sha512 = Get-FileHash -Algorithm SHA512 -Path $str_filepath |
                       Select-Object -Expand Hash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM