5

我有一个在窗口中执行的 .ps1 powershell 脚本,但我的整个数据都在 linux 服务器中,有什么可能的方法可以让我在 red hat 服务器中执行 powershell 脚本

powershell 脚本是:

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()
4

6 回答 6

8

四年后,从最初的问题开始,微软发布了适用于 linux 的 powershell。它是开源的:https ://github.com/PowerShell/PowerShell

在 linux 环境中,您可以使用如下语法: (script.ps1, executable)

#!/usr/bin/powershell -Command

write-host -fore Green "executing PowerShell!";
于 2017-08-03T13:59:22.687 回答
5

Pash是 Powershell 的跨平台单一克隆,正在积极开发中。您只需要下载、使用 构建它xbuild并运行它。就像在 Windows 上使用 Powershell 一样,您可以使用

& '/path/to/script.ps1'
于 2014-12-10T22:57:51.530 回答
2

安装powershell后,很简单:

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$ 

不知道为什么它是pwsh而不是powershell,但那是适合你的 Linux。

于 2018-02-15T22:13:54.970 回答
0

听起来Pash可能对你有用。

于 2013-07-20T06:52:15.687 回答
0

您还可以轻松地在 linux 上安装

snap install powershell --classic

snap“powershell”的这个修订版是使用经典限制发布的,因此可能会在 snap 通常受限的安全沙箱之外执行任意系统更改,这可能会使您的系统处于危险之中。

于 2021-03-11T02:24:02.523 回答
0

我想分享一个对我有帮助的直截了当的方法。请遵循 Microsoft 官方文档。这个问题有两个步骤。

  • 安装 PowerShell(在您的操作系统上)
  • 运行 PowerShell 脚本

安装 PowerShell:

以下 URL 将帮助您在计算机中安装 PowerShell。

要确认您的安装,请打开一个终端并运行pwsh(这是用于 mac)。您将看到以下内容:

在此处输入图像描述

运行 PowerShell 脚本:

接下来,在 PowerShell 终端中,运行以下命令。将my-script.ps1, 替换为您的脚本名称:

./my-script.ps
于 2022-01-07T03:52:06.617 回答