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
}
结果:
-| ****-*****-*****-*****-***** |-
答案 0 :(得分:0)
似乎你已经用这个
取出了你不想要的文字$Result = $License.Trim("OA3xOriginalProductKey")
现在让我们尝试只保留我们想要的内容,设计一个模式,只保留数字,大写字母和“ - ”。此外,我们需要删除额外的行。
$pattern = '[^0-9A-Z-]'
$Result = ($Result -replace $pattern, '').trim() -ne ""
现在你可以查看结果,不需要的一切都应该消失
$Result = "|" + $Result + "|"
$Result