trim() 不适用于许可证密钥工具
trim() not working for license key tool
PS 的个人技能水平:低(学生)
目标:
尝试找到 Windows 10 许可证密钥,将其显示在 window 中并自动将其复制到剪贴板。
问题:
我的输出总是在字符串的开头和结尾处产生空格。我尝试了 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 = $License.Trim("OA3xOriginalProductKey")
现在让我们尝试通过设计一种模式只保留我们想要的内容,只保留数字、大写字符和“-”。另外,我们需要删除多余的行。
$pattern = '[^0-9A-Z-]'
$Result = ($Result -replace $pattern, '').trim() -ne ""
现在你可以查看结果了,不需要的东西应该都没有了
$Result = "|" + $Result + "|"
$Result
PS 的个人技能水平:低(学生)
目标: 尝试找到 Windows 10 许可证密钥,将其显示在 window 中并自动将其复制到剪贴板。
问题: 我的输出总是在字符串的开头和结尾处产生空格。我尝试了 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 = $License.Trim("OA3xOriginalProductKey")
现在让我们尝试通过设计一种模式只保留我们想要的内容,只保留数字、大写字符和“-”。另外,我们需要删除多余的行。
$pattern = '[^0-9A-Z-]'
$Result = ($Result -replace $pattern, '').trim() -ne ""
现在你可以查看结果了,不需要的东西应该都没有了
$Result = "|" + $Result + "|"
$Result