我必须从批处理文件中调用PowerShell脚本.脚本的一个参数是布尔值:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -File .\RunScript.ps1 -Turn 1 -Unify $false
Run Code Online (Sandbox Code Playgroud)
该命令失败,并显示以下错误:
Cannot process argument transformation on parameter 'Unify'. Cannot convert value "System.String" to type "System.Boolean", parameters of this type only accept booleans or numbers, use $true, $false, 1 or 0 instead.
At line:0 char:1
+ <<<< <br/>
+ CategoryInfo : InvalidData: (:) [RunScript.ps1], ParentContainsErrorRecordException <br/>
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,RunScript.ps1
Run Code Online (Sandbox Code Playgroud)
截至目前,我在我的脚本中使用字符串进行布尔转换.但是如何将布尔参数传递给PowerShell?
当在包含空格的路径上调用exe时,我遇到了PowerShell的问题.
PS C:\ Windows服务> invoke-expression"C:\ Windows Services\MyService.exe"
术语"C:\ Windows"未被识别为cmdlet,函数,脚本文件或可操作程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.
它似乎在"Windows"和"服务"之间的空间上分裂.知道怎么解决这个问题吗?
这个自我回答的问题试图解决在 PowerShell中处理进程退出代码的两个不同方面:
在 PowerShell 代码中,如何查询外部进程设置的退出代码(对外部程序的调用),这些退出代码如何与 PowerShell 的错误处理相结合?
当其他人通过其 CLI pwsh(PowerShell Core)/ powershell.exe(Windows PowerShell)调用 PowerShell 时,什么决定了 PowerShell 进程的退出代码,该代码将成功与失败传达给调用进程(可能是构建/CI/自动化服务器任务) ,计划任务或不同的外壳,例如)。
所以我尝试了一些不同的方法从命令行运行PowerShell脚本,每一个都返回一个错误.
这是这条路:
C:\Users\test\Documents\test\line space\PS Script\test.ps1
Run Code Online (Sandbox Code Playgroud)
我试过这些:
powershell -File '"C:\Users\test\Documents\test\line space\PS Script\test.ps1"'
powershell "& ""C:\Users\test\Documents\test\line space\PS Script\test.ps1"""
Powershell "& 'C:\Users\test\Documents\test\line space\PS Script\test.ps1'"
Powershell -File 'C:\Users\test\Documents\test\line space\PS Script\test.ps1'"
Run Code Online (Sandbox Code Playgroud)
我收到所有这些错误:
&:术语"C:\ Users\test\Documents\test\line space\PS Script \"不被识别为cmdlet,函数,脚本文件或可操作程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.
处理-File''C:\ Users\test\Documents\test\line space\PS Script \''失败:不支持给定路径的格式.为-File参数指定有效路径.
任何帮助将不胜感激!
我发现,如果仅在Windows 10上将破折号传递给PowerShell 5.1脚本的参数,例如:
powershell.exe -File Test.ps1 -
Run Code Online (Sandbox Code Playgroud)
我收到一条奇怪的错误消息:
C:\ path \ Test.ps1:无法处理参数,因为参数“名称”的值无效。更改“名称”参数的值,然后再次运行该操作。
- CategoryInfo:InvalidArgument:(:) [Test.ps1],PSArgumentException
- FullyQualifiedErrorId:Argument,Test.ps1
该Test.ps1只:
echo "foo"
Run Code Online (Sandbox Code Playgroud)
我遇到的实际问题是,当脚本声明任何强制性参数时:
param (
[Parameter(Mandatory)]
$value
)
echo "foo"
Run Code Online (Sandbox Code Playgroud)
然后以相同的方式(带有-参数)执行脚本完全没有任何作用。无输出。没有错误讯息。它只是挂了几秒钟。然后控件返回到命令提示符。
echo "foo"
Run Code Online (Sandbox Code Playgroud)
-对PowerShell(5.1)意味着什么?
相反,在Windows 7上使用PowerShell 2.0,在这种情况下,我可以使用脚本:
param (
[Parameter(Mandatory)]
$value
)
echo "foo"
Run Code Online (Sandbox Code Playgroud)
有什么意义(缺少必需的参数)。
在没有强制性参数声明的情况下,脚本可以工作(将其打印输出):
C:\path>powershell.exe -File Test.ps1 -
C:\path>_
Run Code Online (Sandbox Code Playgroud) 这个自我回答的问题旨在系统地概述 PowerShell CLI(命令行界面),适用于Windows PowerShell ( powershell.exe) 和PowerShell (Core) v6+(pwsh.exe在 Windows 上,pwsh在 Unix 上)。
尽管存在官方帮助主题(请参阅答案中的链接),但它们并没有描绘出完整的画面并且缺乏系统的处理(截至撰写本文时)。
其中,回答了以下问题:
特定于版本的 CLI 有何不同?
如何将要执行的 PowerShell 代码传递给 CLI?怎么办-Command(-c)和-File(-f)有什么区别?
传递给这些参数的参数需要如何引用和转义?
哪些字符编码问题会起作用?
PowerShell CLI 如何处理 stdin 输入,它们生成什么 stdout/stderr 以及以什么格式?
powershell quoting command-line-interface character-encoding io-redirection
我知道两者都在 PowerShell 中使用,但用于不同的上下文。
互联网上有关此主题的信息很少,唯一谈论此主题的网站(没有让我理解这个概念)是:
https ://www.rlmueller.net/PowerShellEscape.htm
我是 PowerShell 的初学者,最近才接触它。在我的另一个主题的答案中出现了转义
的用例:当我将参数传递给嵌套的 Start-Process 命令时,PowerShell 删除多个连续的空格\
有谁可以通过示例和用例向我详细解释PowerShell中转义反引号`和反斜杠之间的区别吗\?
至少有一个来源是受欢迎的,但这不是强制性的。
我有 my_project.ps1 文件,我可以从中激活虚拟环境并启动我的项目。
目前我需要打开我的 powershell,然后我需要转到保存 .ps1 文件的目录并且只能从 powershell 打开它。
有什么方法可以让我双击 .ps1 文件并且它将在 power shell 中自动打开吗?
非常简单的设置,请在家尝试一下。
$args
Run Code Online (Sandbox Code Playgroud)
$args
Run Code Online (Sandbox Code Playgroud)
@echo off
ruby test.rb "foo bar moo"
powershell .\test.ps1 "foo bar moo"
pause
Run Code Online (Sandbox Code Playgroud)
运行测试.bat。猜猜输出。
如果你像我一样想那你就错了。
["foo bar moo"]
foo
bar
moo
Run Code Online (Sandbox Code Playgroud)
Ruby 见证了这三个单词作为单个参数传递。PowerShell 以比尔·盖茨的名义在做什么?我怎样才能让它按逻辑工作?
或者我错过了什么?