Running Powershell, VBScript, or bat files via NSH

This topic was edited by a BMC Contributor and has not been approved.  More information.

Contributor content

This topic was created by a BMC Contributor and has not been approved. More information.

This page describes how to run various third-party Windows scripts via Network Shell.

PowerShell

Before you begin, remember to lower your security to run scripts (if you need something more than a command):

  1. In PowerShell, type the following:

    Set-ExecutionPolicy RemoteSigned


    This command enables you to run scripts, but it does not open the server up to scripts downloaded from the network or internet.

  2. Create a new NSH Script Job

    nexec -i -e cmd /c "echo . | powershell C:\\tmp\\a.ps1"
    or alternatively:
    nexec -i -e cmd /c "powershell -inputformat none c:\\tmp\\a.ps1"


  3. Powershell sample code

    echo "hello world"

Executing the powershell script on the fly from a .BAT Script

This alternative method executes the script on-the-fly and has the NSH script job as a Type 3 script with the Copy and execute the script separately against each host option (for non NSH Scripts, see Script Options). The problem here is that Windows can only execute .bat scripts. The solution is to embed the powershell script into a BAT file. The following script uses a one liner that will execute the remaining lines as a powershell script:

@findstr/v "^@f.*&" "%~f0"|powershell -&goto:eof
Write-output "Hello World"
Write-output "Hello some&com & again"

Substitute lines 2 and 3 for the powershell script that you want to execute.

VBScript/JScript

nexec \-i \-e cmd /c "cscript //nologo C:/tmp/a.js"

JavaScript sample code

WScript.StdOut.Write("Hello World")

psexec

Background: Some windows commands need to be run as "domain user", such as the command for opening an AD account.

psexec -accepteula -u ccic/yang_admin -p password
bl-windb ipconfig /all

Run as an NSH Script Job

nexec -i -e cmd /c "psexec -accepteula -u ccic/yang_admin -p password \\\\bl-windb cscript c:/tmp/a.js"

Creating the 3rd party script on the fly from inside a NSH Script

Instead of having to maintain a separate file for the third-party (such as powershell or vbs) script, you can create the script on the file directly from an nsh script job. Create an NSH Script such as the following:

#!/bin/nsh

FULL_PATH_THIS_FILE="$0"
THIS_FILE=`basename "${FULL_PATH_THIS_FILE}"`

# Find location of embedded script
LENGTH_OF_MAIN_SCRIPT=`cat "$FULL_PATH_THIS_FILE" | grep -n '### END OF MAIN SCRIPT ###' | grep -v grep | cut -d ':' -f1`

# Increment this by one to move past the line identifier
LENGTH_OF_MAIN_SCRIPT=`expr $LENGTH_OF_MAIN_SCRIPT + 1`

# Extract Script
tail +$LENGTH_OF_MAIN_SCRIPT "$FULL_PATH_THIS_FILE" > "/tmp/$$"

for target in ${@}
	do
	cp /tmp/$$ //${target}/tmp/$$.vbs
	nexec -i -l ${target} cmd /c "cscript /nolog c:\\\tmp\\\$$.vbs"
	rm -f //${target}/tmp/$$.vbs
done
rm -f /tmp/$$
exit 0

### END OF MAIN SCRIPT ###
PUT SOME CODE HERE

Put your code after END OF MAIN SCRIPT. Swap out the nexec line for one of the lines above.

Running a bat file with Network Shell

You can use Network Shell to call a bat file by using the following command:

nexec <hostname> cmd /c "<batchFilePath>"

Where,

  • <hostname> is the IP address of the host machine
  • <batchFilePath> is the Windows file path of the batch file

For example:

nexec 10.20.47.91 cmd /c "c:\\cli\\test.bat"


Was this page helpful? Yes No Submitting... Thank you

Comments