标签: powershell-core

如何从 PowerShell 脚本打开另一个 PowerShell 控制台

在 OSX 中,我打开 bash 终端并进入 PowerShell 控制台。在我的 PowerShell 脚本中,我想打开另一个 PowerShell 控制台并在那里执行 PowerShell 脚本。

在Windows下,我会做

Invoke-Expression ('cmd /c start powershell -Command test.ps1')
Run Code Online (Sandbox Code Playgroud)

我怎样才能在 OSX 中做同样的事情?

macos terminal powershell powershell-core

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

新的 PowerShell 7 ForEach-Object Parallel 是如何实现的?

PowerShell 7 引入了一个非常需要的功能来并行运行管道输入。

PowerShell 7的文档没有提供有关如何实现的任何详细信息。

已经利用PoshRSJobInvoke-Parallel之前的模块,我知道,运行空间在传统上认为是在运行PowerShell的工作在PowerShell中并行操作的更有效的方法。我已经阅读了一些混合内容,表明这是现在使用线程而不是运行空间,但找不到任何其他特定内容。

我真的很感激对以下方面的一些技术见解:

  1. 从 .NET 的角度来看,执行的生命周期是什么
  2. 新功能是运行空间还是线程?(或者运行空间只是 System.Management.Automation 中的一个 .NET 线程?)
  3. 既然我们正在进入并行操作,这是否会给传统调试带来任何复杂性?从历史上看,我在使用运行空间进行调试时遇到了困难,并且不确定哪些选项可能已得到改进

powershell runspace powershell-core

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

如何删除Powershell启动文本?

Powershell 6具有Unix风格/etc/issue,提到了文档的链接.

PowerShell v6.0.0
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.
Run Code Online (Sandbox Code Playgroud)

这很好,但是:

  • 我知道文档在哪里
  • 我知道我推出了Powershell 6

如何删除部分或全部消息?IIRC Powershell 5仍然有版权信息,所以也许我不能删除它,但摆脱最后3行会有帮助吗?

powershell powershell-v6.0 powershell-core

4
推荐指数
5
解决办法
3016
查看次数

具有提升权限的 Windows 核心运行命令

标准用户有几个选项可以以管理员(或任何其他用户)身份运行,但是,即使以管理员身份登录,某些功能也需要“提升”运行。

在 Windows gui 上,只需右键单击 a.exe并选择run as Administrator甚至提升“cmd”或“powershell”。

如何在 Windows 核心上获得提升的权限?

windows powershell cmd administrator powershell-core

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

如何为同一台机器自动化 PowerShell 或 PowerShell Core

随着 PowerShell Core 的发布,应用程序在使用托管自动化库 (system.management.automation) 时如何选择调用哪个版本的 Powershell(Powershell 5.x 或 PowerShell Core)?关于应该创建的运行空间的一些事情?或者也许是连接信息?

powershell powershell-core

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

PowerShell 远程处理:控制目标版本(PowerShell Core 或 Windows PowerShell);跨平台远程处理的状态

这个自我回答的问题侧重于 Windows [1],解决了以下几个方面:

现在有两个PowerShell 版本- 传统的、仅限Windows 的 Windows PowerShell跨平台的 PowerShell Core,两者都可以安装在给定的 Windows 机器上:

  • 如何判断哪个 PowerShell 版本将执行远程命令,例如 via Invoke-Command -ComputerName

  • 我如何通过配置针对特定版本,包括临时的持久的

笔记:

对于要在给定机器上通过远程处理可定位的版本,必须将其设置为远程处理

  • 只有Windows PowerShell中自动设置为远程处理,但只能在服务器上运行Windows Server 2012或更高版本。

  • 从 v7 开始,PowerShell Core还没有随 Windows 一起提供;如果您使用的是官方安装程序,则可以选择在安装过程中启用远程处理。

在任何情况下,您都可以根据Enable-PSRemoting需要(重新)启用 PowerShell 远程处理,其中:

  • 必须从相应的版本运行。

  • 必须以管理权限运行


[1] 也就是说,问题集中在基于WinRM的远程处理上(WinRM 是 DTMF …

powershell powershell-remoting winrm wsman powershell-core

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

在新窗口中打开powershell核心的正确方法是什么?

此命令打开一个新的 powershell 窗口,运行命令,然后退出:

Start-Process powershell { echo "hello"; sleep 1; echo "two"; sleep 1; echo "goodbye" }
Run Code Online (Sandbox Code Playgroud)

如果我改为启动 Powershell Core,它会打开一个新窗口,但新窗口会立即退出:

Start-Process pwsh { echo "hello"; sleep 1; echo "two"; sleep 1; echo "goodbye" }
Run Code Online (Sandbox Code Playgroud)

使用 pwsh 进行此类调用的正确方法是什么?

powershell powershell-core

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

如何使用 Powershell Core 7 解析 HTML 表格?

我有以下代码:

    $html = New-Object -ComObject "HTMLFile"
    $source = Get-Content -Path $FilePath -Raw
    try
    {
        $html.IHTMLDocument2_write($source) 2> $null
    }
    catch
    {
        $encoded = [Text.Encoding]::Unicode.GetBytes($source)
        $html.write($encoded)
    }
    $t = $html.getElementsByTagName("table") | Where-Object {
        $cells = $_.tBodies[0].rows[0].cells
        $cells[0].innerText -eq "Name" -and
        $cells[1].innerText -eq "Description" -and
        $cells[2].innerText -eq "Default Value" -and
        $cells[3].innerText -eq "Release"
    }
Run Code Online (Sandbox Code Playgroud)

该代码在 Windows Powershell 5.1 上运行良好,但在 Powershell Core 7 上$_.tBodies[0].rows返回 null。

那么,如何在 PS 7 中访问 HTML 表格的行?

html powershell powershell-core

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

如何在 PowerShell 7 中使用 Windows System.Speech for TTS(或者是否有替代方案)

我在 WindowsPowerShell 和 PowerShell 中都有相同的 profile.ps1。它包括调用 Windows Text-To-Speech 的命令,但是,这些命令在 PowerShell 7 中运行时会失败。

当我尝试使用我使用以下代码创建的 $PomrptTTS 对象时会发生错误:

Add-Type -AssemblyName System.speech
$PromptTTS = New-Object System.Speech.Synthesis.SpeechSynthesizer
Run Code Online (Sandbox Code Playgroud)

在 PowerShell 7 中,任何访问或使用我的 $PormptTTS 对象的尝试都会产生以下结果:

SetValueInvocationException: ....\profile.ps1:82
Line |
  82 |  $PromptTTS.Rate = 0 ; $PromptTTS.Speak("Time for the $((Get-Date).DayofWeek) shuffle")
     |  ~~~~~~~~~~~~~~~~~~~
     | Exception setting "Rate": "Object reference not set to an instance of an object."

MethodInvocationException: ....\profile.ps1:82
Line |
  82 |  … e = 0 ; $PromptTTS.Speak("Time for the $((Get-Date).DayofWeek) shuffle")
     |                                             ~~~~~~~~~~~~~~~~~~~~
     | Exception calling …
Run Code Online (Sandbox Code Playgroud)

windows powershell text-to-speech powershell-core

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

在 Powershell 中分配类似版本的值(带有两个或更多点的数字)

最近我发现了一些关于在 Powershell 中将一些类似版本的值分配给变量的令人讨厌的行为(至少在 7.2.5 中)。

起初我尝试过:

> $version = 1.2.3
> echo $version

Run Code Online (Sandbox Code Playgroud)

我很快发现我必须用引号转义 thr 值才能显式地使其成为字符串。这个效果很好:

> $version = "1.2.3"
> echo $version
1.2.3
Run Code Online (Sandbox Code Playgroud)

问题是:为什么会这样?为什么pwsh在第一个示例中不抛出某种错误而只是将其转换为$null?就像其他情况一样:

> 12a
12a: The term '12a' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

> [int]12a
ParserError:
Line |
   1 | …
Run Code Online (Sandbox Code Playgroud)

powershell type-conversion powershell-core

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