Author Topic: Help invoking Powershell command.  (Read 180 times)

morenos1

  • Jr. Member
  • **
  • Posts: 78
Help invoking Powershell command.
« on: July 17, 2019, 01:36:53 pm »
Attempting to invoke the following Powershell command:

Set-NetAdapterAdvancedProperty eth* -DisplayName "Wait for Link" -DisplayValue "On"

Using the script:

   ;***************************************************************************
   ;**   PowerShell Invoke Command
   ;**
   ;** Purpose: Invokes PowerShell Commands
   ;** Inputs: Commands Parameters and Arguements
   ;** Outputs: Results in a Reportview
   ;** Reference:
   ;**       REQUIRES WinBatch 2013A or newer
   ;**       
   ;** Developer: Deana Falk 2013.05.06
   ;***************************************************************************
   If Version( )< '2013A'
      Pause("Notice", "Need 2013A or Newer Version of WInBatch")
      Exit
   EndIf

   ;Host applications can use the PowerShell object to run any combination of cmdlets and scripts that can be invoked at the command line.
   ;By using this object, the host application can create a pipeline, add commands and scripts to the pipeline, specify the runspace where
   ;the commands are run, and then invoke the pipeline synchronously or asynchronously. For more information about host applications, see
   ;Writing a Windows PowerShell Host Application.
;
   ;http://msdn.microsoft.com/en-us/library/ee706556(v=vs.85).aspx

   ObjectClrOption("use", "System.Management.Automation,version=1.0.0.0,publicKeyToken=31bf3856ad364e35,culture=neutral")
   objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell") ; "v4.0.30319"

   oPowerShell = objAutoPs.Create()
   oPowerShell.AddCommand("Set-NetAdapterAdvancedProperty")
   oPowerShell.AddArgument("eth*")
   oPowerShell.AddParameter("-DisplayName")
   oPowerShell.AddArgument("Wait for Link")
   oPowerShell.AddParameter("-DisplayValue")
   oPowerShell.AddArgument("On")

   oAsync = oPowerShell.BeginInvoke() ; Invoke() - invoke Does not work because can't determine which overload to use
   oPsCollection = oPowerShell.EndInvoke(oAsync)

   aRslts = ArrDimension(1024,2)
   cntr = 0
   ForEach oItem In oPsCollection
      oCast = oItem.BaseObject()
      aRslts[cntr,0] = oCast.Id
      aRslts[cntr,1] = oCast.ProcessName
      cntr = cntr+1
   Next
         
Exit

I can not get the Argument/Parameters right …

Thanks.....

td

  • Tech Support
  • *****
  • Posts: 3017
    • WinBatch
Re: Help invoking Powershell command.
« Reply #1 on: July 17, 2019, 02:56:13 pm »
This is more of a PowerShell question than a WinBatch question.  You might want to spend some time reviewing the MSFT documentation for the module:

https://docs.microsoft.com/en-us/powershell/module/netadapter/set-netadapteradvancedproperty?view=win10-ps

Another comment is you would do well to drop the "use" option and use the "useany" option instead.   Note that you would also need to remove the  ",version=1.0.0.0,publicKeyToken=31bf3856ad364e35,culture=neutral" part of your assembly description string to use "useany".  Removing the version dependency makes your script more portable across versions of Windows.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 936
Re: Help invoking Powershell command.
« Reply #2 on: July 18, 2019, 02:27:15 am »
It might be possible to avoid parameters and just run with addscript()


partial code:
Code: Winbatch

cScript='Set-NetAdapterAdvancedProperty eth* -DisplayName "Wait for Link" -DisplayValue "On"'


ObjectClrOption("useany", "System.Management.Automation")
objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell")
oPshell = objAutoPs.Create()
oScope = ObjectType("BOOL",@TRUE)
oPshell.AddScript(cScript,oScope)
objAsync = oPshell.BeginInvoke()
oPShell.EndInvoke(objAsync)  
 

morenos1

  • Jr. Member
  • **
  • Posts: 78
Re: Help invoking Powershell command.
« Reply #3 on: July 18, 2019, 07:11:04 am »
You sir, are correct!

Thank You !.....

td

  • Tech Support
  • *****
  • Posts: 3017
    • WinBatch
Re: Help invoking Powershell command.
« Reply #4 on: July 18, 2019, 08:10:43 am »
For what it's worth.  According to MSFT's Powershell documentation, the correct way to build the command on the fly is something like the following:

Code: Winbatch
oPowerShell.AddCommand("Set-NetAdapterAdvancedProperty")
oPowerShell.AddArgument("Eth*")
oPowerShell.AddParameter("DisplayName", "Wait for Link")
oPowerShell.AddParameter("DisplayValue", "On")
 

Edit: removed the extra single quotes from the AddParameter calls.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

morenos1

  • Jr. Member
  • **
  • Posts: 78
Re: Help invoking Powershell command.
« Reply #5 on: July 18, 2019, 09:33:36 am »
Syntax for the 2nd solution seems correct (no errors) but, the change never happens to the adapter.

Anyhow, the first solution works...

Thanks....

td

  • Tech Support
  • *****
  • Posts: 3017
    • WinBatch
Re: Help invoking Powershell command.
« Reply #6 on: July 18, 2019, 09:56:07 am »
On my system, actually a virtual machine, neither solution one or solution two did anything. The point was more to illustrate the proper use of the AddArgument and AddParamter methods of the PowerShell object.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

  • Tech Support
  • *****
  • Posts: 3017
    • WinBatch
Re: Help invoking Powershell command.
« Reply #7 on: July 18, 2019, 11:53:49 am »
Note: edited the second solution to remove the extra single quotes.  A blunder on my part.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

morenos1

  • Jr. Member
  • **
  • Posts: 78
Re: Help invoking Powershell command.
« Reply #8 on: July 18, 2019, 01:09:49 pm »
Both options now working for me.

Much appreciated.  Thank You ….