I have one raw input from end user in to a variable as below
javascript:DS('https://HS/v1/dev/A/Machinename/SNAPNAME','XXXXX')
I want to extract url from the above input.
I want to split the url using '/' in to seperate variables.
Input :
$DSURL = "javascript:DS('https://HS/v1/dev/A/Machinename/SNAPNAME','XXXXX')"
[regex]$regex = '(?<=https:\/\/\HS\\/v1\/dev\/A\?t=)(.*)(?=' )'
$regex.Matches($DSURL).Value
$1 = https://HS/v1/dev/A/Machinename/SNAPNAME
$2 = Machinename
$3 = SNAPNAME
But it is not working,
I think the regex used is wrong,
Please help
this uses the [uri] type accelerator to convert the uri string into a uri object. then it uses the .Segments property to grab the last two segments and put them into $Vars.
you may want to look at the various properties stored in the $UriObject variable for some other interesting info. [grin]
$DSUrl = "javascript:DS('https://HS/v1/dev/A/SpiffyDoodleServer/SomeSortOfThing','XXXXX')"
$CleanedDSU = $DSUrl.Split('(')[1].Split(',')[0].Trim("'")
$UriObject = [uri]$CleanedDSU
$MachineName = $UriObject.Segments[-2].Trim('/')
$SnapName = $UriObject.Segments[-1]
$DSUrl
$CleanedDSU
$MachineName
$SnapName
output ...
javascript:DS('https://HS/v1/dev/A/SpiffyDoodleServer/SomeSortOfThing','XXXXX')
https://HS/v1/dev/A/SpiffyDoodleServer/SomeSortOfThing
SpiffyDoodleServer
SomeSortOfThing
I have managed to pull this and it is picking the output URL properly.
$text = "javascript:DS('https://HS/v1/dev/A/Machinename/SNAPNAME','XXXXX')"
($text | Select-String -Pattern #"
(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
"#).Matches.Value
OUTPUT:
https://HS/v1/dev/A/Machinename/SNAPNAME','XXXXX
Hope it helps.
Related
I have tried three different approaches, to no avail.
First is:
$s = $presentation.Slides[$targetslide]
$tb = $s.Shapes.AddTextbox($horiz,$left,$top,$width,$height)
$tb.TextFrame.TextRange.Text = $fla
the second is:
$f0a = ''
#$f1a is text to insert
$presentation.Slides[$targetslide].Shapes[1].TextFrame.TextRange.Replace($f0a, $f1a)
and the third is:
$s = $presentation.Slides[$targetslide]
$s.Shape[1].TextFrame.TextRange.Text = $fla
Neither seem to work. Do you have any suggestions as to what I should try?
Apparently,
$f0a=$s.Shapes[5].TextFrame.TextRange.Text
$test = $s.Shapes[5].TextFrame.TextRange.Replace($f0a, $f1a)
works perfectly.
I have some Powershell that works with mail from Outlook folders. There is a footer on most emails starting with text "------". I want to dump all text after this string.
I have added an expression to Select-Object as follows:
$cleanser = {($_.Body).Substring(0, ($_.Body).IndexOf("------"))}
$someObj | Select-Object -Property #{ Name = 'Body'; Expression = $cleanser}
This works when the IndexOf() returns a match... but when there is no match my Select-Object outputs null.
How can I update my expression to return the original string when IndexOf returns null?
PetSerAl, as countless times before, has provided the crucial pointer in a comment on the question:
Use PowerShell's -replace operator, which implements regex-based string replacement that returns the input string as-is if the regex doesn't match:
# The script block to use in a calculated property with Select-Object later.
$cleanser = { $_.Body -replace '(?s)------.*' }
If you want to ensure that ------ only matches at the start of a line, use (?sm)^------.*; if you also want to remove the preceding newline, use (?s)\r?\n------.*
(?s) is an inline regex option that makes . match newlines too, so that .* effectively matches all remaining text, across lines.
By not specifying a replacement operand, '' (the empty string) is implied, which effectively removes the matching part from the input string (technically, a copy of the original string with the matching part removed is returned).
If regex '(?s)------.*' does not match, $_.Body is returned as-is (technically, it is the input string itself that is returned, not a copy).
The net effect is that anything starting with ------ is removed, if present.
I agree with #mklement0 and #PetSerAl Regular Expressions give the best answer. Yay! Regular Expressions to the rescue!
Edit:
Fixing my original post.
Going with #Adam's ideas of using a script block in the expression, you simply need to add more logic to the script block to check the index first before using it:
$cleanser = {
$index = ($_.Body).IndexOf("------");
if($index -eq -1){
$index = $_.Body.Length
};
($_.Body).Substring(0, $index)
}
$someObj | Select-Object -Property #{ Name = 'Body'; Expression = $cleanser}
$fileName = "Name of TheFolder_NE_ED"
$lengthFileName = $fileName.length
$shortenLengthFileName = $lengthFileName - 5
Write-Host("Name of TheFolder_NE_ED").Substring($shortenLengthFileName,$lengthFileName)
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
I am having a problem with SubString() function, it errors with:
I tried printing out values of my variables but they seemed fine.
In PowerShell, substring works in a slightly different way.
With your existing code you could try this:
$fileName = "Name of TheFolder_NE_ED"
$lengthFileName = $fileName.length
$shortenLengthFileName = $lengthFileName - 5
Write-Host("Name of TheFolder_NE_ED").Substring($shortenLengthFileName)
Explanation:
The first parameter inside Substring should be the starting index of the character(in this case 18). Now from that letter it will start counting till the character (which you have to pass as a second parameter). Else it will by default go to the end of the string.
So, if you want to pass 2 parameters and do that, then change it to:
Write-Host("Name of TheFolder_NE_ED").Substring($shortenLengthFileName,5)
For further reference, follow Substring Use
Hope it helps.
I want to grab lots of text content from a .sql file between a --Start and --End comment.
Whatever I do somehow I don`t get the substring method correctly to grab only the text within the --Start and --End comment:
text.sql
This text I want not
--Start
this text I want here
--End
This text I want not
This is what I tried:
$insertStartComment = "--Start"
$insertEndComment = "--End"
$content = [IO.File]::ReadAllText("C:\temp\test.sql")
$insertStartPosition = $content.IndexOf($insertStartComment) + $insertStartComment.Length
$insertEndPosition = $content.IndexOf($insertEndComment)
$content1 = $content.Substring($insertStartPosition, $content1.Length - $insertEndPosition)
$content = $content1.Substring(0,$content1.Length - $insertEndPosition)
It would be nice if someone could help me out find my error :-)
There's an attempt to use uninitialized variable in the code:
$content1 = $content.Substring($insertStartPosition, $content1.Length - $insertEndPosition)
The variable $content1 isn't initialized yet, thus the substring call goes haywire. When you run the code again, the variable is set - and results are even more weird.
Use Powershell's Set-StrictMode to enable warnings about uninitialized variables.
It's not the substring approach you are looking for, but I figured that I would toss out a RegEx solution. This will find the text between the --Start and --End on a text file. In this case, I am grouping the matched text with a named capture called LineYouWant and display the matches that it finds. This also works if you have multiple instances of --Start--End blocks in a single file.
$Text = [IO.File]::ReadAllText("C:\users\proxb\desktop\SQL.txt")
[regex]::Matches($Text,'.*--Start\s+(?<LineYouWant>.*)\s+--End.*') | ForEach {
$_.Groups['LineYouWant'].Value
}
Trying to update a line using the code below.
$dburl = 70.186.192.52
$ptdb = 3388
$Se = "C:\File\location\edit.me"
(Get-Content $Se) |
ForEach-Object { $_ -replace ("http://127.0.0.1:8190/storage/server"), 'http://$dburl:$ptdb/storage/server' } | Set-Content $Se
The output is:
http://$dburl:$ptdb/storage/server or http://\70.186.192.52\:3388/storage/server
I have tried escaping the // and : but no luck in figuring this one out. Anyone have a better way to do this. I have looked through the site and none of the things that I have found address this direct situation. I say this so I don't get any negitive marks for not researching the code.
powershell replace special characters
This one gives me what I am getting. Instead of the variable being substituted they are just printed out exactly as they are typed in. I have tried double quotes and it prints the port but not the address. if I put any escape / or \ it just prints it out in front.
Thanks!
I'm surprised that worked at all. Try using double quotes around the replacement string e.g.:
$dburl = '70.186.192.52'
$ptdb = '3388'
... -replace ("http://127.0.0.1:8190/storage/server"), "http://${dburl}:$ptdb/storage/server"
Also, quote your two variable values.