【发布时间】:2016-05-24 23:04:18
【问题描述】:
PS 个人技能水平:低(学生)
目标: 尝试查找 Windows 10 许可证密钥,将其显示在窗口中并自动将其复制到剪贴板。
问题: 我的输出总是在字符串的开头和结尾产生空格。我已经尝试过 trim()、trimstart() 等。到目前为止没有任何效果。我不会介意空格然后用剪贴板的键保存 - 使功能有点无用,或者至少很乏味。
我在代码中列出了一些符号来识别问题。
代码(结果如下):
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
#Obtain Windows 10 Key
$License = wmic path SoftwareLicensingService get OA3xOriginalProductKey
# FAULT - Used to trim key
$Result = $License.Trim("OA3xOriginalProductKey")
#Used to isolate the spaces in the result. Attempt to see if the spaces occur after the trim, or during the trim.
#The spaces are within the "|" rather than outside of the "|" - meaning that the Trim() is not working properly.
#PLEASE HELP!
$Result = "|" + $Result.Trim() + "|"
#Used to copy key to clipboard
$Result | clip.exe
$form = New-Object System.Windows.Forms.Form
$form.Text = "KeySniffer 1.0"
$form.Size = New-Object System.Drawing.Size(280,140)
$form.StartPosition = "CenterScreen"
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(30,70)
$OKButton.Size = New-Object System.Drawing.Size(60,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = "Key has been copied to clipboard"
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
#Put a trim here as a desperate swing in the dark
#The "-" indicate that the spaces occur within the "$Result"
$textBox.Text = "-" + $Result.Trim() + "-"
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(200,20)
$form.Controls.Add($textBox)
$form.Topmost = $True
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $textBox.Text
$x
}
结果:
-| ****-*****-*****-*****-***** |-
【问题讨论】:
-
你确定它们是空格字符吗?不要添加
-或|,而是查看实际字符:$Result.ToCharArray() | % { "'$_' = $([System.Convert]::ToUInt32($_))" }
标签: powershell trim powershell-5.0