i want to execute a .ps1 powershell script in red hat linux server

匿名 (未验证) 提交于 2019-12-03 01:38:01

问题:

i am having a .ps1 powershell script which executes in window, but my whole data is in linux server, is there any possible way via which i can execute the powershell script in red hat server

the powershell script is :

Clear-Host $path="D:\Deep Backup 26-04-2013\New folder" $systemname=Read-Host 'Enter System Name'    $files=Get-ChildItem $path -Recurse -Force -Include *_Registrar.zip*,*.reg.zip*   $counter=1  foreach($file in $files) {     $name=$file.name     [void][system.reflection.Assembly]::LoadFrom("C:\Program Files\MySQL\MySQL Connector Net 6.5.4\Assemblies\v2.0\MySql.Data.dll")     $dbconnect=New-Object MySql.Data.MySqlClient.MySqlConnection     $dbconnect.ConnectionString="server=localhost;userid=root;password=nPr123*;database=test3;"     $dbconnect.Open()      $sql="insert into eid values('"+$name + "','"+$systemname+"')"       $command=new-object MySql.Data.MySqlClient.MySqlCommand($sql,$dbconnect)     $command.ExecuteNonQuery()   }  $sql="insert into eid_unique        select distinct Packet_name, System_name from eid a        where not exists (select 1 from eid_unique b        where a.Packet_name=b.Packet_name);"       $command=new-object MySql.Data.MySqlClient.MySqlCommand($sql,$dbconnect)     $command.ExecuteNonQuery() $dbconnect.close() 

回答1:

Pash is a cross-platform mono clone of Powershell which is under active development. You only need to download, build it with xbuild, and run it. Just like with Powershell on Windows you can execute your script with

& '/path/to/script.ps1' 


回答2:

Four years later from original question microsoft releases powershell for linux. And it's opensource: https://github.com/PowerShell/PowerShell

In linux environment you can use syntax like this: (script.ps1, executable)

#!/usr/bin/powershell -Command  write-host -fore Green "executing PowerShell!"; 


回答3:

It sounds like Pash may work for you.



回答4:

After installing powershell, easy enough:

thufir@dur:~/powershell$  thufir@dur:~/powershell$ ./hello_world.ps1 hello world done thufir@dur:~/powershell$  thufir@dur:~/powershell$ cat hello_world.ps1  #!/usr/bin/pwsh -Command   echo "hello world" "done"   thufir@dur:~/powershell$  

Not sure why it's pwsh and not powershell, but that's Linux for you.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!