如何创建和运行PowerShell脚本?

Powershell scripts are used to complete tasks simply running script files. This provides easy to use ways to run the same commands again and again. Scripts also used to share the experience with other system administrators easily. Scripts may need sometimes parameters for the operator. In this tutorial, we will look at different aspects and usage forms of Powershell scripts.

Powershell脚本仅用于运行脚本文件即可完成任务。 这提供了易于使用的方式来一次又一次地运行相同的命令。 脚本还用于与其他系统管理员轻松共享经验。 脚本有时可能需要操作员使用参数。 在本教程中,我们将研究Powershell脚本的不同方面和用法形式。

使用执行策略启用Powershell脚本执行 (Enable Powershell Script Execution with Execution Policy)

Security is an important topic today. Microsoft has learned a lot of things from previous mistakes. It created some security mechanisms for its powerful scripting engine Powershell.

安全是当今的重要话题。 微软从以前的错误中学到了很多东西。 它为其强大的脚本引擎Powershell创建了一些安全机制。

打印当前执行政策 (Print Current Execution Policy)

Get-ExecutionPolicy is used to list the current status of the execution policy. We will get like below.

Get-ExecutionPolicy用于列出执行策略的当前状态。 我们会像下面这样。

PS> Get-ExecutionPolicy
Print Current Execution Policy
Print Current Execution Policy
打印当前执行政策

As we can see that current execution policy is whichRestricted simply means we can not run downloaded PowerShell scripts.

正如我们看到的那样,当前执行策略是“ Restricted仅表示我们无法运行下载的PowerShell脚本。

启用执行策略 (Enable Execution Policy)

We can change the PowerShell execution policy with commandSet-ExecutionPolicy by specifying the new policy name. We will allow all scripts by specifying the policy named Unrestricted . In order to change the current shell should have Administrator privileges. This will run scripts if they are disabled on the system.

通过指定新的策略名称,我们可以使用命令Set-ExecutionPolicy更改PowerShell执行策略。 我们将通过指定名为Unrestricted的策略来允许所有脚本。 为了更改当前的shell,应具有管理员权限。 如果系统上禁用了脚本,它将运行脚本。

PS>  Set-ExecutionPolicy Unrestricted
Enable Execution Policy
Enable Execution Policy
启用执行策略

We will simply answer toY the confirmation question.

我们将仅向Y回答确认问题。

创建简单的PowerShell脚本 (Create Simple PowerShell Script)

In order to use it in the test, we will create a script file named whichhelloworld.ps1 contains the following script. ps1 is used to specify the PowerShell script.

为了在测试中使用它,我们将创建一个名为helloworld.ps1的脚本文件,其中包含以下脚本。 ps1用于指定PowerShell脚本。

echo "Hello World"

在当前目录中运行脚本 (Run Script In The Current Directory)

We can simply run the script which resides in the current directory. In this example, we will run helloworld.ps1 . . is the current working directory.

我们可以简单地运行当前目录中的脚本。 在此示例中,我们将运行helloworld.ps1. 是当前的工作目录。

PS> .\helloworld.ps1

空间路径 (Path with Spaces)

Spaces have a different meaning in Powershell and path, file or command names with spaces must be surrounded with double  "  or ' single quotes. In this example, we have a Powershell file which is named hello world.ps1. We can see that hello and world are separated with space. In order to call this Powershell script, we need to use " which will surround the name completely.

空间在PowerShell和路径有不同的含义,用空格的文件或命令的名称必须用双包围"'单引号。在这个例子中,我们已经被命名为一个PowerShell文件hello world.ps1 。我们可以看到,你好世界被空格隔开。要调用此Powershell脚本,我们需要使用" ,它将完全包围该名称。

PS> ".\hello world.ps1"

If we do not use a single or double quote like below we will get a `Not Recognized Name of a cmdlet, function, script file or operable program error” with CommandNotFound type.

如果我们不使用如下所示的单引号或双引号,则将获得CommandNotFound类型的“ cmdlet,函数,脚本文件或可操作程序错误的无法识别的名称”。

PS> .\hello wordl.ps1
Path with Spaces
Path with Spaces
空间路径

使用调用命令Commandlet(Using Invoke Command Commandlet)

Powershell provides Invoke-Command commandlet for command invocation. We can use this for script call. More detailed about Invoke-Command can be found below.

Powershell为命令调用提供了Invoke-Command命令集。 我们可以将其用于脚本调用。 可以在下面找到有关Invoke-Command更多详细信息。

LEARN MORE  Linux Bash Profile File Configuration with Examples
了解更多Linux Bash配置文件的示例配置

Run Commands In Local and Remote System With Powershell Invoke-Command Cmdlet

使用Powershell调用命令Cmdlet在本地和远程系统中运行命令

使用CMD.exe运行(Run With CMD.exe)

We can call different shells from Powershell. For example, we can call commandspython and script from Powershell by specifying the python interpreter. We can also runcmd.exe to call MS-DOS scripts like below. But keep in mind that MSDOS can only run bat files.

我们可以从Powershell调用不同的Shell。 例如,我们可以通过指定python解释器从Powershell调用命令python和script。 我们也可以运行cmd.exe来调用MS-DOS脚本,如下所示。 但是请记住,MSDOS只能运行bat文件。

PS> cmd.exe /C helloworld.bat

无退出运行 (Run without Exit)

After the script is completed the PowerShell window will be closed as the process is complete. If we need to prevent exit from the command prompt we can use orRuncmd.exe with the following command by providing with option-noexit.

脚本完成后,随着过程完成,PowerShell窗口将关闭。 如果需要防止从命令提示符退出,则可以通过提供选项-noexit来将或Runcmd.exe与以下命令配合-noexit

PS> powershell -noexit .\helloword.ps1

以管理员身份运行PowerShell和脚本 (Run PowerShell and Script As Administrator)

If the script needs Administrator privileges we should start the process with Administrator privileges which will be needed by the script. We can start the Powershell with the by right-clicking existing Powershell screen.

如果脚本需要管理员权限,则应使用脚本所需的管理员权限启动该过程。 可以通过右键单击现有Powershell屏幕来启动Powershell。

Run PowerShell and Script As Administrator
Run PowerShell and Script As Administrator
以管理员身份运行PowerShell和脚本

翻译自: https://www.poftut.com/run-powershell-scripts/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值