标签: powershell

如何从文本文件中提取特定行

我有一个大日志文件 (user.log) ,例如:

   2019-10-02 00:03:55.407095
   2019-10-02 00:03:55.410345
   2019-10-02 00:03:55.410765
   2019-10-02 00:03:55.411187
   2019-10-02 00:03:55.411791
   2019-10-02 00:03:55.412657
Run Code Online (Sandbox Code Playgroud)

我如何从日志文件中提取第 3 行作为;

2019-10-02 00:03:55.410765

通过使用powershell脚本?

powershell line-numbers text-files

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

如何使用 Powershell 查找、停止和禁用 Windows 服务

我试图找到一项服务,停止它,然后使用 Powershell 远程禁用它。它可以找到并停止,但不能禁用。为了禁用,我必须Set-Service单独运行命令。可以在一行中完成吗?

以下代码片段将停止Print Spooler服务,但不会禁用它:

$ip = "10.10.10.10"
$svc_name = "Spooler"
get-service -ComputerName $ip | Where-Object {$_.Name -eq $svc_name} |  Stop-Service | Set-Service -StartupType  Disabled
Run Code Online (Sandbox Code Playgroud)

以下代码片段将停止并禁用Print Spooler服务:

$ip = "10.10.10.10"
$svc_name = "Spooler"
get-service -ComputerName $ip | Where-Object {$_.Name -eq $svc_name} |  Stop-Service
Set-Service $svc_name -StartupType  Disabled
Run Code Online (Sandbox Code Playgroud)

Powershell 版本是5.1.14393.2969.

编辑: 以下行也将查找和禁用。所以,看起来我可以用管道给出两条指令。

get-service -ComputerName $ip | Where-Object {$_.Name -eq $svc_name} | Set-Service -StartupType  Disabled
Run Code Online (Sandbox Code Playgroud)

windows powershell service

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

在 powershell 模块函数中发出错误的正确方法是什么?

我已经阅读了很多关于 powershell 错误处理的内容,现在我对在任何给定情况下应该做什么(错误处理)感到非常困惑。我正在使用 powershell 5.1(不是核心)。话虽如此:假设我有一个模块,它的功能看起来像这个模拟:

