powershell-3.0

Getting all open PS Sessions on a remote server (from new console window)

谁都会走 提交于 2020-02-26 11:54:27
问题 I can start 5 new PS sessions on a remote server and see them all by running Get-PSSession PS C:\> New-PSSession -ComputerName MyServerName Id Name ComputerName State ConfigurationName Availability -- ---- ------------ ----- ----------------- ------------ 1 Session1 MyServerName Opened Microsoft.PowerShell Available [repeat 4 more times] As expected, when I try to open a 6th session, I get the error saying that's a no-no (due to PoswerShells default limit of 5 concurrent remote PSSessions).

PowerShell unable to use enum MultipleInstances

心不动则不痛 提交于 2020-01-30 11:38:06
问题 I am looking to use Powershell to set "if the task is already running, then the following rules applies: to "Do not start a new instance" my powershell script looks like below: $Task = Get-ScheduledTask -TaskName "MyJob" $Task.Settings.MultipleInstances= "StopExisting" Set-ScheduledTask -User "USER" -Password "PASS" $Task I get error as : Exception setting "MultipleInstances": "Cannot convert value "StopExisting" to type "Microsoft.PowerShell.Cmdletization.GeneratedTypes.ScheduledTask

How to ignore specific error in PowerShell when executing a command?

╄→尐↘猪︶ㄣ 提交于 2020-01-23 11:07:25
问题 I am aware ErrorAction argument, also $ErrorActionPreference , $Error and $ . Context The issue I would like to solve is when running an external command (eg choco or git) it gives error which should be warning or not even warning, at least in my task context. Because of their exit code PowerShell considers that result as error in any sense, for example writes out red to output, etc, which is not desirable in my task's context. I can suppress those commands error output with -ErrorAction or 2

How to ignore specific error in PowerShell when executing a command?

主宰稳场 提交于 2020-01-23 11:07:08
问题 I am aware ErrorAction argument, also $ErrorActionPreference , $Error and $ . Context The issue I would like to solve is when running an external command (eg choco or git) it gives error which should be warning or not even warning, at least in my task context. Because of their exit code PowerShell considers that result as error in any sense, for example writes out red to output, etc, which is not desirable in my task's context. I can suppress those commands error output with -ErrorAction or 2

Parsing a text file in PowerShell

一笑奈何 提交于 2020-01-22 03:43:04
问题 I have a text file with multiple users (in the exact format): username:t-Hancock phoneNumber: 555555555 username:a-smith PhoneNumber: 987654321 username:r-Byer phonenumber: 123456789 I am trying to get all these users into an object with two properties (name and phone number). I tried Get-Content, tried some regex from other posts, but I couldn't modify them because I didn't understand it. How could this be done? 回答1: It's not that clear entirely what you want here - what kind of object and

How to remove words from all text files in a folder?

不想你离开。 提交于 2020-01-15 04:59:10
问题 I have a code like this: $txt = get-content c:\work\test\01.i $txt[0] = $txt[0] -replace '-' $txt[$txt.length - 1 ] = $txt[$txt.length - 1 ] -replace '-' $txt | set-content c:\work\test\01.i It just removes a - from first line and last line in a text file, but I need to do this for all text files in the directory tree which contains over 5k text files. how should I modify this code? All name of text files are random. I need to do this in powershell. Its a directory tree, under c:\work\test

PowerShell to delete Desktop Items from a remote PC

若如初见. 提交于 2020-01-13 20:39:13
问题 I have 200 PC that need to have some specific icons removed. I created a CSV file with the ComputerName (1 name per row) I have another file with the file name of the icon that needs to be removed from the desktops (Shortcut1.lnk, etc). This other file is also a CSV (1 file name per row). How can I run a PowerShell script to remove those icons. (Please note that not all computers in my CSV file maybe turned on. Some maybe off or have network issues). $SOURCE = "C:\powershell\shortcuts"

Powershell - “Expressions are only allowed as the first element of a pipeline”

冷暖自知 提交于 2020-01-13 11:13:22
问题 Could someone please tell me how to avoid this error in the below circumstance? $codegenDir = "Z:\Desktop\Song-Renamer" $PowerShellRepresentation = dir -path $MyMusicFolder -recurse -include *.mp3,*.m4a,*.wma,*.flac,*.ape | select -ExpandProperty FullName | $codegenDir\codegen.exe -s 10 20 | Out-String | ConvertFrom-Json What completely puzzles me is if simply omit $codegenDir (see below), the code operates correctly. I "think" I understand the concept of placing the expression first (ahead

Powershell - “Expressions are only allowed as the first element of a pipeline”

两盒软妹~` 提交于 2020-01-13 11:12:04
问题 Could someone please tell me how to avoid this error in the below circumstance? $codegenDir = "Z:\Desktop\Song-Renamer" $PowerShellRepresentation = dir -path $MyMusicFolder -recurse -include *.mp3,*.m4a,*.wma,*.flac,*.ape | select -ExpandProperty FullName | $codegenDir\codegen.exe -s 10 20 | Out-String | ConvertFrom-Json What completely puzzles me is if simply omit $codegenDir (see below), the code operates correctly. I "think" I understand the concept of placing the expression first (ahead

is get-childItem's new -file parameter fast like -filter or slow like -include?

一曲冷凌霜 提交于 2020-01-11 13:22:08
问题 EDIT Hoping here to clarify my convoluted and misleading question... based on my mistaken assumption that -file accepts inputs. Thanks for setting me straight and pointing out that it's just a switch parameter; the inputs in my example actually get passed to -path. Sounds like that may be the fastest purely powershell way to search for multiple file types, since -filter accepts only a single input and -include is slower. The get-childItem documentation says "Filters are more efficient than