当我启动 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 等标志,但我没有命令行来传递它。
尝试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) 我找不到传递函数的方法。只是变数。
没有将函数放在 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 行的 AST。但是要做到这一点,第一步是在提交之前(在按下 ENTER 之前)获取当前行的文本,但我找不到 API 函数或钩子来执行此操作。一个存在吗?
我在新的 Windows 终端上使用 PowerShell Core 7.1.0。
似乎 PSReadLine 的 PredictiveSource 选项可能可以用于此目的,前提是它可以在每个字母条目上调用,而不仅仅是在 TAB 上调用,但我在挖掘后找不到有关 3rd-party 插件的类型合同的任何信息通过文档和 C# 代码...
正如传说中的@mklement0 所建议的那样,也许Set-PSReadLineKeyHandler
可以使用。它似乎是用于键绑定的,但我仍在思考如何将其用于此目的。
有几种语言可以提供默认或逻辑或分配机制:
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
我在 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) 我正在生成两个文件,userscript.meta.js
并且userscript.user.js
. 我需要将 的输出userscript.meta.js
放置在 的开头userscript.user.js
。
Add-Content
似乎不接受前置参数,并且Get-Content | Set-Content
会失败,因为userscript.user.js
正在使用Get-Content
.
如果物理上可能有一个干净的解决方案,我宁愿不创建中间文件。
如何实现这一目标?
假设我在命令行上输错:
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 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
注意:我试图
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) powershell ×10
powershell-core ×10
.net ×1
.net-core ×1
azure ×1
azure-devops ×1
cim ×1
command-line ×1
prepend ×1
version ×1
windows ×1
wmi ×1