none
run ps1 as admin and keep window open

    Question

  • Hello

    I've been trying lots of different things to do this.

    I want another admin to simply double click the .ps1 file and have it show the Original Product key for that box.

    (Get-WmiObject -query 'select * from SoftwareLicensingService').OA3xOriginalProductKey

    So, I tried from a cmd file - executing ps as an admin using the runAs.

    powershell.exe "Start-Process powershell -ArgumentList '-ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -File c:\myscript.ps1' -Verb RunAs"
    Start-Process powershell.exe -ArgumentList "-noexit", "-NoProfile", "{(Get-WmiObject -query 'select * from SoftwareLicensingService').OA3xOriginalProductKey}"
    But what happens is it opens powershell, runs the command and closes.  So the admin does not get to view the original product key so that he can copy it and paste it in for Activation (W10 pro).

    I can't seem to combine the runas and -noexit in the same command,; which I think needs to happen.

    Any suggestions?

    Terry



    Thursday, January 10, 2019 10:03 PM

Answers

  • This is how to do what you are trying to do:

    $command = {
        if($key=(Get-WmiObject -query 'select * from SoftwareLicensingService').OA3xOriginalProductKey){
            $key
        } else {
            'Use this Product Key -- xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'
        }
    }
    Start-Process powershell.exe -ArgumentList "-noexit -noprofile  -command $command"
    


    \_(ツ)_/

    • Marked as answer by terryvanduzee Friday, January 11, 2019 7:13 PM
    Friday, January 11, 2019 6:06 PM
    Moderator

All replies

  • Add "pause" to the end of the script.


    \_(ツ)_/

    Thursday, January 10, 2019 11:12 PM
    Moderator
  • try putting this "Read-Host" as the last line in your myscript.ps1

    Read-Host 'Press Enter to continue…' | Out-Null

    Example:

    (Get-WmiObject -query 'select * from SoftwareLicensingService').OA3xOriginalProductKey
    Read-Host 'Press Enter to continue…' | Out-Null

    Friday, January 11, 2019 1:31 AM
  • Hi,

    Thanks for your question.

    Maybe you can try to output the original product key to a text file. Then you can copy it and paste it.

    (Get-WmiObject -query 'select * from SoftwareLicensingService').OA3xOriginalProductKey | Out-File c:\key.txt

    Best Regards,

    Lee


    Just do it.

    Friday, January 11, 2019 2:19 AM
    Moderator
  • Using the Out-file is working:  Thank You.

    But I have noticed that if the computer did not come with the bios key, I would like to preset our Volume Key to the tech to activate with.

    Running from a cmd file:

    powershell.exe -noprofile -command "&{$A ="Use this Product Key -- xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"
    $B = (Get-WmiObject -query 'select * from SoftwareLicensingService').OA3xOriginalProductKey
    if($B) {$B | Out-file $Env:UserProfile\Desktop\key.txt -force} 
    Else {A$ | Out-file $Env:UserProfile\Desktop\Key.txt -force}}"

    If I simply run the powershell portion of the command in ISE, it works.  But when I put the wrapper around it, the cmd windows flashes and disappears and no txt file is created.

    I'm not sure what I'm not including or what I am including that makes it fail.

    Any suggestions?

    Thank You

    Terry




    • Edited by terryvanduzee Friday, January 11, 2019 5:50 PM copied incomplete text
    Friday, January 11, 2019 5:47 PM
  • If you had asked the full question to begin with we could have given you the correct an swer:

    powershell.exe  -NOEXIT -noprofile -command

    Now the window will persist and you will see your error.


    \_(ツ)_/

    Friday, January 11, 2019 5:55 PM
    Moderator
  • This is how to do what you are trying to do:

    $command = {
        if($key=(Get-WmiObject -query 'select * from SoftwareLicensingService').OA3xOriginalProductKey){
            $key
        } else {
            'Use this Product Key -- xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'
        }
    }
    Start-Process powershell.exe -ArgumentList "-noexit -noprofile  -command $command"
    


    \_(ツ)_/

    • Marked as answer by terryvanduzee Friday, January 11, 2019 7:13 PM
    Friday, January 11, 2019 6:06 PM
    Moderator
  • Thank you for the quick reply :)  Yes, the -NoExit works  (I had used this before, but I think I missed putting a space after it, so it wasn't recognized.  The "If" statement was an after thought just incase there wasn't an original key.  Sorry for the misleading info...

    It says:

    Missing closing '}' in statement block or type definition.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingEndCurlyBracen

    I can't seem to pinpoint where the bracket is missing

    New Scirpt:

    powershell.exe -NoExit -noprofile -command "&{$A ='Use this Product Key -- xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'
    $B = (Get-WmiObject -query 'select * from SoftwareLicensingService').OA3xOriginalProductKey
    if($B) {$B | Out-file $Env:UserProfile\Desktop\key.txt -force} 
    Else {$A | Out-file $Env:UserProfile\Desktop\Key.txt -force}}"


    Friday, January 11, 2019 6:09 PM
  • Please use the code I posted above.  It has been tested.


    \_(ツ)_/

    Friday, January 11, 2019 6:20 PM
    Moderator
  • This worked perfectly,

    Thank You so much !!

    Terry

    Friday, January 11, 2019 7:13 PM
  • This is how I do it.  Saves confusion:

    $msg = if($key = (Get-WmiObject -query 'select * from SoftwareLicensingService').OA3xOriginalProductKey){
        $key
    } else {
        'Use this Product Key -- xxxxx-xxxxx-xxxxx-xxxxx-xxxxx'
    }
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Clipboard]::SetText($msg)
    [System.Windows.Forms.MessageBox]::Show("The product key has been copied to your clipboard`n`n$msg")


    \_(ツ)_/




    Friday, January 11, 2019 7:36 PM
    Moderator