powershell

Powershell - add catch to pick up if there are no Excel processes

我怕爱的太早我们不能终老 提交于 2021-01-27 13:20:41
问题 Is there way to add an if/else catch to only $excelId = get-process excel | %{$_.Id} | ?{$before -notcontains $_} if there is no excel process running? e.g. if Excel is running then get-process id, if not then ignore it. Get-Process : Cannot find a process with the name "excel". Verify the process name and call the cmdlet again. At run.ps1:3 char:24 + $before = @(get-process <<<< excel | %{$_.Id} ) + CategoryInfo : ObjectNotFound: (excel:String) [Get-Process], ProcessCommandException +

Close Specific Website with Stop-Process in PowerShell

瘦欲@ 提交于 2021-01-27 12:51:37
问题 I was wondering if anyone is aware of a way to close a specific website that the user has open in Google Chrome? As of right now, I have Google Chrome opening up to a website that the user types in. The site is assigned to the $question variable that you will see below: $question2 = Read-Host "Would you like to copy $question back to the server?" if( ($question2 -eq 'yes') -or ($question2 -eq 'YES') -or ($question2 -eq 'Yes') -or ($question2 -eq 'Ye') -or ($question2 -eq 'Y') -or ($question2

Commands like “uname -s” is not recognized in WSL when executed from poweshell

被刻印的时光 ゝ 提交于 2021-01-27 12:41:05
问题 I need to execute the following command in WSL: sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose In order to execute it from powershell, i tried to run: Ubuntu1804 run "sudo curl -L 'https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)' -o /usr/local/bin/docker-compose" But errors occur as it cannot find the value of uname -s and uname -m uname : The

Error handling within Foreach-object Parallel block - Powershell 7

泄露秘密 提交于 2021-01-27 12:34:58
问题 What would be the best way to capture error within below Foreach-Object Parallel block as there would be three separate threads/runspaces running and executing code written in the block and multiple errors/exception can occurs at the same time? Would it be possible to capture all the errors in a list/variable and show at the end of the execution of the script? 1..3 | ForEach-Object -ThrottleLimit 3 -Parallel { #Some code here that throws error } 回答1: The -ErrorVariable param will give you an

Error with PowerShell command for copying file to remote server with credential

吃可爱长大的小学妹 提交于 2021-01-27 11:25:03
问题 PS C:\Windows\system32> Copy-Item -ToSession $s C:\Programs\temp\test.txt -Destination C:\Programs\temp\test.txt Copy-Item : A parameter cannot be found that matches parameter name 'ToSession'. At line:1 char:11 Copy-Item -ToSession $s C:\Programs\temp\test.txt -Destination C:\Programs\temp\ ... ~~~~~~~~~~ CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand 回答1: As it is noted

Error with PowerShell command for copying file to remote server with credential

此生再无相见时 提交于 2021-01-27 11:24:22
问题 PS C:\Windows\system32> Copy-Item -ToSession $s C:\Programs\temp\test.txt -Destination C:\Programs\temp\test.txt Copy-Item : A parameter cannot be found that matches parameter name 'ToSession'. At line:1 char:11 Copy-Item -ToSession $s C:\Programs\temp\test.txt -Destination C:\Programs\temp\ ... ~~~~~~~~~~ CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand 回答1: As it is noted

How to alias a Powershell function?

孤者浪人 提交于 2021-01-27 07:33:02
问题 The aim is to call the function hello by calling hello or alias helloworld Code: function hello() { param( [string] $name ) Write-Host "Hello $name!" } hello "Utrecht" helloworld "Utreg" Expected outcome: Hello Utrecht! Hello Utreg! 回答1: Use the set-alias cmdlet. set-alias -name helloworld -value hello It should be noted though that your function name does not follow the PowerShell convention and may be confusing to someone more accustomed to using PowerShell. 回答2: You can also add Alias

Powershell Speed: How to speed up ForEach-Object MD5/hash check

女生的网名这么多〃 提交于 2021-01-27 07:23:48
问题 I'm running the following MD5 check on 500 million files to check for duplicates. The scripts taking forever to run and I was wondering how to speed it up. How could I speed it up? Could I use a try catch loop instead of contains to throw an error when the hash already exists instead? What would you all recommend? $folder = Read-Host -Prompt 'Enter a folder path' $hash = @{} $lineCheck = 0 Get-ChildItem $folder -Recurse | where {! $_.PSIsContainer} | ForEach-Object { $lineCheck++ Write-Host

Powershell Speed: How to speed up ForEach-Object MD5/hash check

感情迁移 提交于 2021-01-27 07:22:26
问题 I'm running the following MD5 check on 500 million files to check for duplicates. The scripts taking forever to run and I was wondering how to speed it up. How could I speed it up? Could I use a try catch loop instead of contains to throw an error when the hash already exists instead? What would you all recommend? $folder = Read-Host -Prompt 'Enter a folder path' $hash = @{} $lineCheck = 0 Get-ChildItem $folder -Recurse | where {! $_.PSIsContainer} | ForEach-Object { $lineCheck++ Write-Host

PowerShell cmdlet Install-WindowsFeature called from C# errors and is not listed in the collections of commands

北城余情 提交于 2021-01-27 07:16:57
问题 I'm trying to install new features on a Windows Server 2012 system through PowerShell and C#. I am getting this error back when trying to call the Install-WindowsFeature cmdlet: The term 'Install-WindowsFeature' is not recognized as the name of a cmdlet, function, script file, or operable program... I can call other cmdlets though C# and even some of the other new ones. I did a Get-Command and Install-WindowsFeature , Remove-WindowsFeature , and Get-WindowsFeatures are not listed...but about