标签: powershell

Powershell 向上舍入功能

我想在 Powershell 中四舍五入到两位小数。

我从双“178.532033”开始。如果我使用 Excel ROUNDUP 函数保留两位小数,我会得到“178.54”。

=ROUNDUP(A1,2)
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用 Powershell 中 Math 类中包含的 Round 函数,我会得到结果“178.53”(因为我是四舍五入而不是四舍五入):

$value = 178.532033

Write-Output = ([Math]::Round($value, 2))
Run Code Online (Sandbox Code Playgroud)

有没有办法在Powershell中四舍五入到两位小数?

math powershell rounding

3
推荐指数
3
解决办法
2万
查看次数

运行powershell脚本时VSC抛出错误

当我在 Visual Studio Code 中打开 Powershell 脚本时,刚开始出现以下错误。直到昨天它都运行良好。这是错误:

The language service could not be started: 
Error: The language client requires VS Code version ^1.26 but recevied version 1.25.1
Run Code Online (Sandbox Code Playgroud)

我用谷歌搜索它找到了一个页面,上面写着将 package.json 文件更改为旧版本,但我找不到那个文件,我正在使用 macos。

powershell visual-studio-code

3
推荐指数
1
解决办法
679
查看次数

如何使用 Powershell 连接到本地 Azure 存储模拟器?

我正在尝试编写一个 powershell 脚本来创建我所有的 azure 存储容器和东西,而不是通过一些 GUI 手动创建它们。

我正在查看以下文档网站,但看不到连接到我本地 azure 存储服务的方法 - 只有实际的 azure 服务。

https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-powershell

有人知道如何连接到本地存储模拟器吗?

powershell azure azure-storage

3
推荐指数
1
解决办法
1090
查看次数

powershell 更新帮助失败

我刚刚开始从 Microsoft 虚拟学院学习 PowerShell,并且我正在运行指示的命令之一。虽然在他们的最后它起作用了,但我的却没有。我确实四处寻找解决此问题的方法。我只是不知道出了什么问题。任何提示都会对这个新的 PowerShell 学习者有所帮助。

PS C:\Windows\system32> Update-Help -Force
Update-Help : Failed to update Help for the module(s) 'WindowsUpdateProvider' with UI culture(s) {en-US} : Unable to retrieve the HelpInfo XML file for UI culture en-US. Make sure the
HelpInfoUri property in the module manifest is valid or check your network connection and then try the command again.
At line:1 char:1
+ Update-Help -Force
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [Update-Help], Exception
    + FullyQualifiedErrorId : UnableToRetrieveHelpInfoXml,Microsoft.PowerShell.Commands.UpdateHelpCommand

PS C:\Windows\system32> $PSVersionTable …
Run Code Online (Sandbox Code Playgroud)

powershell powershell-v5.1

3
推荐指数
1
解决办法
3273
查看次数

PowerShell:在传递给外部程序的字符串中转义双引号的最佳方法?例如,一个 JSON 字符串

我阅读了我认为所有关于在 PowerShell 中转义字符串的文章,但我仍然没有找到让我满意的解决方案。

假设我有一个foo.json包含 JSON 字符串的文件。我需要将 JSON 字符串作为参数传递给程序。

在 bash 中,这很好用:

myprogram "$(cat ~/foo.json)"

在 PowerShell 中,当我正常读取文件内容并将其传入时,接收程序会抱怨键和值周围没有引号。

我想出的是:

$json = (get-content ~/foo.json | Out-String) -replace '"','""' myprogram $json

在 PowerShell 中是否有一种不那么尴尬的方法来做到这一点?我已经退出会话,在 bash 中运行命令,然后再次启动会话。

powershell json

3
推荐指数
1
解决办法
3166
查看次数

在电子邮件中嵌入图像并通过 Powershell 发送

我正在编辑我的一个旧脚本,向用户发送一封电子邮件,其中嵌入了图像。我正在尝试使用 Send-MailMessage 函数来发送电子邮件,而不是旧的 $smtp.send($msg) 方法。但是,当尝试更新脚本时,图像不再被嵌入。

我知道如何将它们作为实际附件附加到电子邮件中,但我不确定将它们显示为实际嵌入图像我做错了什么。

