我今天第一次给PowerShell(v3.0)拍摄了一个镜头,并且对它的一些错误处理概念实现的奇怪方式感到非常沮丧.
我编写了以下代码(使用远程注册表PowerShell模块)
try
{
New-RegKey -ComputerName $PCName -Key $Key -Name $Value
Write-Host -fore Green ($Key + ": created")
}
catch
{
Write-Host -fore Red "Unable to create RegKey: " $Key
Write-Host -fore Red $_
}
Run Code Online (Sandbox Code Playgroud)
(这只是一个片段)
显然,PowerShell的默认行为是不捕获非终止的错误.所以我按照不同的人的建议在我的脚本顶部添加了以下行:
$ErrorActionPreference = "Stop"
Run Code Online (Sandbox Code Playgroud)
在PowerShell ISE中执行此操作确实捕获了所有错误.但是,从终端执行以下命令仍然无法捕获我的错误.
来自ISE:
PS C:\windows\system32> C:\Data\Scripts\PowerShell\Error.ps1
Errorhandling: Stop
SOFTWARE\MySoftware does not exist. Attempting to create
Unable to create RegKey: SOFTWARE\MySoftware
Key 'SOFTWARE\MySoftware' doesn't exist.
Run Code Online (Sandbox Code Playgroud)
从命令行:
PS C:\Data\Scripts\PowerShell> .\Error.ps1
Errorhandling: Stop
SOFTWARE\MySoftware does not exist. Attempting to create
New-RegKey : Key …Run Code Online (Sandbox Code Playgroud) 我在Powershell ISE(手动加载脚本并按F5)和Powershell控制台(执行脚本文件)中运行完全相同的script.ps1文件.它们都有效,但ISE显示控制台没有的错误.为什么?
代码是:
git push origin master
Write-Host "lastExitCode: $lastExitCode Last command was successful: $?"
Run Code Online (Sandbox Code Playgroud)
此代码在ISE中输出此错误:
git.cmd : Initializing to normal mode
At E:\script.ps1:28 char:4
+ git <<<< push origin master
+ CategoryInfo : NotSpecified: (Initializing to normal mode:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Initializing to normal mode
Everything up-to-date
lastExitCode: 0 Last command was successful: False
Run Code Online (Sandbox Code Playgroud)
这在控制台中:
Everything up-to-date
lastExitCode: 0 Last command was successful: True
Run Code Online (Sandbox Code Playgroud)
您可以看到成功状态也不一样.
我想开始为我的一些Powershell脚本使用自定义扩展,但是当我将它们拖到PowerShell ISE中时,它们被视为纯文本,并且我没有得到任何语法突出显示.有什么办法可以让ISE将我的自定义文件扩展名识别为PowerShell脚本吗?
我编写了一个脚本,它基本上是一个用于Outlook的小型所见即所得签名生成器.我们希望我们的签名采用公司颜色和半标准化 - 因此需要这样做.
我在Powershell ISE进行了开发工作,看起来一切都很好.但是,当我使用PowerShell运行脚本时(就像用户一样),它看起来完全不同:
Powershell vs Powershell ISE http://www.freeimagehosting.net/uploads/1d6e6c5c6f.png
最上面的一个是在ISE中生成的,看起来就像我想要的那样.底部的一个直接从powershell运行,似乎已经在视觉上退了五年!
如何在Powershell中使脚本看起来像在Powershell ISE中那样时髦?
谢谢,
本
我遇到过一些情况,当你试图用Ctrl + C杀死一个进程时,PowerShell ISE会冻结.我仍然可以在控制台周围移动光标,但状态仍然停留在"停止"状态.
这已经发生在几个命令中,但是我注意到一个特定的命令一直在发生
mvn jasmine:bdd
Run Code Online (Sandbox Code Playgroud)
此命令运行maven插件,启动jetty服务器.我可以使用Powershell控制台的Ctrl + C来停止此操作,但不能从Powershell ISE停止.这可能与Powershell ISE无法运行交互式命令这一事实有关吗?
现在我的解决方法是在另一个进程中"启动"maven.其他人遇到类似的问题?
谢谢
在PowerShell中,git checkout运行时没有任何错误消息.在ISE中,当git checkout静止不动时,ISE会给出错误消息.
> git checkout master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
git : Switched to branch 'master'
At line:1 char:1
+ git checkout master
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Switched to branch 'master':String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Run Code Online (Sandbox Code Playgroud)
这不是一个主要问题,因为git checkout仍然有效.但这很烦人,所以我想知道为什么ISE会在标准的PowerShell没有时抱怨,更重要的是,我们怎样才能防止这种烦恼.
我看过为什么Powershell ISE显示Powershell控制台没有显示的错误?,这解释了ISE只是显示正常shell正在经历的内容.这个答案并不能解释如何平息这种烦人的行为.
我觉得我做的事情很傻,但问题在于:
Function getPropertyOfFile($a, $b, $c)
{
$a.GetDetailsOf($b, $c)
}
Run Code Online (Sandbox Code Playgroud)
如果我传递适合该函数的$ a,$ b,$ c变量,它就会失败
"方法调用失败,因为[System.Object []]不包含名为'GetDetailsOf'的方法."
但是,如果我直接用我传递的参数替换$ a,$ b,$ c,然后尝试运行它,它运行正常.
到底他妈发生了什么?
注意:我正在使用PowerShell ISE,并通过将其复制/粘贴到控制台中来将函数输入到powershell.我也一直在假设如果我输入一个具有相同名称的新函数,它将覆盖.是否有更好的方法来从.ps1中读取PS?
编辑:我试图将这个问题的答案包装成函数.
编辑2:
Function getPropertyOfFile $a $b $c
{
$a.GetDetailsOf($b, $c)
}
Run Code Online (Sandbox Code Playgroud)
给出Missing function body in function declaration.
At line:1 char:28错误.
我试图在PowerShell ISE中使用.NET 4.0程序集,并尝试更改通过以下方式使用的配置文件:
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $PathToConfig);
Run Code Online (Sandbox Code Playgroud)
[Configuration.ConfigurationManager] :: ConnectionStrings.Count总是返回"1",
"[Configuration.ConfigurationManager] :: ConnectionStrings [0] .Name"总是返回"LocalSqlServer",而ConnectionString名称不在我的".config"中文件.
请注意,从PowerShell命令提示符执行PowerShell脚本将按预期运行.就在我从PowerShell ISE中执行它时,它无法按预期工作.
我对Powershell ISE控制台窗口感到困扰.命令行中没有自动换行功能.当我使用它时,如果我的命令行很长,我将离命令太远.有没有办法来修复命令行的宽度?
谢谢.
如何使PowerShell ISE与PowerShell 6.0一起使用。当前,它具有4.0。
此服务器已安装PowerShell 4.0,并且我通过以下链接通过PowerShell-6.1.0-win-x64.msi安装了PowerShell 6.0:https : //github.com/PowerShell/PowerShell/releases 文件现在位于C:\ Program Files中\ PowerShell \ 6。
但是,ISE仍然显示4.0,但是我需要它运行6.0
$ PSVersionTable.psversion
重大次要建筑修订
4 0 -1 -1
powershell-ise ×10
powershell ×9
.net-4.0 ×1
forms ×1
git ×1
maven ×1
version ×1
windows ×1
windows-8 ×1