powershell

Extract string from text file via Powershell

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-04 18:51:28
问题 I have been trying to extract certain values from multiple lines inside a .txt file with PowerShell. Host Class INCLUDE vmware:/?filter=Displayname Equal "server01" OR Displayname Equal "server02" OR Displayname Equal "server03 test" This is what I want : server01 server02 server03 test I have code so far : $Regex = [Regex]::new("(?<=Equal)(.*)(?=OR") $Match = $Regex.Match($String) 回答1: You may use [regex]::matches($String, '(?<=Equal\s*")[^"]+') See the regex demo. See more ways to extract

PowerShell Copy-Item Method Fails - Brackets in Filename

女生的网名这么多〃 提交于 2021-02-04 18:26:27
问题 I am trying to use PowerShell (v.1) to copy over only files that match a pattern. The file naming convention is: Daily_Reviews[0001-0871].journal Daily_Reviews[1002-9887].journal [...] When I run it, the method "Copy-Item" complains: Dynamic parameters for cmdlet cannot be retrieved. The specified wildcard pattern is not valid: Daily_Reviews[0001-0871].journal + Copy-Item <<<< $sourcefile $destination The error is due to the "[" and "]" in the file names. When I remove the left and right

Powershell wget protocol violation

无人久伴 提交于 2021-02-04 17:38:21
问题 I am trying to browse a command on an embedded webserver using wget / invoke-webrequest. How can this error be avoided? wget : The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF Already tried multiple things, for example below without success: [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} When using BITS instead, this is the error I'm getting start-bitstransfer : The server did not return the file size. The URL

Powershell Service Account Password Change Logon Failure

寵の児 提交于 2021-02-04 17:38:05
问题 i am trying to use a powershell script that allows me to change the user account and password a specific service runs under. $account="domain\account" $password="password" $svc=gwmi win32_service -filter "name='MyService'" $svc.change($null,$null,$null,$null,$null,$false,$account,$password,$null,$null,$null) I could check that the account was changed but when i tried to run the service from the Service.msc GUI it fails with a logon failure. If I use to start the service from the script itself

Getting home directory?

依然范特西╮ 提交于 2021-02-04 16:59:06
问题 I'm trying to get a directory off of the user's home directory in a script. This is what I'm trying, but the ~ is interperated as a literal instead of expanding to the home directory. Is there anyway to get it to expand? If not, can I get the home directory another way? $mySourceDir = "~/Projects/svn/myProject/trunk" # Single quote also does not expand cd $mySourceDir This is using the PS 6 beta on OSX. 回答1: In PowerShell, the most robust way to refer to the current user's home directory is

Powershell - Excel Change format of the column to Text

落爺英雄遲暮 提交于 2021-02-04 16:31:58
问题 I have to convert 2 columns in 20 excel files (same format) everyday. Is there a way in powershell to make it work. I searched for some examples but all point to change of Number format. Current code: $worksheet.columns.item('d').NumberFormat = "$#,##0.00" The entire Workbook / Worksheet is in "General" format Help is appreciated. 回答1: As per comments, you can use @ to specify text formatting: $worksheet.columns.item('d').NumberFormat = "@" 来源: https://stackoverflow.com/questions/45632765

How to run this Beyond Compare script from Powershell?

天涯浪子 提交于 2021-02-04 16:19:06
问题 currently I can use Beyond Compare script to output results to a file. You all were able to help me in figuring out how to check for differences in the result text file via Powershell. However, I do have another question. To get the results into a text file, Beyond Compare has you use a basic script and then run the script via cmd. My question is how do I run the script via Powershell instead of cmd? Below is the command used in cmd to run the script. c:\Program Files\Beyond Compare 4

Modifying remote variables inside ScriptBlock using Invoke-Command

假装没事ソ 提交于 2021-02-04 16:06:24
问题 Is it possible to modify remote variables? I am trying to do something like the following: $var1 = "" $var2 = "" Invoke-Command -ComputerName Server1 -ScriptBlock{ $using:var1 = "Hello World" $using:var2 = "Goodbye World" } When I try this I get the error: The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property. So obviously, it doesn't work using this method, but are there any other

PowerShell Start-Process not setting $lastexitcode

放肆的年华 提交于 2021-02-04 15:53:25
问题 I have a set of test DLL's that I'm running from a powershell script that calls OpenCover.Console.exe via the Start-Process command. I have the -returntargetcode flag set After execution I check $lastexitcode and $? . They return 1 and True respectively all the time. Even when tests are failing. Shouldn't $lastexitcode be 0 when all tests pass and 1 when they fail? 回答1: By default, Start-Process is asynchronous, so it doesn't wait for your process to exit. If you want your command-line tool

How to set Powershell background color programmatically to RGB Value

╄→гoц情女王★ 提交于 2021-02-04 13:25:28
问题 The current selection of 16 colors from Console Colors is not the right choice for me. I would like to use much darker variants of these for the background. I could definitely set these using the UI and changing the RGB value there. For example, I could choose Darkblue and select 65 for Blue in the RGB section(128 is the default). Could some one tell me how to do this programmatically. Something like: (Get-Host).UI.RawUI.BackgroundColor=DarkBlue But with additional options. 回答1: This old post