标签: powershell-core

如何在 PowerShell 中为 Linux 创建永久环境变量

如何在 PowerShell Core 中为 Linux 创建、设置和更新永久环境变量。以下解决方案正常工作,但仅当我在 Windows 上运行脚本时:

[Environment]::SetEnvironmentVariable("Variable", "Value", "Machine")
Run Code Online (Sandbox Code Playgroud)

linux powershell powershell-core

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

用于 Linux 的 Powershell 核心 sqlserver 模块中缺少 Invoke-Sqlcmd cmlet

我在 Linux Ubuntu 16.04 上使用最新版本的 Powershell 核心。我正在尝试使用 powershell 的 invoke-sqlcmd cmdlet 查询 sql server 数据库。(是的,我已经安装了 sqlserver 模块)我收到一条错误消息,指出它找不到 cmdlet。当我安装 sqlserver 模块时,该命令在 windows powershell 中。 一样的截图

invoke-sqlcmd 是否仅适用于 Windows?如果是,是否有另一种方法可以在 powershell 核心中实现相同的结果。

powershell-core

5
推荐指数
2
解决办法
1370
查看次数

为什么 PowerShell ISE 中的控制台不使用最新安装的 PowerShell 版本?

我最近安装了 PowerShell 6.2。

如果我启动 PowerShell 6 (x64) 命令提示符并运行$PSVersionTable.PSVersion这是结果

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
6      2      0
Run Code Online (Sandbox Code Playgroud)

在相同的提示下,我运行 ISEpowershell_ise.exe并启动 PowerShell ISE。但是,在 ISE 中的控制台中,如果我运行$PSVersionTable.PSVersion它会报告:

Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      -1     -1  
Run Code Online (Sandbox Code Playgroud)

是否有设置来控制 ISE 查找 PowerShell 的位置?或者有什么方法可以确保它使用安装的最新版本?


更新:作为安装 PowerShell Core(即 6.2 版)的一部分,我必须安装 Windows Management Framework 5.1。我从这个文档中了解到,这也应该将 ISE 控制台的 PowerShell 版本升级到 5.1。如上所述,我仍然看到 4.0 版。我错过了什么?

powershell powershell-ise powershell-4.0 powershell-v6.0 powershell-core

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

如何在 Powershell Core 中解析 html

因此,Invoke-WebRequest在 Powershell Core 中,使用-UseBasicParsing. 可以在此处此处找到更多详细信息。

净结果 -ParsedHtml结果对象上没有属性。

虽然基本原理非常合理,但问题是 - 如何在 Powershell Core 中解析 HTML?

现在,我可能可以使用 .NET Core 库,如HtmlAgilityPack,但似乎需要大量的代码更改才能迁移现有的 PS 代码以使用此库。

这是我们最好的选择还是对 PS Core 用户有更好的选择?

编辑 1

答案似乎在这里找到 -无法使用 IHTMLDocument2

html powershell parsing powershell-core

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

PowerShell Core 可以使用 Windows 终端而不是 pwsh.exe 控制台吗?

默认的 PowerShell Core (pwsh.exe) 控制台看起来就像命令提示符 (cmd.exe) 控制台。此外,PowerShell Core 控制台也有无法正确显示外文字符(例如韩语、日语等)的限制,除非我更改字体,但我不想这样做。另一方面,Windows 终端可以正确显示外来字符,因为它默认使用 UTF-8。

我使用 AutoHotKey 启动 PowerShell 脚本 (.ps1),我想知道是否可以在 Windows 终端中打开该脚本并自动使用 PowerShell Core?我需要执行任何配置更改吗?

powershell-core windows-terminal

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

如何将 AzureRm 模块与 PowerShell 7 一起使用?

语境

我刚刚安装了 PowerShell 7。我正在尝试运行经过工作测试的 Azure 相关脚本...因此我安装并导入了 AzureAd 和 AzureRM 模块。

当尝试登录其中一个Connect-AzureADConnect-AzureRmAccount两个时出现以下错误(继续阅读)

无法加载类型“System.Security.Cryptography.SHA256Cng”

好的,这是因为 Azure 模块正在寻找该 API,而该 API 在 .NET Core 中不可用,因此我将Import-Module-UseWindowsPowerShell参数一起使用,这解决了问题,但仅限于 AzureAD 模块

问题

对于该命令,Import-Module AzureRm -UseWindowsPowerShell我收到以下错误消息:

导入模块:无法为远程模块“AzureRM”生成代理。在远程会话中运行 Get-Command 命令未返回任何结果。

所以我仍然无法使用Connect-AzureRmAccount有什么想法吗?

powershell azure-powershell powershell-core

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

使用 Powershell Core 进行多线程在线交换 - C#

我正在编写一个 C# 应用程序,该应用程序将涉及使用PowerShell V2 模块从 Exchange Online 收集数据。在客户获得管理员同意后,我将使用在 Windows 虚拟机上运行的多线程 C# 应用程序与他们的环境建立 PowerShell 连接。我正在使用 Net 5.0 和 PowerShell 7.x。我需要使用多个线程,因为从单个租户收集数据可能是一个漫长的过程。

问题是,虽然应用程序运行良好,但如果我尝试使用多个线程同时为两个租户运行应用程序,则会发生冲突。该模块似乎不是线程安全的。

我构建了一个通过 .Net DI 作为瞬态注入的服务。此服务创建一个 HostedRunspace 类,用于执行 PowerShell 的状态管理。

public class HostedRunspace : IDisposable
{
    private Runspace runspace;
 
    public void Initialize(string[] modulesToLoad = null)
    {

        InitialSessionState defaultSessionState = InitialSessionState.CreateDefault();
        defaultSessionState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.RemoteSigned;
        
        if (modulesToLoad != null)
        {
            foreach (string moduleName in modulesToLoad)
            {
                defaultSessionState.ImportPSModule(moduleName);
            }
        }

        runspace = RunspaceFactory.CreateRunspace(defaultSessionState);
        
        runspace.ThreadOptions = PSThreadOptions.UseNewThread;
        runspace.ApartmentState = ApartmentState.STA;

        runspace.Open();
    }

    public async …
Run Code Online (Sandbox Code Playgroud)

c# powershell exchange-server office365 powershell-core

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

exec: "pwsh": 在 %PATH% 中找不到可执行文件

我一直在尝试在 gitlab CI/CD 上为演示项目启动我的管道。我已经在我的 Windows 本地机器上安装了 gitlab-runner,并将执行程序类型指定为“Shell”。我已经成功地将 gitlab-runner 与我的 gitlab 项目集成在一起。但是,每当我将任何更改推送到 repo 时,管道都会启动并最终在 %PATH 错误中找不到“pshw”。 这是我每次都会遇到的错误

ERROR: Job failed (system failure): prepare environment: failed to start process: exec: "pwsh": executable file not found in %PATH%. Check https://docs.gitlab.com/runner/shells/index.html#shell-profile-loading for more information
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我解决这个问题并解释我收到此错误的原因和原因。

提前致谢

gitlab-ci gitlab-ci-runner powershell-core

5
推荐指数
3
解决办法
1292
查看次数

如果属性名称是整数,为什么我不能点引用哈希表键的值属性

考虑以下哈希表:

$table = @{
  6           = '10.11.12.13', '10.11.12.14'
  15          = 'domain.tld'
  NameServers = '10.11.12.13', '10.11.12.14'
}
Run Code Online (Sandbox Code Playgroud)

通常,当您有哈希表时,您可以引用.键名称以更加面向对象的方式使用它,而不是使用数组访问器语法[key]。例如,调用NameServers上面哈希表上的键并.返回值,我可以按预期使用该属性值的成员:

$table.NameServers       # ========> 10.11.12.13
                         # ========> 10.11.12.14

$table.NameServers.Count # ========> 2
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试使用包含相同字符串内容的6a 访问键,它会引用正确的值,但我无法调用返回对象上的任何成员。.必须在这里使用传统的数组访问器:

$table.6        # =====> 10.11.12.13
                # =====> 10.11.12.14

$table.6.Count  # =====> ParserError:
                # =====> Line |
                # =====>    1 |  $hi.6.Count
                # =====>      |      ~
                # =====>      | Missing property name after reference operator.

$table[6].Count # =====> …
Run Code Online (Sandbox Code Playgroud)

powershell powershell-core

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

为什么 -ErrorAction Stop / $ErrorActionPreference = 'Stop' 有时无效,例如使用隐式远程处理/Windows PowerShell 兼容性?

这个自我回答的问题是从 PowerShell (Core) v7.3.x 开始编写的,旨在解决以下症状:

有时,使用或似乎都无法有效地通过/语句捕获和处理错误。-ErrorAction -Stop$ErrorActionPreference = 'Stop'trycatch

这是两个例子:

  • Get-NetAdapter$ErrorActionPreference = 'Stop' 从脚本或函数中忽略(从全局范围以外的范围):

    # Run on Windows. Affects both PowerShell editions.
    # The `catch` block isn't triggered, and the (still non-terminating) error prints.
    & { # Simulate running in a child scope.
      $ErrorActionPreference = 'Stop'
      try {     
        Get-NetAdapter nosuch 
      }
      catch {
        "Should get here, but don't."
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)
    • 然而,使用-ErrorAction Stop( Get-NetAdapter nosuch -ErrorAction Stop …

error-handling powershell powershell-remoting powershell-core

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