标签: powershell

枚举PSCustomObject作为键/值对

我正在调用Invoke-RestMethod一个返回JSON的URI,如下所示:

{
  "01": {"ID":1, "Name":"Foo"},
  "02": {...},
  "03": {...}
}
Run Code Online (Sandbox Code Playgroud)

我最终得到PSCustomObject的属性名称是键中的数字,值是对象图,但我想将对象视为键/值对的列表(即:字典).我试过了:

(Invoke-RestMethod -Uri $uri) | foreach-object {
  $_.ID
  $_.Key.ID
}
Run Code Online (Sandbox Code Playgroud)

等等; 但后来意识到这Foreach-Object只是迭代一次; 来自的返回值Invoke-RestMethod不是IEnumerable

如何在结果对象中获取属性和值的集合?

powershell

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

如何在CmdletBinding()脚本中定义函数?

我正在编写一个脚本,我想使用PowerShell的CmdletBinding().有没有办法在脚本中定义函数?当我尝试时,PowerShell在表达式或语句中抱怨"意外的toke'函数"

这是我正在尝试做的简化示例.

[CmdletBinding()]
param(
    [String]
    $Value
)

BEGIN {
    f("Begin")
}

PROCESS {
    f("Process:" + $Value)
}

END {
    f("End")
}

Function f() {
    param([String]$m)
    Write-Host $m
}
Run Code Online (Sandbox Code Playgroud)

在我的情况下,编写模块是浪费的开销.这些函数只需要可用于这一个脚本.我不想弄乱模块路径或脚本位置.我只想运行一个包含其中定义的函数的脚本.

powershell pipeline function cmdlet

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

在Windows上,如何杀死目录中打开文件的所有进程?

Windows的一个非常令人沮丧的"功能"是进程锁定文件并阻止删除目录.

我正在寻找一种方法来查找在该目录中打开文件的所有进程,例如unix上的"lsof".我正在寻找一些powershell魔法,而我宁愿不必购买一些自定义维护工具.

windows powershell

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

使用特殊名称启动进程,或使用句柄来终止它

我们有许多node.js程序,都是用PowerShell创建的.我们使用start-process在后台生成node.js可执行文件(node.exe).

我们想给每个人一个不同的"句柄",以便我们以后可以使用powershell命令stop-process来杀死它们.例如,start-process -Name'wServer'要删除的powershell脚本将不同于要启动的脚本,并且不同的用户可能会使用它.

我坚持如何识别每个node.exe不同.它不是可执行文件,而是app.js的路径

以下是我们如何开始其中之一:

$reg = Get-Item -Path "hklm:\SOFTWARE\us\EGPL\GeoLibrarian" 
$path = $reg.GetValue('LibrarianPath').ToString() 
$sixtyfour = [Environment]::Is64BitProcess

# Now start running Watson
$node = "C:\Program Files\nodejs\node.exe" 
$arg = "app.js"
$dir = "$path\Watson"
start-process -WorkingDirectory $dir $node $arg
Write-Host "64-Bit Powershell: "$sixtyfour
Write-Host "PowerShell Version: "$PSVersionTable.PSVersion
Write-Host "Watson running in background"
Run Code Online (Sandbox Code Playgroud)

现在,我可以通过这个序列杀死以独特窗口开始的那些,我不认为在powershell中启动的窗口会有一个窗口.

Write-Host "Kill watson task"
$watson = get-process | where-object {$_.MainWindowTitle -eq 'WatsonJS'}
Stop-Process -Id $watson.Id -ErrorAction SilentlyContinue
Run Code Online (Sandbox Code Playgroud)

powershell powershell-3.0

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

当一个列表可能只是Get-Item中的一项时,如何在PowerShell中合并两个“列表”?

我正在使用Get-ChildItem来获取本地安装的证书。然后,我想从本地证书存储中删除这些证书。如果我执行下面的脚本,则在两个Get-ChildItem查询中返回一个以上的项目时,它将起作用。但是,如果在任何一个查询中仅返回一项,则两个集合的添加将失败。仅仅因为只返回一个对象,实现只返回一个实例,而不是一个实例的集合。这里正确的方法是什么?我不知道查询返回的结果是否会导致0、1或多个证书。

$myCerts = Get-ChildItem cert:\CurrentUser\My | Where-Object { $_.Subject -like "CN=$certName" }
$trustedPeopleCerts = Get-ChildItem cert:\CurrentUser\TrustedPeople | Where-Object { $_.Subject -like "CN=$certName" }

$allCerts = $myCerts + $trustedPeopleCerts

foreach ($cert in $allCerts) {
    $store = Get-Item $cert.PSParentPath
    $store.Open('ReadWrite')
    $store.Remove($cert)
    $store.Close()
}
Run Code Online (Sandbox Code Playgroud)

powershell get-childitem

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

将环境变量转换为数组

我需要将数组传递给PowerShell子进程,并且想知道如何将环境变量(字符串)转换为PowerShell数组。我是否需要遵循一个约定,PowerShell会自动为我完成此约定,还是我自己需要对其进行解析?

我正在寻找bash可以做的事情。如果我设置一个环境变量,例如:

MYARR = one two three
Run Code Online (Sandbox Code Playgroud)

它会自动解释bash为数组,因此我可以这样做:

for a in ${MYARR[@]} ; do
    echo Element: $a
done
Run Code Online (Sandbox Code Playgroud)

那将返回:

Element: one
Element: two
Element: three
Run Code Online (Sandbox Code Playgroud)

有什么办法可以在PowerShell中执行相同操作吗?

powershell

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

创建powershell参数默认值为当前目录

我希望创建一个参数,其默认值是'当前目录'(.).

例如,Path参数Get-ChildItem:

PS> Get-Help Get-ChildItem -Full
Run Code Online (Sandbox Code Playgroud)

-Path指定一个或多个位置的路径.允许使用通配符.默认位置是当前目录(.).

    Required?                    false
    Position?                    1
    Default value                Current directory
    Accept pipeline input?       true (ByValue, ByPropertyName)
    Accept wildcard characters?  true
Run Code Online (Sandbox Code Playgroud)

我创建了一个带有Path参数的函数,该参数接受来自管道的输入,默认值为.:

<#
.SYNOPSIS
Does something with paths supplied via pipeline.
.PARAMETER Path
Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).
#>
Function Invoke-PipelineTest {

    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
        [string[]]$Path='.'
    )
    BEGIN {}
    PROCESS {
      $Path …
Run Code Online (Sandbox Code Playgroud)

powershell powershell-3.0

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

从带有通配符的路径获取实际路径

Test-Path在if语句中使用时,我希望获得if语句成功的路径.

例如,这些文件存在于C中:

C:\Test6_1_15.txt
C:\Test6_2_15.txt
C:\Test6_3_15.txt
C:\Test6_4_15.txt
Run Code Online (Sandbox Code Playgroud)

我在"then"分支怎么办?

$Path = "C:\Test6_*_15.txt"
if (Test-Path $Path)
{
   # if test passes because there are 4 files that fit the test, but I want to be
   # able to output the file that made the if statement succeed. 
}
Run Code Online (Sandbox Code Playgroud)

powershell if-statement

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

有"Sum"功能吗?

我有以下代码片段:

> 1..10|%{$count=0}{$count+=$_}{$count}
55
> 1..10|Sum
Run Code Online (Sandbox Code Playgroud)

最后一行显然显示错误,因为没有"Sum"功能.有没有一种方便的方法来汇总PowerShell中的项目?

powershell sum

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

如何在powershell中写一个-replace,其中一个数字跟在序列之后

例如,假设我想取字符串Hello World并使用a -replace来输出"World1".这是我对表达的尝试:

"Hello World" -replace '.*(World)','$11'
Run Code Online (Sandbox Code Playgroud)

但问题是,它看到11美元作为第11个序列,而不是1美元,然后是1.我已经尝试搜索转义字符来指定我想要1跟随1美元,但没有找到任何东西.

我试图解决的现实问题是获取一堆电子邮件地址并在最后创建带有数字的别名.例如

jsmith@example.com
Run Code Online (Sandbox Code Playgroud)

变

jsmith1@example.com
jsmith2@example.com
Run Code Online (Sandbox Code Playgroud)

regex powershell

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