小编Kee*_*tle的帖子

Visual Studio Code 集成终端默认为 PowerShell Core

我最近安装了PowerShell Core v6。我想检查一下,看看它是什么样的,如果我想玩它,可以很容易地切换到Windows 终端。安装版本 6 后,Visual Studio Code 继续使用集成控制台的核心,尽管我的settings.json文件指向System32中的powershell.exe文件。我怎样才能解决这个问题?

我们的环境全部使用 5.1 版本,核心缺少 5.1 中的许多功能和 cmdlet。在测试我将部署的脚本时我需要这些。我可以添加另一个终端,它将使用 PowerShell 5.1,但由于它未集成,运行脚本块有时会失败。这是我的settings.json文件:

{
    "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
    "window.zoomLevel": 0,
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "explorer.confirmDragAndDrop": false,
    "files.autoSave": "afterDelay",
    "powershell.powerShellDefaultVersion": "Windows Powershell (x64)",
    "editor.accessibilitySupport": "off"
}
Run Code Online (Sandbox Code Playgroud)

terminal powershell visual-studio-code

2
推荐指数
1
解决办法
5855
查看次数

使用 if 语句检查变量是否为空返回错误

我有一个我创建的 powershell 脚本,如果模块尚未导入,它会尝试导入模块。

Try {
    Import-Module -Name 'ModuleName' -ErrorAction Stop -ErrorVariable ModFail             
}
Catch {
    Write-Error "Module failed to be loaded."
}
Run Code Online (Sandbox Code Playgroud)

稍后在脚本中,我试图检查模块是否无法导入,但检查错误变量。对于未通过 errorvariable 参数设置的其他变量,我仅使用以下内容来检查其是否为空。

If ($null -eq $var) {
    Do stuff
}
Run Code Online (Sandbox Code Playgroud)

但是使用我使用 errorvariable 设置的变量这样做是行不通的。

if ($null -ne $Modfail) {
    Do stuff
}
Run Code Online (Sandbox Code Playgroud)

在测试变量是否实际上为空时,上述评估为真。这与我想要的相反。当我运行变量时,它确实为空并且正在运行

$modfail | Gm 
Run Code Online (Sandbox Code Playgroud)

失败,因为它是空的。为什么会这样?如果我在 errorvariable 参数之外设置变量或者不设置它,它会返回正确的。即使它有空格,它也应该在管道到 Get-Member 时以正确的字符串返回?

variables error-handling powershell

1
推荐指数
1
解决办法
2968
查看次数