`

hinemos执行windows下的powershell脚本

阅读更多

目的:

通过hinemos执行windows下的脚本

 

过程:

创建powershell脚本

以前也没有写过windows下的脚本,第一次写powshell的脚本,也是折腾了一会。

网上翻了几篇文章后,发现其实在powshell的命令行下和在cmd的命令行下,还是挺像的。

比如:

切换到D盘下⇒D:

生成一个powershell文件,并向改文件输入字符串⇒'"hello powershell!"'>test.ps1

执行powershell文件⇒.\test.ps1⇒这样hello powershell!就会被打印出来

 

但是一般情况下,第一次都会执行不成功过,我就碰到了。因为Powershell一般初始化情况下都会禁止脚本执行。脚本能否执行取决于Powershell的执行策略。

.\test.ps1执行上面的命令时,显示无法加载脚本,因为在此系统中禁止执行脚本。有关详细信息,请参阅 "get-help about_signing"。

所在位置 行:1 字符: 15
+ .MyScript.ps1 < <<<
+ CategoryInfo : NotSpecified: (:) [], PSSecurityException
+ FullyQualifiedErrorId : RuntimeException

那么这个时候就需要更改策略,记得只有管理员才有权限更改这个策略。非管理员会报错。

更改策略步骤:

1.查看策略⇒Get-ExecutionPolicy

2.Restricted修改策略⇒Set-ExecutionPolicy UnRestricted

Unrestricted:权限最高,可以不受限制执行任何脚本。

修改完成后再去执行,就没有问题了。

为了很明显的证明从Hinemos中成功执行了windows上的powershell脚本,决定写一个关于“创建文件”的脚本。

命令如下:

1.将创建文件的脚本写入test.ps1,'New-Item C:\Users\Administrator\test.txt -type file' > test.ps1

2.通过dir命令发现在C:\Users\Administrator\目录下确实生成了test.ps1,

3.执行脚本./test.ps1,确实在C:\Users\Administrator\下也生成了test.txt文件

4.删除test.txt文件rm test.txt

 

这样就成功创建了一个“创建文件”的powershell文件,接下来在hinemos配置。

 

配置Hinemos job

如何在hinemos下通过job的功能执行该脚本呢。其实很简单,和在Linux下执行shell差不多

在command tab下的启动command里写上powershell -Command C:\Users\Administrator\test.ps1

 

结果

执行job后,job的状态是正常的,再去实际windows机器里的,C:\Users\Administrator\目录下,生成了test.txt文件。那么确实job是被正常执行了。

 
 
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics