标签: powershell

powershell 哈希表键与对象属性

我对 PowerShell 如何处理 Hashtable 键与对象属性感到困惑。

考虑:

$list = @{
    'one' = @{
        name  = 'one'
        order = 80
        };

    'two' = @{
        name  = 'two'
        order = 40
        };

    'twotwo' = @{
        name  = 'twotwo'
        order = 40
        };

    'three' = @{
        name  = 'three'
        order = 20
        }
    }

$list.Values|group-object { $_.order }
$list.Values|group-object -property order
Run Code Online (Sandbox Code Playgroud)

第一个组对象给了我我期望的(三个组),第二个没有(一个大组)。显然 Hashtable 键不是对象属性,但在语法上它们以相同的方式(var.name)被引用。

第二个组对象实际上在做什么?
它认为“订单”属性是什么?

powershell

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

如何在 Powershell 上禁用个人和系统配置文件加载时间消息

如何禁用 Powershell 启动时显示的此消息?

消息是:

加载个人和系统配置文件需要 xxx 毫秒

我知道它显示配置文件加载时间很慢。
但是,我对我的个人资料加载时间感到满意。

有什么办法可以禁用此消息吗?

请注意,此消息似乎仅在 Powershell 5.1+ 上显示。

我使用:Powershell 5.1 Windows10 1709

windows powershell windows-10

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

使用 PowerShell 在计算机上查找特定的 .exe 文件

因此,在我目前正在实习的公司中,他们有大约 20 台装有公司指定软件的“技术笔记本电脑”。它们位于 C:\ 目录中。

例如:

aaa.exe 位于 C:\aaa\aaa.exe

bbb.exe 位于 C:\bbb\bbb.exe

我真正需要的是一个 .ps1 脚本来在列表中显示这些特定的可执行文件。

到目前为止我所拥有的是: Get-ChildItem C:\ -recurse | where {$_.extension -eq ".exe"}

现在,我相信它只是编辑搜索查询,因为这给了我 C:\ 驱动器上的所有 .exe 文件,我只需要aaa和bbb。

仅供参考:我需要该脚本,以便我可以使用它来监视 zabbix 代理并查看特定软件是否在计算机上,以便我可以在 Zabbix 服务器上运行此脚本。

windows shell powershell monitoring

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

使用 Powershell 使用 Azure VM 创建自动关机策略

我正在尝试使用 Powershell 为我的 Azure VM 创建自动关闭策略,但一直遇到此错误:

New-AzureRmResource : MissingRequiredProperty :缺少必需的属性 TargetResourceId。在 C:\Users\home\Documents\CreateAzureVM.ps1:167 char:1 + New-AzureRmResource -Location $Loc -ResourceId $ScheduledShutdownReso ... + ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ + CategoryInfo : CloseError: (:) [New-AzureRmResource], ErrorResponseMessageException +fullyQualifiedErrorId : MissingRequiredProperty,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceCmdlet

我对如何解决这个错误一无所知,到目前为止,这是我的脚本:

 $SubscriptionId = $AzContext.Context.Subscription.Id;  
$VMResourceId = (Get-AzureRmVM).id
$ScheduledShutdownResourceId = "/subscriptions/$SubscriptionId/resourceGroups/$RSGName/providers/microsoft.devtestlab/schedules/shutdown-computevm-$VMName"

$Properties = @{}
$Properties.Add('status', 'Enabled')
$Properties.Add('taskType', 'ComputeVmShutdownTask')
$Properties.Add('dailyRecurrence', @{'time'= 1159})
$Properties.Add('timeZoneId', "Eastern Standard Time")
$Properties.Add('notificationSettings', @{status='Disabled'; timeInMinutes=15})
$Properties.Add('targetResourceId', $VMResourceId)

#Error
New-AzureRmResource -Location $Loc -ResourceId $ScheduledShutdownResourceId -Properties $Properties -Force
Run Code Online (Sandbox Code Playgroud)

powershell virtual-machine azure

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

Powershell - 无法将“System.Management.Automation.PSCustomObject”类型的值转换为“System.Management.Automation.PSCustomObject”类型

我正在 Windows 7 下使用 Powershell 4,我的 cmdlet 定义如下:

 Function Transfer-File {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)]
        [System.Management.Automation.PSCustomObject]
        $source,

        # other parameters removed for brevity
    )

    # actual logic does not matter 

  }
Run Code Online (Sandbox Code Playgroud)

我这样称呼它:

$hasError = Transfer-File -source $source -destination $cfg.Destination -logfile $logFile
Run Code Online (Sandbox Code Playgroud)

$source 是通过解析 JSON 文件获得的,如下所示:

