标签: powershell-core

如何删除 Windows 终端中的 powershell 广告和更新检查?

当我启动 Windows 终端时,我得到以下信息:

在此输入图像描述

如何摆脱版权和版本通知、帮助 URL 和更新检查?我只想进入命令行。

我的 powershell 配置文件在 settings.json (Windows 终端的配置文件)中如下所示:

{
    "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
    "hidden": false,
    "name": "PowerShell",
    "source": "Windows.Terminal.PowershellCore",
    "fontFace": "Cascadia Code PL",
    "startingDirectory": "C:\\Users\\user1\\desktop\\"
}
Run Code Online (Sandbox Code Playgroud)

我见过 -nologo 等标志,但我没有命令行来传递它。

windows powershell powershell-core windows-terminal

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

Powershell 6 的哪个库包含 get-wmiobject 命令?

尝试get-WmiObject在 PowerShell(版本 6)中使用该命令时出现以下错误:

PS C:\Users\zsofi> Get-WmiObject Win32_product | select name, packagecache

Get-WmiObject : The term 'Get-WmiObject' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Get-WmiObject Win32_product | select name, packagecache
+ ~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException`
Run Code Online (Sandbox Code Playgroud)

powershell wmi cim powershell-core

13
推荐指数
2
解决办法
6778
查看次数

如何在 ForEach-Object -Parallel 中传递自定义函数

我找不到传递函数的方法。只是变数。

没有将函数放在 ForEach 循环中的任何想法?

function CustomFunction {
    Param (
        $A
    )
    Write-Host $A
}

$List = "Apple", "Banana", "Grape" 
$List | ForEach-Object -Parallel {
    Write-Host $using:CustomFunction $_
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

powershell powershell-core

12
推荐指数
2
解决办法
3204
查看次数

在按下 ENTER 之前获取 powershell 当前行

我有一个想法,编写一个可视化工具,在您键入时显示 PowerShell 行的 AST。但是要做到这一点,第一步是在提交之前(在按下 ENTER 之前)获取当前行的文本,但我找不到 API 函数或钩子来执行此操作。一个存在吗?

我在新的 Windows 终端上使用 PowerShell Core 7.1.0。

预测来源

似乎 PSReadLine 的 PredictiveSource 选项可能可以用于此目的,前提是它可以在每个字母条目上调用,而不仅仅是在 TAB 上调用,但我在挖掘后找不到有关 3rd-party 插件的类型合同的任何信息通过文档和 C# 代码...

设置 PSReadLineKeyHandler

正如传说中的@mklement0 所建议的那样,也许Set-PSReadLineKeyHandler可以使用。它似乎是用于键绑定的,但我仍在思考如何将其用于此目的。

powershell powershell-core windows-terminal

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

Powershell'x或y'赋值

有几种语言可以提供默认或逻辑或分配机制:

a = b || c;
a = b or c
a="${b:-$c}"
a = b ? b : c;
Run Code Online (Sandbox Code Playgroud)

到目前为止,我在Powershell Core中找到的唯一等价物是非常冗长:

$a = if ($b) { $b } else { $c }
Run Code Online (Sandbox Code Playgroud)

在某些情况下必须成为

$a = if ($b -ne $null) { $b } else { $c }
Run Code Online (Sandbox Code Playgroud)

有没有更好的选择[编辑:],这不会牺牲可读性?

powershell ternary-operator null-coalescing-operator powershell-core

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

Azure Devops Powershell 不显示 Get-ChildItem 的文件名

我在 Azure DevOps 管道中使用以下任务vmImage: ubuntu-latest

- pwsh: Get-ChildItem *.* -Recurse -Path $(Pipeline.Workspace)
  displayName: 'Show folder contents for debugging'
Run Code Online (Sandbox Code Playgroud)

我这样做是为了列出在先前的管道工件步骤dir中下载的文件列表。download我也尝试过常规powershell而不是 Powershell Core。

文件夹名称显示正确,并且似乎显示了文件条目,但根本不包含文件名。这是我看到的输出的一部分:

/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -Command . '/home/vsts/work/_temp/1f22f03a-d1c6-4983-a08e-39ed349876be.ps1'

    Directory: /home/vsts/work/1

UnixMode   User             Group                 LastWriteTime           Size
--------   ----             -----                 -------------           ----
drwxr-xr-x vsts             docker             04/03/2022 16:22           4096
drwxr-xr-x vsts             docker             04/03/2022 16:22           4096
drwxr-xr-x vsts             docker             04/03/2022 16:22           4096
drwxr-xr-x vsts             docker             04/03/2022 16:22           4096
drwxr-xr-x …
Run Code Online (Sandbox Code Playgroud)

powershell azure azure-devops powershell-core

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

如何在 PowerShell 中添加到文件前面?

我正在生成两个文件,userscript.meta.js并且userscript.user.js. 我需要将 的输出userscript.meta.js放置在 的开头userscript.user.js

Add-Content似乎不接受前置参数,并且Get-Content | Set-Content会失败,因为userscript.user.js正在使用Get-Content.

如果物理上可能有一个干净的解决方案,我宁愿不创建中间文件。

如何实现这一目标?

powershell prepend powershell-core

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

我可以在Powershell中自定义"未识别为cmdlet的名称"错误吗?

假设我在命令行上输错:

whih foo
Run Code Online (Sandbox Code Playgroud)

Powershell返回:

whih : The term 'whih' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ whih mocha
+ ~~~~
+ CategoryInfo          : ObjectNotFound: (whih:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

长消息对脚本很有用,但是对于交互式shell使用,我想用更短的东西包装它,比如:

'whih' isn't a cmdlet, function, script file, or operable program.
Run Code Online (Sandbox Code Playgroud)

我可以包装错误并将其更改为更短的内容吗?

powershell command-line powershell-core

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

PowerShell ISE:如何运行新的PowerShell版本

如何使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 version powershell-ise powershell-core

9
推荐指数
2
解决办法
9130
查看次数

当没有输出时 Process.StandardOutput.Readline() 挂起

注意:我试图packer.exe作为后台进程运行来解决构建器的特定问题azure-arm,并且我需要观察输出。我没有使用
Start-Process,因为我不想使用中间文件来使用输出。

我将以下代码设置packer.exe为在后台运行,以便我可以使用其输出并对特定日志消息采取行动。这是一个较大脚本的一部分,但这是有问题的,行为不正确:

  $builderDir = ( Split-Path -Parent $PSCommandPath )
  Push-Location $builderDir
  
  # Set up the packer command to run asynchronously
  $pStartProps = @{
    FileName               = ( Get-Command -CommandType Application packer ).Source
    Arguments              = "build -var-file ""${builderDir}\win-dev.pkrvars.hcl"" -only ""azure-arm.base"" ."
    UseShellExecute        = $false
    RedirectStandardOutput = $true
    RedirectStandardError  = $false
    LoadUserProfile        = $true
  }
  $pStartInfo = New-Object ProcessStartInfo -Property $pStartProps

  $p = [Process]::Start($pStartInfo)

  while ( $null -ne ( $output = $p.StandardOutput.Readline() ) -or …
Run Code Online (Sandbox Code Playgroud)

.net powershell .net-core powershell-core

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