powershell-4.0

Replace a character with new line

不打扰是莪最后的温柔 提交于 2020-01-03 15:19:34
问题 Powershell ver 4. Windows 7 I wanted to replace , with new lines in a text file. I tried the script below (Get-Content C:\Test\test.txt).Replace(',','`n') | Set-Content C:\Test\testv2.txt but when I see the output file I see , replaced with '`n' instead of new line. I also tried double quotes instead of single. Replace(',',"`n") 回答1: Try this: [IO.File]::ReadAllText(C:\Test\test.txt) -replace ',',"`r`n" | Out-File C:\Test\testv2.txt P.S. Sorry that don't have time to explain it, now. 回答2:

Split a string with powershell to get the first and last element

安稳与你 提交于 2020-01-03 08:57:10
问题 If you do: git describe --long you get: 0.3.1-15-g3b885c5 Thats the meaning of the above string: Tag-CommitDistance-CommitId (http://git-scm.com/docs/git-describe) How would you split the string to get the first (Tag) and last (CommitId) element? 回答1: By using String.split() with the count parameter to manage dashes in the commitid: $x = "0.3.1-15-g3b885c5" $tag = $x.split("-",3)[0] $commitid = $x.split("-",3)[-1] 回答2: Note : This answer focuses on improving on the split-into-tokens-by- -

Powershell 4 Get-ScheduledTask and Windows

北城余情 提交于 2020-01-03 08:43:12
问题 I thought no matter what OS you had, if you had Powershell installed, you would have access to the same default cmdlets. So I want to use Get-ScheduledTask on my Windows 7 machine. I have Powershell 4 installed. However, when I run it, I get the error: Get-ScheduledTask : The term 'Get-ScheduledTask' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At

Stripping value in PS and comparing if it is an integer value

倾然丶 夕夏残阳落幕 提交于 2020-01-03 05:50:12
问题 I am running PS cmdlet get-customcmdlet which is generating following output Name FreeSpaceGB ---- ----------- ABC-vol001 1,474.201 I have another variable $var=vol Now, I want to strip out just 001 and want to check if it is an integer. I am using but getting null value $vdetails = get-customcmdlet | split($var)[1] $vnum = $vdetails -replace '.*?(\d+)$','$1' My result should be integer 001 回答1: Assumption: get-customcmdlet is returning a pscustomobject object with a property Name that is of

Send mail via gmail with PowerShell V4

我们两清 提交于 2020-01-02 19:26:33
问题 I have been using this PowerShell script to send my an email to me when opened. $EmailFrom = "notifications@somedomain.com" $EmailTo = "anything@gmail.com" $Subject = "sample subject" $Body = "sample body" $SMTPServer = "smtp.gmail.com" $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("YOURUSERNAME", "YOURPASSWORD"); $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body) I changed

Add Element to XML

天大地大妈咪最大 提交于 2019-12-31 05:37:17
问题 I want to add a element (connector) to a existing XML, this was succesfull but I need to remove the xmlns= and want to add an value to it. The connector blaat is added with my code. The XML: <?xml version="1.0"?> <configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd"> <core xmlns="urn:activemq:core" xmlns:xsi="http://www.w3.org/2001/XMLSchema

Group-Object diffencies with or without code block

独自空忆成欢 提交于 2019-12-31 00:24:41
问题 The code below produce 2 "identical" Hashtables, however on the one that was grouped using a code block I can't get items from the key. $HashTableWithoutBlock = Get-WmiObject Win32_Service | Group-Object State -AsHashTable $HashTableWithBlock = Get-WmiObject Win32_Service | Group-Object {$_.State} -AsHashTable Write-Host "Search result for HashTable without using code block : " -NoNewline if($HashTableWithoutBlock["Stopped"] -eq $null) { Write-Host "Failed" } else { Write-Host "Success" }

How to exclude non-valued object properties when converting to JSON in Powershell

╄→гoц情女王★ 提交于 2019-12-30 06:11:10
问题 I have a piece of code that works but I want to know if there is a better way to do it. I could not find anything related so far. Here are the facts: I have an object with n properties. I want to convert this object to JSON using (ConvertTo-Json). I don't want to include in the JSON those object properties that are not valued. Building the object (not really important): $object = New-Object PSObject Add-Member -InputObject $object -MemberType NoteProperty -Name TableName -Value "MyTable" Add

Find numbers after specific text in a string with RegEx

不羁岁月 提交于 2019-12-30 06:04:47
问题 I have a multiline string like the following: 2012-15-08 07:04 Bla bla bla blup 2012-15-08 07:05 *** Error importing row no. 5: The import of this line failed because bla bla 2012-15-08 07:05 Another text that I don't want to search... 2012-15-08 07:06 Another text that I don't want to search... 2012-15-08 07:06 *** Error importing row no. 5: The import of this line failed because bla bla 2012-15-08 07:07 Import has finished bla bla What I want is to extract all row numbers that have errors

Append values to Powershell object

本小妞迷上赌 提交于 2019-12-25 07:58:43
问题 I have a PS object and couldnt figure out a way to append values to my object. $object = New-Object PSObject Add-Member -InputObject $object -MemberType NoteProperty -Name Col1 -Value "" Add-Member -InputObject $object -MemberType NoteProperty -Name Col2 -Value "" Add-Member -InputObject $object -MemberType NoteProperty -Name Type -Value "" 1..10 |ForEach{ $src=$_ 11..20 | ForEach{ $dst = $_ $object.Col1=$src $object.Col2=$dst $object.Type="New" } } I want my result like col1 col2 Type ---- -