Help invoking Powershell command.

Started by morenos1, July 17, 2019, 01:36:53 PM

Previous topic - Next topic

morenos1

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

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

It might be possible to avoid parameters and just run with addscript()


partial code:
Code (WINBATCH) Select


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

You sir, are correct!

Thank You !.....

td

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) Select
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

Syntax for the 2nd solution seems correct (no errors) but, the change never happens to the adapter.

Anyhow, the first solution works...

Thanks....

td

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

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

Both options now working for me.

Much appreciated.  Thank You ....