BSA Network Shell系列-通过NSH执行Powershell,VBScript或bat files脚本

时间:2023-03-09 04:27:27
BSA Network Shell系列-通过NSH执行Powershell,VBScript或bat files脚本

参考:Running Powershell, VBScript, or bat files via NSH

如果你直接在NSH命令行执行的话,可以参考我翻译的下面的东东,如果想运行NSH 脚本作业的话,可以参考上面链接。

一步步来

## 1 执行Powershell脚本

1.1 首先需要在远程机上修改Powershell的执行策略,使远程机可以运行受信任的脚本

打开Powershell,执行:Set-ExecutionPolicy RemoteSigned

1.2 创建Powershell脚本

假设脚本名为a.ps1,位于c:/tmp/路径下,脚本内容为:echo "hello baby"

1.3 执行NSH命令运行脚本

此处可以使用NSH 脚本作业执行,也可以直接使用命令,命令如下:

nexec -i 10.200.108.92 cmd /c "echo . | powershell C:\\tmp\\a.ps1"

或者

nexec -i 10.200.108.92 cmd /c "powershell -inputformat none c:\\tmp\\a.ps1"

返回结果:

cloudexpress# nexec -i 10.200.108.92 cmd /c "echo . | powershell C:\\a.ps1"    

hellow baby

cloudexpress#

## 2 在BAT脚本中嵌入执行Powershell脚本

当windows机器只能执行bat脚本时,可以在这里面嵌入powershell脚本来执行

@findstr/v "^@f.*&" "%~f0"|powershell -&goto:eof

Write-output "Hello World"

Write-output "Hello some&com & again"

替换第二、三行为你要执行的powershell脚本即可

解释下脚本的意思:

@:关闭本行显示

goto:eof:执行完毕后退出

%~f0:指这个批处理本身

findstr/v:则是不打印匹配到的行

## 3 执行VBScripts/JScript

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

其中a.js脚本内容为:

WScript.StdOut.Write("Hello World")

## 4 执行BAT脚本

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

其中

<hostname> is the IP address of the host machine

<batchFilePath> is the Windows file path of the batch file

例如:

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

参考:

goto :eof作用

How to run a PowerShell script within a DOS batch file

Frequently used NSH commands