![]() ![]() ![]() The next step is to return the response in the case where one is generated. Now that we know the basics, let’s look at how we could then return the response from PowerShell commands. 'Execute and capture the returned value CreateObject( "WScript.Shell").Run sPSCmd, 0, True End SubĬreateObject("WScript.Shell").Run sPSCmd, 0, True Run we can silently run PowerShell commands. Luckily, there a a small change we can make to avoid this. The issue with the above is that it generates a visible PowerShell window that the end-user will see. Public Sub PS_Execute(ByVal sPSCmd As String)ĬreateObject("WScript.Shell").Exec (sPSCmd) ' PS_Execute "New-Item -Path 'C:\temp\charts\Test\1\2\3\4' -ItemType Directory" ' Can do multiple directories in one call! ' PS_Execute "New-Item -Path 'C:\temp\charts\Test' -ItemType Directory" ' PS_Execute "Copy-Item -Path C:\temp\Book1.xls -Destination C:\temp\charts\Book1.xls -Force" ' Req'd Refs: Late Binding -> none required ' Copyright : The following is release as Attribution-ShareAlike 4.0 International ' Author : Daniel Pineault, CARDA Consultants Inc. 'Execute and capture the returned value CreateObject( "WScript.Shell").Exec (sPSCmd) ' Website : ' Purpose : Run a PowerShell command ' Copyright : The following is release as Attribution-ShareAlike 4.0 International ' (CC BY-SA 4.0) - ' Req'd Refs: Late Binding -> none required ' ' Input Variables: ' ~~~~~~~~~~~~~~~~ ' sPSCmd : PowerShell command to run ' ' Usage: ' ~~~~~~ ' Copy a File ' PS_Execute "Copy-Item -Path C:\temp\Book1.xls -Destination C:\temp\charts\Book1.xls -Force" ' Create a Directory or Directories ' PS_Execute "New-Item -Path 'C:\temp\charts\Test' -ItemType Directory" ' Can do multiple directories in one call! ' PS_Execute "New-Item -Path 'C:\temp\charts\Test\1\2\3\4' -ItemType Directory" ' ' Revision History: ' Rev Date(yyyy-mm-dd) Description ' ************************************************************************************** ' 1 Initial Release '- Public Sub PS_Execute( ByVal sPSCmd As String) '- ' Procedure : PS_Execute ' Author : Daniel Pineault, CARDA Consultants Inc. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |