如何运行 PowerShell 脚本 [英] How to run a PowerShell script

查看:54
本文介绍了如何运行 PowerShell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何运行 PowerShell 脚本?

How do I run a PowerShell script?

  • 我有一个名为 myscript.ps1 的脚本
  • 我已经安装了所有必要的框架
  • 我设置了执行政策
  • 我已按照此 MSDN 帮助页面上的说明进行操作并试图像这样运行它:powershell.exe 'C:\my_path\yada_yada\run_import_script.ps1'(有或没有--noexit)
  • I have a script named myscript.ps1
  • I have all the necessary frameworks installed
  • I set that execution policy thing
  • I have followed the instructions on this MSDN help page and am trying to run it like so: powershell.exe 'C:\my_path\yada_yada\run_import_script.ps1' (with or without --noexit)

除了输出文件名外,什么都不返回.

which returns exactly nothing, except that the file name is output.

没有错误,没有消息,什么都没有.哦,当我添加 -noexit 时,同样的事情发生了,但我仍然在 PowerShell 中,必须手动退出.

No error, no message, nothing. Oh, when I add -noexit, the same thing happens, but I remain within PowerShell and have to exit manually.

.ps1 文件应该运行一个程序并根据该程序的输出返回错误级别.但我很确定我什至还没有到达那里.

The .ps1 file is supposed to run a program and return the error level dependent on that program's output. But I'm quite sure I'm not even getting there yet.

我做错了什么?

推荐答案

  1. 启动 Windows PowerShell,然后等待 PS 命令提示符出现
  2. 导航到脚本所在的目录

  1. Launch Windows PowerShell, and wait a moment for the PS command prompt to appear
  2. Navigate to the directory where the script lives

PS> cd C:\my_path\yada_yada\ (enter)

  • 执行脚本:

  • Execute the script:

    PS> .\run_import_script.ps1 (enter)
    

  • 我错过了什么??

    或者:您可以像这样从 cmd.exe 运行 PowerShell 脚本:

    Or: you can run the PowerShell script from cmd.exe like this:

    powershell -noexit "& ""C:\my_path\yada_yada\run_import_script.ps1""" (enter)
    

    根据这个博客文章在这里

    或者你甚至可以从你的 C# 应用程序运行你的 PowerShell 脚本:-)

    Or you could even run your PowerShell script from your C# application :-)

    从您的 C# 应用程序异步执行 PowerShell 脚本

    这篇关于如何运行 PowerShell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