Running Powershell, VBScript, or bat files via NSH
This page describes how to run various third-party Windows scripts via Network Shell.
- PowerShell
- Executing the powershell script on the fly from a .BAT Script
- VBScript/JScript
- psexec
- Creating the 3rd party script on the fly from inside a NSH Script
- Running a bat file with Network ShellPublished
PowerShell
Before you begin, remember to lower your security to run scripts (if you need something more than a command):
In PowerShell, type the following:
Set-ExecutionPolicy RemoteSignedThis command enables you to run scripts, but it does not open the server up to scripts downloaded from the network or internet.
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"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:
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
JavaScript sample code
psexec
Background: Some windows commands need to be run as "domain user", such as the command for opening an AD account.
bl-windb ipconfig /all
Run as an NSH Script Job
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:
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 ShellPublished
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: