据我了解,PowerShell 6 和 Core 使用 .Net Core 框架并且是跨平台的,而 PowerShell 5.1 使用 .Net 框架。据我了解,PowersSell Core 也没有 5.1 那样发达
你会建议我开始学习什么?我还是一名高中生,想在 INFOSEC 工作。我想开始学习 PowerShell 来创建自动编辑设置的脚本。你会建议我从 5.1、6 还是核心开始?有什么资源推荐吗?
作为Windows系统管理员,我经常使用PowerShell。随着PS Core的发布以及ISE已死的隐含意义,我开始尝试使用VS Code作为我的日常工具。ISE我缺少的一项功能是能够在全屏模式下在编辑器和终端之间交换。我通常保持ISE处于打开状态并最大化,并根据需要使用Ctrl + R在编辑器和终端之间进行交换。我还没有找到一种最大化终端,以及在终端和编辑器之间轻松交换的方法。我知道我可以使终端占据大部分屏幕,但是a)顶部仍然有大约2行编辑器处于打开状态,b)似乎没有一种简便的方法可以最大化编辑器。有没有办法最小化我尚未发现的ISE行为?
我能够在 MacOSX 上安装 Powershell Core,现在我正在尝试从它管理 Azure AD 实例。
当我安装 AzureAD 模块时,出现以下错误
PS /Users/c> Install-Module -name AzureAD
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this
repository, change its InstallationPolicy value by running the Set-PSRepository
cmdlet. Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "N"):y
PackageManagement\Install-Package : Unable to load DLL 'api-ms-win-core-sysinfo-l1-1-0.dll': The specified …Run Code Online (Sandbox Code Playgroud) macos azure azure-powershell azure-active-directory powershell-core
我用C#编写了一个简单的 PowerShell cmdlet来展示我遇到的问题。随意克隆 repo,或 fork 并使其工作并提交 PR,或者只是查看源代码以确切了解我在做什么。
我已经使用PowerShellStandard.Library NuGet 包创建了一个简单的 PowerShell Core cmdlet 。我正在使用 xUnit 并尝试针对我创建的 PowerShell cmdlet 运行单元测试。问题是当我调用.Invoke()cmdlet 实例的方法时,它抛出一个NullReferenceException.
这是我创建的 cmdlet:
[Cmdlet(VerbsCommon.Get, "RepeatedString")]
[OutputType(typeof(string))]
public class GetRepeatedStringCmdlet : Cmdlet
{
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[Alias("Word")]
[ValidateNotNullOrEmpty()]
public string Phrase { get; set; }
[Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true)]
[Alias("Repeat")]
public int NumberOfTimesToRepeatPhrase { get; set; }
protected …Run Code Online (Sandbox Code Playgroud) 我正在使用PowerShell和PowerShell Core并行。并非我为“大”PowerShell 编写的所有脚本都在 PowerShell Core 中运行,反之亦然。
为了避免混乱,我在考虑是否应该使用两个文件扩展名:
.ps1: 电源外壳.pwsh: PowerShell 核心在Windows 中,扩展比在Unix系统中更重要,其中shebang(#!/bin/mytool) 有助于解决问题。
是使用两个扩展“最佳实践”还是有更好的选择?
附加:.pwsh当我从命令行/终端调用脚本时,我不确定PowerShell 是否会执行脚本。
我在 Unix / Linux 上下文中发现了一个类似的问题。 https://unix.stackexchange.com/questions/182882/use-sh-or-bash-extension-for-bash-scripts
如果$env未指定值,我正在尝试从环境变量或默认值在 PowerShell 脚本中设置变量。我可以通过执行以下操作在 Bash 中做类似的事情。
BASH_VAR_IN_SCRIPT=${MY_ENV_VAR:="default value"}
Run Code Online (Sandbox Code Playgroud)
PowerShell 中有类似的东西吗?我的 Google-fu 没有返回我要找的东西。我正在使用 PowerShell Core 7。
刚才我在我的 Powershell 脚本中观察到一些奇怪的东西,我终于把它缩小到我的代码中的一个错误,就是这一行:
$AnyExecuted = true
Run Code Online (Sandbox Code Playgroud)
应该是
$AnyExecuted = $true
Run Code Online (Sandbox Code Playgroud)
但是,这一行会在 Windows 10 上的标准 Powershell Core 窗口以及 Windows 终端中搞乱转义码处理:
有谁知道为什么会这样?true表达方式是什么?我找不到任何提及它,为什么它会对 Powershell 的输出产生这种影响?
请注意,显然我更新了我的脚本以使用 $true,这是我的意图,这个问题只是关于什么true是。
我有以下 JSON,我想从 Address 下的 JSON 对象中删除街道,这是一个数组。我正在尝试在 powershell 中执行此操作
{
"Customer": [
{
"id": "123"
}
],
"Nationality": [
{
"name": "US",
"id": "456"
}
],
"address": [
{
"$type": "Home",
"name": "Houston",
"streets": [
{
"name": "Union",
"postalCode": "10",
}
]
},
{
"$type": "Office",
"name": "Hawai",
"streets": [
{
"name": "Rock",
"postalCode": "11",
}
]
}
],
"address": [
{
"$type": "Home1",
"name": "Houston",
"streets": [
{
"name": "Union1",
"postalCode": "14",
}
]
},
{
"$type": "Office1",
"name": "Hawaii1", …Run Code Online (Sandbox Code Playgroud) 为什么Start-Process powershell.exe { Read-Host }有效,但Start-Process pwsh.exe { Read-Host }不起作用?
我知道 的-ArgumentList切换Start-Process,但是当涉及转义引号时,它使事情变得更加复杂:
PowerShell 7.0.3 和 7.2.0-preview.3:
Start-Process pwsh.exe -ArgumentList '-Command "& {
''Hello World.''
Read-Host
}"' -Verb RunAs
Run Code Online (Sandbox Code Playgroud)
PowerShell 5.1:
Start-Process powershell.exe {
'Hello World.'
Read-Host
} -Verb RunAs
Run Code Online (Sandbox Code Playgroud)
另一方面,当创建新的 PowerShell 会话(不带Start-Process)时,仍然可以使用脚本块:
pwsh.exe {
'Hello World.'
Read-Host
}
Run Code Online (Sandbox Code Playgroud)
我在这里遗漏了一些东西还是有更好/不同的方法允许脚本块与脚本的其余部分并行执行?
powershell start-process scriptblock powershell-core powershell-7.0
我正在开发一个基于此存储库的项目
(我会问有关它的问题,但它很陈旧......)
我正在尝试执行任务,创建管道,但在此步骤中失败:
- pwsh: |
dotnet tool install --global GitVersion.Tool
Write-Output ">>>>>>>>>>>>>>>>"
dotnet-gitversion
Write-Output "<<<<<<<<<<<<<<<<"
$gitVersionObject = dotnet-gitversion | ConvertFrom-Json
$gitVersionObject.PSObject.Properties.ForEach{
Write-Host -Object "Setting Task Variable '$($_.Name)' with value '$($_.Value)'."
Write-Host -Object "##vso[task.setvariable variable=$($_.Name);]$($_.Value)"
}
Write-Host -Object "##vso[build.updatebuildnumber]$($gitVersionObject.FullSemVer)"
displayName: Calculate ModuleVersion (GitVersion)
Run Code Online (Sandbox Code Playgroud)
我发现这是 gitversion 的问题(也尝试了 AZ 任务中的 gitversion 任务)
INFO [09/23/22 11:30:47:90] HEAD points at branch 'refs/heads/gitversion-v5'.
INFO [09/23/22 11:30:47:90] End: Normalizing git directory for branch 'refs/heads/gitversion-v5' (Took: 90.63ms)
INFO [09/23/22 11:30:47:91] Begin: Loading version variables from disk …Run Code Online (Sandbox Code Playgroud) powershell-core ×10
powershell ×7
json ×2
azure ×1
azure-devops ×1
bash ×1
c# ×1
cmdlet ×1
gitversion ×1
macos ×1
scriptblock ×1
unit-testing ×1