@{Path=some path string here; Pack=False}
Run Code Online (Sandbox Code Playgroud)

Write-Host $source.GetType() 输出:

System.Management.Automation.PSCustomObject
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

无法将“System.Management.Automation.PSCustomObject”类型的“@{Path=some path string here; Pack=False}”值转换为“System.Management.Automation.PSCustomObject”类型。

在绝望地尝试凭经验解决它时,我替换System.Management.Automation.PSCustomObject了psobject它,它似乎工作正常。

问题:为什么我的System.Management.Automation.PSCustomObject似乎不适合System.Management.Automation.PSCustomObjectcmdlet 原型?


创建对象的完整代码:

### Configuration ###
$cfg = New-Object –TypeName PSObject …
Run Code Online (Sandbox Code Playgroud)

powershell type-conversion cmdlet powershell-4.0

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

如何在 PowerShell 中获取 CPU 功耗?

有没有办法使用PS获得CPU功耗?Open Hardware Monitor显然有办法返回该数字,但我无法在OHM 的 source 中找到相关代码。

windows powershell

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

在 Windows Server 2016 中使用命令行获取 exe 的“版权”信息字段

我有兴趣使用 cmd 或 PowerShell 获取“版权”信息字段。手动,它可以通过

RighClick the executable -> Properties -> Details -> Copyright

我知道wmic datafile命令,

wmic 数据文件,其中 Name="C:\Windows\System32\cmd.exe" 获取制造商、名称、版本

但它没有提供Details文件属性选项卡中的大部分详细信息。

如何使用 cmd 或 PowerShell 获取此信息?我相信Python应该能够轻松解决它,但有时在生产服务器上安装python不是一个选择。

powershell cmd file-properties

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

从外部 Windows 命令实用程序捕获异常

我试图在运行外部命令时终止脚本导致错误。考虑这个简单的代码:

try {
    where.exe Test-App
}

catch {
    Write-Error "Exception caught." -ErrorAction Continue
    throw
}

Write-Host "Test message!"
Run Code Online (Sandbox Code Playgroud)

输出:

where.exe : INFO: Could not find files for the given pattern(s).
At line:2 char:5
    where.exe Test-App
...
Test message!
Run Code Online (Sandbox Code Playgroud)

当外部命令导致错误时,是否可以进入 catch 块并抛出?

期望的输出:

C:\Scripts\Test-Script.ps1 : Exception caught.
Run Code Online (Sandbox Code Playgroud)

powershell exception-handling

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

Powershell 显示重复文件

我有一项任务是检查当天是否在共享位置文件夹中导入了新文件,并提醒是否有任何重复文件并且不需要递归检查。

下面的代码显示大小为 1 天的所有文件详细信息但是我只需要大小相同的文件,因为我无法使用名称比较它们。

$Files = Get-ChildItem -Path E:\Script\test |
Where-Object {$_.CreationTime -gt (Get-Date).AddDays(-1)}

$Files | Select-Object -Property Name, hash, LastWriteTime, @{N='SizeInKb';E={[double]('{0:N2}' -f ($_.Length/1kb))}}
Run Code Online (Sandbox Code Playgroud)

powershell

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

通过 Powershell 为证书的私钥分配权限(Win 2012 R2)

在连接到域的 Windows Server 2012 R2 机器上,我正在运行以下语句:

$target_machine_fqdn = [System.Net.Dns]::GetHostByName($env:computerName)

$certificate_request = Get-Certificate `
    -Template 'AcmeComputer' `
    -DnsName $target_machine_fqdn `
    -CertStoreLocation 'Cert:\LocalMachine\My'
Run Code Online (Sandbox Code Playgroud)

我正在向域的 CA 请求主机证书。该语句返回没有错误。为机器生成证书并根据要求将其放置在“Cert:\LocalMachine\My”中。

问题:我不知道如何向服务帐户授予证书私钥的权限。

现在,大约有 1,000篇文章通过检索UniqueKeyContainerName以如下开头的代码来指导人们如何授予权限:

$certificate.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName
Run Code Online (Sandbox Code Playgroud)

这在这里行不通。虽然证书有私钥,但私钥数据成员为空:

在此处输入图片说明

在我刚刚逃避的解决方案有效的情况下,私钥在文件系统上。但是,在这种情况下,私钥位于以下注册表中:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\SystemCertificates\MY\Keys
Run Code Online (Sandbox Code Playgroud)

使用证书 MMC-snapin 时,我可以看到证书。我可以管理私钥的权限。所以,我知道它在那里。不幸的是,我需要自动分配权限,因此不能选择使用证书 MMC-snapin。我需要通过 Powershell 以某种方式执行此操作。

windows powershell cryptography x509certificate

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