function Set-ComputerTestConfig {
  [CmdletBinding()]
  param(
    [Parameter(Position=0, Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [string] $Name)

begin { ... }
process { 
 # task 1
 # task 2 => results in a failure that prevents further tasks
 # task 3
 # task 4
}
end { ... }
Run Code Online (Sandbox Code Playgroud)

假设对于我传递给此函数的每台计算机名称,我有 4 个任务要完成,但如果其中任何一个任务失败,我将无法继续执行剩余的任务。我应该如何产生错误(最佳实践),以便它停止此特定计算机名称的“进程”,但有效地继续处​​理管道?

powershell powershell-5.0

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

如何将参数传递给所有 pester 测试脚本

该Invoke-Pester命令可以使用-Script参数调用具有显式参数的单个测试脚本。但是如果我想将相同的参数传递给所有的测试脚本呢?我不想在循环中调用 peter,因为我希望它生成单个测试结果文件。

那么,我们该怎么做呢?

powershell pester

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

为 Powershell 脚本创建一个协议处理程序

如何为 powershell 脚本创建协议处理程序并使目标 powershell 脚本接收命令行参数?

这样做的安全问题是什么?

windows powershell

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

PowerShell 如何将 Get-ChildItem 输出转换为字符串数组

好吧,已经为此工作了几个小时并疯狂地研究,但仍然没有得到任何工作。我需要string[]创建一个对象get-childitem来传递给Copy-Item -exclude参数。

障碍是我需要进行递归并且需要有相对路径,所以这就是我想出的:

$((Get-ChildItem -Path $Dest -Recurse -File).FullName.TrimStart($Dest))
Run Code Online (Sandbox Code Playgroud)

这会生成一个干净的现有文件列表,$dest其中显示了$dest. 问题是,如果我将copy-item -exclude它添加到参数中,它似乎会忽略它。在线进一步研究表明,如果参数不是 type ,copy-item则将忽略该-exclude参数string[]。

如果我检查上述命令返回的类型,我会得到System.Object[]. 我希望它是System.String[]或只是简单的String[]。

如何将上述命令的输出转换为字符串数组?

copy-item为清楚起见,使用 的完整命令是:

Copy-Item -Path (Join-Path $src "*") -Destination $dest -Recurse -Force -Exclude $((Get-ChildItem -Path $Dest -Recurse -File).FullName.TrimStart($Dest))
Run Code Online (Sandbox Code Playgroud)

我的最终目标是递归复制文件而不覆盖现有文件。

arrays string powershell

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

为什么 dotnet 测试工具没有创建代码覆盖率报告?

我的项目结构如下:

.
??? Application
??? Application.Core
??? Application.Domain
??? Application.Tests
??? Application.IntegrationTests
??? README.md
Run Code Online (Sandbox Code Playgroud)

运行我的测试的命令我使用以下输入:

dotnet test $folder.FullName -c Release --no-build /p:CoverletOutput='$root\coverage' /p:MergeWith='$root\coverage.json' /p:CoverletOutputFormat=opencover 
Run Code Online (Sandbox Code Playgroud)

这里$folder是指向我Application.Tests路径:C:\projects\coredemoapp\CoreDemoApp.Tests与$root以C:\projects\coredemoapp\

所有测试都成功运行。然而,问题是,coverage.json也不是coverage.opencover.xml没有创建的文件。

我也尝试coverlet直接使用以下命令:

coverlet <path to test.dll> -
-target "dotnet" --targetargs "test --no-build" --merge-with $root\coverage.json --format opencover
Run Code Online (Sandbox Code Playgroud)

,一切都在我的本地机器上运行,并具有指向 dll 的显式路径。但是,在使用前一个$folder.FullName作为运行命令的情况下<path to assembly>,coverlet 假定调试dll的路径,而不是它应该的发布版本并且失败。

链接到完整的构建脚本

powershell dotnet-test coverlet

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

如何从 Azure 命令“powershell 中的 az group list”的“结果”数组中只选择一个属性?

我正在尝试从az group listpowershell 中的 Azure 命令返回的“结果”(对象?)数组中仅选择一个属性?

我知道这听起来微不足道,但这就是奇怪的地方,我希望有一个简单的解释。

如果我运行 Azure 命令az group list -o table(在我使用成功登录后az login),我会得到以下典型响应

PS src> az group list -o table             
Name              Location    Status
----------------  ----------  ---------
group0            westeurope  Succeeded
group1            westeurope  Succeeded
group2            uksouth     Succeeded
group3            westeurope  Succeeded
group4            westeurope  Succeeded
group5            westeurope  Succeeded
group6            westeurope  Succeeded
group7            uksouth     Succeeded
group8            westeurope  Succeeded
group9            westeurope  Succeeded
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试Name通过执行仅选择属性

az 组列表 | 选择 -p 名称

然后我得到大约 2 个充满空行的屏幕,没有显示任何内容。那么问题来了,上面的命令有什么问题?我应该如何修复它?

我尝试了以下实验来深入研究返回的对象的确切类型,并得到一些我不明白的结果。我希望这对拥有更多 Azure 和 Powershell …

powershell azure

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

在 Where-Object 条件之后获取第一个值

给出以下命令如何仅获取where条件后的第一个值?

Get-WinEvent -FilterHashtable @{Logname = 'Security';EndTime=$Time; ID = 5379} |
  select id , TimeCreated, @{ n='USER'; E ={ $_.Properties[1].Value}} |
  where USER -notlike 'condition'
Run Code Online (Sandbox Code Playgroud)

我试过了-MaxEvents 1,-First 1但这仅适用于Select-Object,不适用于where.

powershell

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

在 GitHub 操作中从节点程序运行 Powershell 脚本

嗯,是的,这是一回事。在尝试直接运行 PS 脚本并失败后,让我们尝试从节点脚本运行它的迂回方式,这在 GitHub 操作环境中是合法的。所以经过多次尝试,这是我的最后一个(取自这个答案:

var spawn = require("child_process").spawn,child;
var workspace = process.env.GITHUB_WORKSPACE;
var file = workspace + "\\upgrade.ps1";
console.log( "Workspace ", workspace,  " file ", file );
child = spawn("powershell.exe",[ file ]); // more stuff to print output after this
Run Code Online (Sandbox Code Playgroud)

这失败了:

Workspace  d:\a\rakudo-star-fix-action\rakudo-star-fix-action  file  d:\a\rakudo-star-fix-action\rakudo-star-fix-action\upgrade.ps1
Powershell Errors: d:\a\rakudo-star-fix-action\rakudo-star-fix-action\upgrade.ps1 : The term 

Powershell Errors: 'd:\a\rakudo-star-fix-action\rakudo-star-fix-action\upgrade.ps1' is not recognized as the name of a cmdlet, function, 
script file, or operable program. Check the spelling of …
Run Code Online (Sandbox Code Playgroud)

powershell node.js github-actions

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