注意:为简洁起见,我删除了一些完整的电子邮件,因为它很大,只要我能得到一两个图像,它都会起作用。

# force powershell to run as an x86 process
    Set-ExecutionPolicy -Scope CurrentUser Unrestricted
    if ($env:Processor_Architecture -ne "x86") {
        &"$env:windir\syswow64\windowspowershell\v1.0\powershell.exe" -file $myinvocation.Mycommand.path
        exit
    }

# initialize the script
    if ($startupvariables) { try {Remove-Variable -Name startupvariables  -Scope Global -ErrorAction SilentlyContinue } catch { } }
    New-Variable -force -name startupVariables -value ( Get-Variable | ForEach-Object { $_.Name } )
    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
    $Separator = ".", "@"

# advise what the script does
    Add-Type -AssemblyName PresentationCore,PresentationFramework
    $ButtonType …
Run Code Online (Sandbox Code Playgroud)

html powershell

3
推荐指数
1
解决办法
2万
查看次数

如何为 Azure CLI 使用 Powershell splatting

我想使用Powershell splatting有条件地控制用于某些 Azure CLI 调用的参数。专门用于创建 CosmosDb 集合。

目标是这样的:

$params = @{
    "db-name" = "test";
    "collection-name"= "test2";
    # makes no difference if I prefix with '-' or '--'
    "-key" = "secretKey";
    "url-connection" = "https://myaccount.documents.azure.com:443"
    "-url-connection" = "https://myaccount.documents.azure.com:443"
}

az cosmosdb collection create @params
Run Code Online (Sandbox Code Playgroud)

不幸的是,这只适用于db-name和collection-name。其他参数失败并显示此错误:

az : ERROR: az: error: unrecognized arguments: --url-connection:https://myaccount.documents.azure.com:443 
--key:secretKey
Run Code Online (Sandbox Code Playgroud)

powershell azure azure-cli

3
推荐指数
1
解决办法
492
查看次数

检查是否安装了特定版本的程序

我正在尝试创建一个非常简单的脚本,该脚本将检查是否安装了特定程序,如果安装了,则返回该程序的版本号。

我已经能够运行脚本并能够在安装与否的情况下返回二进制值,但不确定如何返回该已安装程序的版本号。

如果安装了程序,我将发布的内容将是我正在做的返回,并且需要帮助才能获得版本号。

function Check_Program_Installed {
    $my_check = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
                Select-Object DisplayName, DisplayVersion, InstallDate |
                Format-Table -AutoSize |
                Out-String

    # Check if Google Chrome is installed
    $my_check -Match "Google Chrome"
}

Check_Program_Installed
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

windows powershell scripting installed-applications

3
推荐指数
2
解决办法
6079
查看次数

列出来自密钥保管库的纯文本的所有机密

我知道您可以使用如下命令获取 azure Key Vault 中特定机密的纯文本:

$a = Get-AzureKeyVaultSecret -VaultName MyKeyVaultName -Name "SecretName"
$a.SecretValueText
Run Code Online (Sandbox Code Playgroud)

但是是否可以列出所有秘密和纯文本值?这仅显示秘密值的空白字符串:

Get-AzureKeyVaultSecret -VaultName MyKeyVaultName | Select-Object Name,SecretValueText
Run Code Online (Sandbox Code Playgroud)

powershell azure azure-powershell azure-keyvault

3
推荐指数
1
解决办法
3740
查看次数

Powershell $MyInvocation.MyCommand.Path 在 exe 中转换脚本时返回 null

我创建了我的 ps1 脚本并动态获取路径我使用了这个命令

$ScriptPath = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
Run Code Online (Sandbox Code Playgroud)

当我使用 ISE 控制台时一切正常,但是当我尝试使用 ps2exe 将我的 ps1 脚本转换为 exe 时,当我执行我的 exe 文件时出现错误,因为 $ScriptPath 返回空值。
我试过这种方式但没有成功

$ScriptPath = Split-Path -Parent $PSCommandPath
Run Code Online (Sandbox Code Playgroud)

我希望脚本与版本 2 兼容。
我该如何解决?
谢谢

powershell

3
推荐指数
1
解决办法
1522
查看次数