string - 无法从 cmd.exe 运行 powershell 脚本?

标签 string powershell powershell-2.0 cmd

我写了一个 powershell 脚本,可以像这样从 powershell 运行:

PS C:> S:\MyScript.ps1 -a "select thing from table where name like 'name'"

我已将它存储在共享驱动器 (S:) 上,但我无法从 powershell 或 cmd.exe 以这种形式运行我的 powershell 脚本:

PS C:> powershell S:\MyScript.ps1 -a "select thing from table where name like 'name'"
C:\> powershell S:\MyScript.ps1 -a "select thing from table where name like 'name'"

我还尝试绕过执行策略运行它:

C:\> powershell -executionpolicy bypass -command S:\MyScript.ps1 -a "select thing from table where name like 'name'"

我还尝试使用完整的 UNC 路径(以上述所有形式)运行它:

C:\> powershell \\MyServer\path\to\MyScript.ps1 -a "select thing from table where name like 'name'"

显示输出的唯一形式是如果我使用不需要传递字符串的参数:

C:\> powershell S:\MyScript.ps1 -z

thing
-----
thing1
thing2
thing3

有人知道这是为什么吗?有什么方法可以将字符串从 cmd.exe 传递给脚本吗?

最佳答案

另请注意,powershell.exe 有一个参数 -File,对于文件而言,它可能比 -command 更好。我不确定脚本参数,因为它们应该像 Nick 所说的那样分组在一个字符串中。只需尝试 Nicks 解决方案,如果这不起作用,请尝试 -File 参数而不是 -command

关于string - 无法从 cmd.exe 运行 powershell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14407998/

相关文章:

c++ - 比较字符串中的字符(由 "="操作符修改)

powershell - Windows Powershell - 如何仅在某些 Windows 服务发生变化时重新显示它们的状态?

powershell - 如何从 Powershell 中已过滤的列表中排除关键字

c# - 如何按特殊顺序提取括号之间的文本?

javascript - 在javascript中使用子字符串清除字符串的最后一个字母

powershell - 有没有办法引用先前输入的命令的倒数第二个单词(例如 $^ 代表第一个单词, $$ 代表最后一个单词)

powershell - 如何在 PowerShell 中按列排序?

powershell - 替换文件中的字符串,并获得有关所做操作的一些反馈

powershell - 尝试在远程计算机上调用 powershell 命令时出现错误

C# 从包含数字和单词的字符串中获取数字序列?