locked
Help with PS Script RRS feed

  • Question

  • Hello Guys,

    We have a requirement to pull the MS Office versions and the keys used in our environment and i am having hard time in pulling the info.

    Here's my script:

    $computers = gc "C:\computer.txt"
    foreach($computer in $computers )
    {
    Invoke-Command -comp name -script {$env:computername}
    cscript "C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS" /dstatus
    }

    cscript "C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS" /dstatus - is used to pull the last 5digits of the key

    But when i run the script it only displays the office key from my local machine, for all the supplied machines.

    Can anyone please help.


    Regards, Prasad

    Tuesday, January 7, 2020 7:36 AM

Answers

  • Yes, i don't have any experience or training in powershell, that's the reason why i'm asking for help!

    And i used ScritpBlock itselft to execute the VB script not the filepath! its shown in my comment above.

    Invoke-Command -ComputerName SYD-L9180.corp.com -ScriptBlock {cscript "C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS" /dstatus}

    Is this the appropriate way to use it?


    Regards, Prasad


    This would be the correct method to execute a remote VBS script.

    A cleaner way would be to do this:

    $sb = {
        cscript "C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS" /dstatus
    }
    Invoke-Command -ScriptBlock $sb -ComputerName SYD-L9180.corp.com
    This makes the code easier to edit and read.


    \_(ツ)_/

    • Marked as answer by RealStruggler Thursday, July 2, 2020 7:24 AM
    Tuesday, January 7, 2020 4:53 PM

All replies

  • Whenever you wan to use a new command I recommend reading the complete help first.

    help invoke-command -online


    \_(ツ)_/

    Tuesday, January 7, 2020 7:41 AM
  • Hi Jrv,

    I tried this, i couldn't understand much from help as i'm new to powershell

    Invoke-Command -scriptBlock {cscript "C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS" /dstatus}

    But for the provided list of computers, it keeps displaying results from my local machine.

    Can you please advice.


    Regards, Prasad

    Tuesday, January 7, 2020 8:48 AM
  • Please read the help again until you understand what it is telling you. You found one of your many mistakes. Stop guessing. The help has exp[licit examples. You must learn any technology before attempting to use it. Don't make others read the documentation to you.


    \_(ツ)_/

    Tuesday, January 7, 2020 8:58 AM
  • Okay thanks for your advice!

    Regards, Prasad

    Tuesday, January 7, 2020 9:03 AM
  • Invoke-Command -ComputerName (Get-Content Servers.txt) -FilePath C:\Scripts\Sample.ps1 -ArgumentList Process, Service

    So i tried this, it says cannot validate argument on parameter 'ComputerName'. The argument is null or empty.

    Invoke-Command -ComputerName SYD-L9180.corp.com -ScriptBlock {cscript "C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS" /dstatus}

    Can you let me know if this single command is correct?


    Regards, Prasad

    Tuesday, January 7, 2020 9:29 AM
  • no. If you read the help you will see that the "FilePath" parameter is wrong for a remote command that executes a VB script.

    You must use a ScriptBlock.

    Most of your problem is that you don't have any experience or training in PowerShell.

    Learning to script properly with PowerShell


    \_(ツ)_/

    Tuesday, January 7, 2020 9:49 AM
  • Yes, i don't have any experience or training in powershell, that's the reason why i'm asking for help!

    And i used ScritpBlock itselft to execute the VB script not the filepath! its shown in my comment above.

    Invoke-Command -ComputerName SYD-L9180.corp.com -ScriptBlock {cscript "C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS" /dstatus}

    Is this the appropriate way to use it?


    Regards, Prasad


    Tuesday, January 7, 2020 10:36 AM
  • Or this

    Invoke-Command -ComputerName (Get-Content C:\computer.txt) -ScriptBlock{C:\Scripts\Sample.ps1 -ArgumentList Process, Service}


    Regards, Prasad

    Tuesday, January 7, 2020 10:39 AM
  • Yes, i don't have any experience or training in powershell, that's the reason why i'm asking for help!

    And i used ScritpBlock itselft to execute the VB script not the filepath! its shown in my comment above.

    Invoke-Command -ComputerName SYD-L9180.corp.com -ScriptBlock {cscript "C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS" /dstatus}

    Is this the appropriate way to use it?


    Regards, Prasad


    This would be the correct method to execute a remote VBS script.

    A cleaner way would be to do this:

    $sb = {
        cscript "C:\Program Files (x86)\Microsoft Office\Office16\OSPP.VBS" /dstatus
    }
    Invoke-Command -ScriptBlock $sb -ComputerName SYD-L9180.corp.com
    This makes the code easier to edit and read.


    \_(ツ)_/

    • Marked as answer by RealStruggler Thursday, July 2, 2020 7:24 AM
    Tuesday, January 7, 2020 4:53 PM