WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: morenos1 on July 17, 2019, 01:36:53 PM

Title: Help invoking Powershell command.
Post by: morenos1 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.....
Title: Re: Help invoking Powershell command.
Post by: td 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.
Title: Re: Help invoking Powershell command.
Post by: stanl on July 18, 2019, 02:27:15 AM
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) 
Title: Re: Help invoking Powershell command.
Post by: morenos1 on July 18, 2019, 07:11:04 AM
You sir, are correct!

Thank You !.....
Title: Re: Help invoking Powershell command.
Post by: td 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) 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.
Title: Re: Help invoking Powershell command.
Post by: morenos1 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....
Title: Re: Help invoking Powershell command.
Post by: td 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.
Title: Re: Help invoking Powershell command.
Post by: td on July 18, 2019, 11:53:49 AM
Note: edited the second solution to remove the extra single quotes.  A blunder on my part.
Title: Re: Help invoking Powershell command.
Post by: morenos1 on July 18, 2019, 01:09:49 PM
Both options now working for me.

Much appreciated.  Thank You ....