powershell-5.0

How to convert to UInt64 from a string in Powershell? String-to-number conversion

风流意气都作罢 提交于 2020-03-19 06:28:23
问题 Consider the following Powershell snippet: [Uint64] $Memory = 1GB [string] $MemoryFromString = "1GB" [Uint64] $ConvertedMemory = [Convert]::ToUInt64($MemoryFromString) The 3rd Line fails with: Exception calling "ToUInt64" with "1" argument(s): "Input string was not in a correct format." At line:1 char:1 + [Uint64]$ConvertedMemory = [Convert]::ToUInt64($MemoryFromString) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [],

How to run powershell script using task scheduler in silent/hidden mode?

佐手、 提交于 2020-03-18 05:35:08
问题 I am trying to run powershell script using task scheduler and it pops up powershell cmd window. Is there a way to disable this while running the script? I am on powershell v5.0 I've tried -WindowStyle Hidden but still powershell cmd window pops up. Any guidance welcome. Thank you. 回答1: Since powershell.exe is a console program, you can't execute it normally without its console window appearing (although you can hide it shortly after it starts by using -WindowStyle Hidden , as you have noted).

How to stop Get-Content -wait after I find a particular line in the text file using powershell? Exit a pipeline on demand

£可爱£侵袭症+ 提交于 2020-03-11 14:52:34
问题 I am using this below script in Powershell (Version is 5.1): Get-Content -Path path\to\text\file\to\be\read.txt -Wait Now this continues to read even after the file is getting no update. How can I stop after I find particular line in the text file? Is there any other way to stop this on condition? 回答1: You can pipe the output and use foreach to check each line, if the line equals a certain string you can use break to stop the command: Get-Content path\to\text\file\to\be\read.txt -wait | % {$_

how to Filter the full Log context using PowerShell

陌路散爱 提交于 2020-02-01 09:46:28
问题 I am trying to filter out the logs and its full context using Select-String in powershell. I know the parameters of context and their meanings but this limits to what I am trying to achieve. Below is the log snippet and I want to search the log with date as a search parameter to select-string filter. [AD Thread-Metric Reporter1] 16 Dec 2019 19:03:32,371 ERROR ManagedMonitorDelegate - Error sending metrics - will requeue for later transmission com.singularity.ee.agent.commonservices

how to Filter the full Log context using PowerShell

馋奶兔 提交于 2020-02-01 09:45:08
问题 I am trying to filter out the logs and its full context using Select-String in powershell. I know the parameters of context and their meanings but this limits to what I am trying to achieve. Below is the log snippet and I want to search the log with date as a search parameter to select-string filter. [AD Thread-Metric Reporter1] 16 Dec 2019 19:03:32,371 ERROR ManagedMonitorDelegate - Error sending metrics - will requeue for later transmission com.singularity.ee.agent.commonservices

Start-Job - retrieve output in real time

断了今生、忘了曾经 提交于 2020-01-24 13:54:50
问题 We start a Powershell function via Start-Job and want to retrieve its output in the caller in real time. Is there a nice way to do so without calling Retrieve-Job in a loop? 回答1: Try something like this: $appJob = Start-Job { foreach($i in 1..10) { Write-Output "Testing $i"; Start-Sleep 10 } } $appJob.ChildJobs[0].Output 来源: https://stackoverflow.com/questions/48909983/start-job-retrieve-output-in-real-time

Start-Job - retrieve output in real time

喜你入骨 提交于 2020-01-24 13:52:07
问题 We start a Powershell function via Start-Job and want to retrieve its output in the caller in real time. Is there a nice way to do so without calling Retrieve-Job in a loop? 回答1: Try something like this: $appJob = Start-Job { foreach($i in 1..10) { Write-Output "Testing $i"; Start-Sleep 10 } } $appJob.ChildJobs[0].Output 来源: https://stackoverflow.com/questions/48909983/start-job-retrieve-output-in-real-time

Invoke-Command on remote session returns local values

倾然丶 夕夏残阳落幕 提交于 2020-01-23 17:58:13
问题 Question Should the script block of Invoke-Command , when run with a PSSession, always run on the remote computer? Context I ran the below powershell against a list of servers: Clear-Host $cred = get-credential 'myDomain\myUsername' $psSessions = New-PSSession -ComputerName @(1..10 | %{'myServer{0:00}' -f $_}) -Credential $cred Invoke-Command -Session $psSessions -ScriptBlock { Get-Item -Path 'HKLM:\System\CurrentControlSet\Control\Lsa\Kerberos\Parameters' } | Sort-Object PSComputerName #

What's the right way to emit errors in powershell module functions?

那年仲夏 提交于 2020-01-21 06:02:51
问题 I've read quite a bit on powershell error handling and now I'm quite confused about what I should be doing on any given situation (error handling). I'm working with powershell 5.1 (not core). With that said: Suppose I have a module with a function that would look like this mock: function Set-ComputerTestConfig { [CmdletBinding()] param( [Parameter(Position=0, Mandatory=$true)] [ValidateNotNullOrEmpty()] [string] $Name) begin { ... } process { # task 1 # task 2 => results in a failure that

Powershell5 Compact code by combining foreach, begin, process, and replace command

房东的猫 提交于 2020-01-02 19:10:36
问题 Can I get the same results with less code? The code searches sample.bat for the strings AROUND LINE {1-9999} and LINE2 {1-9999} and replaces {1-9999} with the {line number} the code is on. sample.bat: AROUND LINE 262 LINE2 1964 Old code: gc $env:temp\sample.bat | foreach -Begin {$lc = 1} -Process { $_ -replace "AROUND LINE \d*", "AROUND LINE $lc"; $lc += 1 } | Out-File -Encoding Ascii $env:temp\results.bat (gc $env:temp\results.bat) | foreach -Begin {$lc = 1} -Process { $_ -replace "LINE2 \d*