如何在PowerShell中运行可执行文件并通过if语句确定它是成功还是失败?
更具体地说,我正在尝试让devenv.exe从PowerShell脚本构建解决方案,我需要知道它是成功还是失败.失败了,我的意思是构建失败了,我假设devenv正在向shell发送一些东西(可能在stderr流中?)
我尝试使用&,Invoke-Expression并Invoke-Item设法让所有这些运行exe.但我无法获得有关成功/失败的反馈.
有没有办法压缩Powershell中的ArrayList的返回值(= Index)(使用System.Collections.ArrayList),还是应该使用另一个类?
$myArrayList = New-Object System.Collections.ArrayList($null)
$myArrayList.Add("test")
Output: 0
Run Code Online (Sandbox Code Playgroud)
在此先感谢米尔德
我有一个脚本,我利用函数来包装部分代码,允许我在指定的点移动部分.我发现我必须在脚本中首先列出函数才能正确运行.
$stepChoice = read-host 'Where would you like to start.'
switch($stepChoice)
{
1{Step1}
2{Step2}
3{Step3}
}
# Steps.ps1
function Step1 {
'Step 1'
Step2
}
function Step2 {
'Step 2'
Step3
}
function Step3 {
'Step 3'
'Done!'
}
Run Code Online (Sandbox Code Playgroud)
这给我以下错误:
术语"Step1"未被识别为cmdlet,函数,脚本文件或可操作程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.
Run Code Online (Sandbox Code Playgroud)At C:\Tools\Scripts\functiontest.ps1:7 char:12 + 1{Step1 <<<< } + CategoryInfo : ObjectNotFound: (Step1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException*
如果我改变它的顺序它工作正常:
# Steps.ps1
function Step1 {
'Step 1'
Step2
}
function Step2 {
'Step 2'
Step3
}
function …Run Code Online (Sandbox Code Playgroud) 如何使用PowerShell获取Windows服务启动类型而不使用WMI?
我查看了Get-Service命令,它没有提供显示"启动类型"的东西.
这是应用于powershell的"ASCIIbetical"命令与"Natural"命令的着名问题.为了能够以与资源管理器相同的方式对powershell进行排序,您可以在StrCmpLogicalW API上使用此包装器,它实际上为Windows资源管理器执行自然排序.这需要一些管道.
但是,这篇文章表明在python中有一个三线程实现的排序.人们希望Get-ChildItem cmdlet或至少文件系统提供程序可以具有内置的自然排序选项.不幸的是,他们没有.
所以这是一个问题,Powershell中最简单的实现是什么?简单来说,我的意思是编写的代码量最少,可能没有第三方/外部脚本/组件.理想情况下,我想要一个简短的 Powershell函数来为我做排序.
我最近编写了一个功能很好的PowerShell脚本 - 但是,我现在想升级脚本并添加一些错误检查/处理 - 但我似乎已经陷入了第一道障碍.为什么以下代码不起作用?
try {
Remove-Item "C:\somenonexistentfolder\file.txt" -ErrorAction Stop
}
catch [System.Management.Automation.ItemNotFoundException] {
"item not found"
}
catch {
"any other undefined errors"
$error[0]
}
finally {
"Finished"
}
Run Code Online (Sandbox Code Playgroud)
错误被捕获在第二个catch块中 - 您可以看到输出$error[0].显然我想在第一个区块中抓住它.我错过了什么?
我使用了高级参数处理来支持多个参数集.是否有任何预定义的变量或方法来确定哪个参数集用于调用脚本?
例如
if($parameterSet -eq "set1") { ... } elseif ($parameterSet -eq "set2") { ... }
Run Code Online (Sandbox Code Playgroud)
?
我在PowerShell脚本中以下列方式调用GPG:
$key = & 'gpg' --decrypt "secret.gpg" --quiet --no-verbose > $null
Run Code Online (Sandbox Code Playgroud)
当我运行脚本时,我不希望在主控制台上看到GPG的任何输出.
由于我在PowerShell中的高通,我不知道该怎么做.我搜索了Stack Overflow并用谷歌搜索了一种方法来做到这一点,找到了很多方法来做到这一点,但是没有它起作用.
例如,"> $ null"无效.我发现--quiet --no-verboseGPG 的选项可以在控制台中输出更少的输出,但它并不完全安静,我相信PowerShell也有一种方法.
我有以下脚本代码
#[string]$password = $( Read-Host "Input password, please" )
param (
[string]$ReleaseFile = $(throw "-ReleaseFile is required"),
[string]$Destination = $(throw "-Destination is required")
)
function unzipRelease($src, $dst)
{
$shell = new-object -com shell.application
$zip = $shell.NameSpace($src)
foreach($item in $zip.items())
{
$shell.Namespace($dst).copyhere($item)
}
}
# .\deployrelease.ps1 -ReleaseFile ".\deploy.zip" -Destination "."
unzipRelease –Src '$ReleaseFile' -Dst '$Destination'
Run Code Online (Sandbox Code Playgroud)
我运行脚本:.\ deployrelease.ps1 -ReleaseFile".\ deploy.zip"-Destination"."
但我一直这样说:
PS C:\Users\Administrator\Documents\Tools> .\deployrelease.ps1 -ReleaseFile ".\deploy.zip" -Destination
The string starting:
At C:\Users\Administrator\Documents\Tools\deployrelease.ps1:19 char:16
+ unzipRelease â? <<<< "Src '$ReleaseFile' -Dst '$Destination'
is …Run Code Online (Sandbox Code Playgroud) 终端错误:无法加载 nodemon.ps1,因为在此系统上禁用了运行脚本。有关详细信息,请参阅 https://go.microsoft.com/fwlink/?LinkID=135170 上的 about_Execution_Policies。
我有一个通过这种方式解决问题的解决方案
这解决了问题,但这样系统会显示安全风险警告。我的问题:有没有其他方法可以在没有安全风险的情况下解决这个问题?目标是使用 nodemon。
powershell web-development-server node.js nodemon visual-studio-code
powershell ×10
.net ×1
arraylist ×1
block ×1
coding-style ×1
node.js ×1
nodemon ×1
output ×1
parameters ×1
try-catch ×1
verbosity ×1