我已经安装了 PowerShell 6.1.3 版本,我想使用以下 Azure PowerShell 命令连接到 Azure 帐户:
Connect-AzAccount -Tenant <tenantId> -Subscription <subId>
Run Code Online (Sandbox Code Playgroud)
输入此命令后,我收到带有 url 和一些代码的警告。然后我必须转到 URL 并在那里输入代码。之后,我连接到 Azure 帐户。
有没有办法避免这种确认?
我也尝试使用以下命令来做到这一点:
az login -u <username> -p <password>
Run Code Online (Sandbox Code Playgroud)
此命令仅返回一些帐户信息(subscriptionId、tenantId 等),但不会安装到此帐户的连接。
我正在尝试在 Linux 上的 PowerShell 7 中设置 NuGet 包提供程序,以便我可以用来Install-Package从 NuGet Gallery 获取包。但是,当我运行时:
Install-PackageProvider -Name NuGet -Force
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Install-PackageProvider: No match was found for the specified search criteria for the provider 'NuGet'.
The package provider requires 'PackageManagement' and 'Provider' tags. Please check if the specified
package has the tags.
Run Code Online (Sandbox Code Playgroud)
我做了一些搜索,发现这个网站和其他网站上出现了这个错误的一些问题,一些答案说我需要强制 TLS 1.2:
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
Run Code Online (Sandbox Code Playgroud)
有的说指定-RequiredVersion,Install-PackageSource有的说使用-ForceBootstrap,有的说使用-Force。这些都不起作用,我每次仍然遇到同样的错误。Get-PackageProvider将 NuGet 列为提供程序。
我也无法使用 PowerShell Core 在 Windows 上安装 NuGet 提供程序,并出现相同的错误。PowerShell Core 是否不支持此功能?
我希望以下代码能够使用默认前景色打印详细文本:
$Host.PrivateData.VerboseForegroundColor = [console]::ForegroundColor
Write-Verbose 'Test' -Verbose
Run Code Online (Sandbox Code Playgroud)
但是,它照常打印黄色文本。更改错误前景色确实有效:
$Host.PrivateData.ErrorForegroundColor = [console]::ForegroundColor
Write-Error 'test'
Run Code Online (Sandbox Code Playgroud)
我发现规避这个问题的唯一方法是这样做:
Write-Verbose 'Test' -Verbose *>&1 | Write-Host
Run Code Online (Sandbox Code Playgroud)
但这并没有真正改变详细颜色,它只是强制它使用 Write-Host 作为默认文本直接打印到控制台主机。我确实知道 Write-Host 确实可以让您将消息颜色更改为您想要的任何颜色,但这并不是一个理想的解决方案。
假设我有以下 PowerShell源:
PowerShell shell = PowerShell.Create().AddCommand("Get-NetAdapter")
.AddParameter("name", "Ethernet*")
.AddParameter("ThrottleLimit", 5);
Run Code Online (Sandbox Code Playgroud)
现在,在 call 之前shell.Invoke(),为了记录目的,我想检查最终的命令行。在这种情况下,我希望像
Get-NetAdapter -name Ethernet* -ThrottleLimit 5
Run Code Online (Sandbox Code Playgroud)
我测试了这些,但没有一个工作:
shell.Commands.ToString()
shell.Commands.Commands.ToString()
shell.Commands.Commands.First().CommandText
shell.Commands.Commands.First().ToString()
Run Code Online (Sandbox Code Playgroud)
是否有一些内置的方法来检查最终的命令行?
当尝试使用 scoop via 更新 Powershell Core 时scoop update pwsh,出现以下错误:
ERROR Application "pwsh" is still running. Close all instances and try again.
我尝试关闭 PowerShell 并更新 via cmd,但它仍然引发该错误。
我想使用Powershell 5的PKI模块中的各种命令:
$ Get-Command -module PKI
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Add-CertificateEnrollmentPolicyServer 1.0.0.0 PKI
Cmdlet Export-Certificate 1.0.0.0 PKI
Cmdlet Export-PfxCertificate 1.0.0.0 PKI
Cmdlet Get-Certificate 1.0.0.0 PKI
Cmdlet Get-CertificateAutoEnrollmentPolicy 1.0.0.0 PKI
Cmdlet Get-CertificateEnrollmentPolicyServer 1.0.0.0 PKI
Cmdlet Get-CertificateNotificationTask 1.0.0.0 PKI
Cmdlet Get-PfxData 1.0.0.0 PKI
Cmdlet Import-Certificate 1.0.0.0 PKI
Cmdlet Import-PfxCertificate 1.0.0.0 PKI
Cmdlet New-CertificateNotificationTask 1.0.0.0 PKI
Cmdlet New-SelfSignedCertificate 1.0.0.0 PKI
Cmdlet Remove-CertificateEnrollmentPolicyServer 1.0.0.0 PKI
Cmdlet Remove-CertificateNotificationTask 1.0.0.0 PKI
Cmdlet Set-CertificateAutoEnrollmentPolicy 1.0.0.0 PKI
Cmdlet Switch-Certificate 1.0.0.0 PKI …Run Code Online (Sandbox Code Playgroud) 有没有办法在powershell中判断指定文件是否包含指定的字节数组(在任意位置)?
\n\n就像是:
\n\nfgrep --binary-files=binary "$data" "$filepath"\nRun Code Online (Sandbox Code Playgroud)\n\n当然,我可以写一个简单的实现:
\n\nfunction posOfArrayWithinArray {\n param ([byte[]] $arrayA, [byte[]]$arrayB)\n if ($arrayB.Length -ge $arrayA.Length) {\n foreach ($pos in 0..($arrayB.Length - $arrayA.Length)) {\n if ([System.Linq.Enumerable]::SequenceEqual(\n $arrayA,\n [System.Linq.Enumerable]::Skip($arrayB, $pos).Take($arrayA.Length)\n )) {return $pos}\n }\n }\n -1\n}\n\nfunction posOfArrayWithinFile {\n param ([byte[]] $array, [string]$filepath)\n posOfArrayWithinArray $array (Get-Content $filepath -Raw -AsByteStream)\n}\n\n// They return position or -1, but simple $false/$true are also enough for me.\nRun Code Online (Sandbox Code Playgroud)\n\n\xe2\x80\x94 但速度非常慢。
\n我正在尝试使用 powershell 在文件末尾添加多行。例如:该文件已经存在并且其中包含一些内容。
abc.txt
abc
def
ghi
jkl
mno
pqr
Run Code Online (Sandbox Code Playgroud)
所以 abc.txt 文件看起来就像我上面提到的那样。现在我尝试在文件末尾和新行添加以下内容。
qwe
asd
zxc
vbn
Run Code Online (Sandbox Code Playgroud)
最终输出将是 - abc.txt
abc
def
ghi
jkl
mno
pqr
qwe
asd
zxc
vbn
Run Code Online (Sandbox Code Playgroud)
由于我是一名新手,我尝试在已成功执行的文件中添加一行,但不知道添加多行。
Add-Content "./sample3.txt" "This is the last line `n"
Run Code Online (Sandbox Code Playgroud) powershell powershell-remoting powershell-3.0 powershell-core
我正在尝试使用 cli 获取我的产品的订阅密钥以及默认订阅密钥。我已浏览文档https://learn.microsoft.com/en-us/cli/azure/apim/api?view=azure-cli-latest,但现在我没有看到任何命令来获取我的信息订阅密钥。
\n虽然我可以看到有 powershell 方式来获取它,但我们在 ubuntu 管道中运行任务,并且下面列出的命令在 Linux 代理中不起作用。它说 Set-AzContext 不是已知命令
\n$subscriptionId = "id"\n$RG = "rg"\n$service = "apim-name"\n\nSet-AzContext -Subscription $subscriptionId\xc2\xa0\n\n$apimContext = New-AzApiManagementContext -ResourceGroupName $RG -ServiceName $service\n\nGet-AzApiManagementSubscriptionKey -Context $apimContext -SubscriptionId "master"\nRun Code Online (Sandbox Code Playgroud)\n更新\n我能够通过 DevOps 管道中的 Azure powershell 任务获取详细信息。如果 azure cli 中没有选项,我将使用它作为解决方法。
\nazure azure-api-management azure-cli azure-devops powershell-core
以下脚本在 5.1 和 7.4.0 上运行时给出不同的结果。
function foo($x) {
$val = switch ($x){
1 { 5 }
default {
return 10
}
}
Write-Host "Val is $val"
$val + 1
}
Write-Host "foo 1: $(foo 1)"
Write-Host "foo 2: $(foo 2)"
Run Code Online (Sandbox Code Playgroud)
PS 5.1 输出:
Val is 5
foo 1: 6
foo 2: 10
Run Code Online (Sandbox Code Playgroud)
PS 7.4.0 输出:
Val is 5
foo 1: 6
foo 2:
Run Code Online (Sandbox Code Playgroud)
这是 PS Core 中的错误(这对我来说肯定是出乎意料的)还是这是一个记录在案的更改/始终是未定义的行为?
powershell-core ×10
powershell ×8
azure ×2
.net-core ×1
azure-cli ×1
azure-devops ×1
binaryfiles ×1
c# ×1
colors ×1
grep ×1
nuget ×1
verbose ×1