Соответствие регулярному выражению для первой строки вывода ошибок Powershell - PullRequest
1 голос
/ 09 января 2020

Я использую new-pssession для подключения к машинам и храню любые ошибки в значении, указанном параметром -errorVariable. Я хочу вызвать эту переменную позже для вывода, но хочу показать первую строку, за исключением имени компа в скобках.

Вот пример того, что я получаю.

New-PSSession : [ABCGFAXYZUT579] Connecting to remote server ABCGFAXYZUT579 failed with the 
following error message : WinRM cannot complete the operation. Verify that the 
specified computer name is valid, that the computer is accessible over the network, and that a 
firewall exception for the WinRM service is enabled and allows access from this 
computer. By default, the WinRM firewall exception for public profiles limits access to remote 
computers within the same local subnet. For more information, see the
about_Remote_Troubleshooting Help topic.
At line:1 char:1
+ New-PSSession -ComputerName ABCGFAXYZUT579 -ErrorVariable errors912  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New- 
PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : WinRMOperationTimeout,PSSessionOpenFailed

Я пытался передать переменную в строку выбора, чтобы получить только то, что мне нужно, но все, что я пробовал, все еще дает мне

[ABCGFAXYZUT579] Connecting to remote server ABCGFAXYZUT579 failed with the following error 
message : WinRM cannot complete the operation. Verify that the specified computer name 
is valid, that the computer is accessible over the network, and that a firewall exception for 
the WinRM service is enabled and allows access from this computer. By default, the 
WinRM firewall exception for public profiles limits access to remote computers within the same 
local subnet. For more information, see the about_Remote_Troubleshooting Help topic.

Я также попытался разбить его на отдельные строки, подобные этой ($errors912 -split '\r?\n' -ne ''), и передать в строку выбора, но я все еще получаю возвращенное выше.

Есть ли простой способ получить все после первого пробела и до первого периода? оставив это?

Connecting to remote server ABCGFAXYZUT579 failed with the following error 
message : WinRM cannot complete the operation.

1 Ответ

1 голос
/ 10 января 2020
$NewSession = New-PSSession -ComputerName ABCGFAXYZUT579 -ErrorVariable errors912 -ErrorAction SilentlyContinue
if ( $errors912.Count -gt 0 ) {
    [string]$errors912 -replace "^(\[.*?\] )|(?<=\.).*"
}
Connecting to remote server ABCGFAXYZUT579 failed with the following error message : The WinRM client cannot process the request.

Условие $errors912.Count -gt 0 эквивалентно $null -eq $NewSession.

Пожалуйста, go для некоторых он-лайн тестеров / отладчиков регулярных выражений, например https://regex101.com/ для объяснения регулярного выражения.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...