PowerShell: remove or replace quote marks from variable - powershell

I'm using Get-EventLog to set a variable, and then setting another variable with the event ID description. I then use blat.exe to email this information to a group.
The description contains quotation marks. The quotation marks are causing blat to exit with error.
Is there a way to remove the quotes from the event.Message and replace them with a space or something?

If the variable is a String object then you can do the following:
$Variable.Replace("`"","")

I actually just got it. The number of quotes and double quotes was confusing me, but this has worked and blat did not error.
$var -replace '"', ""
Those quotes are: single, double, single, comma, double, double.

Depending on a case, it might be simpler to use Trim(Char[]) method:
...Removes all leading and trailing occurrences...
e.g. $your_variable.Trim('"')
It will remove quotes only from start and end of $your_variable. It will keep any quotes, escaped or not, which are inside the text of $your_variable as they were:
PS C:\> $v.Trim('"') # where $v is: "hu""hu"hu'hu"
hu""hu"hu'hu
You can use Trim('"'), Trim("'"), but also both: Trim("`"'")
Note that Trim() does not care if a quote is orphaned, meaning that it will remove ending or starting quote regardless of it having or not a paired quote on the other side of the string.
PS C:\Users\Papo> $hu = "A: He asked `"whos this sofa?`" B: She replied: `"Chris'`""
PS C:\Users\Papo> $hu
A: He asked "whos this sofa?" B: She replied: "Chris'"
PS C:\Users\Papo> $hu.trim('"')
A: He asked "whos this sofa?" B: She replied: "Chris'
PS C:\Users\Papo> # and even worse:
PS C:\Users\Papo> $hu.trim("'`"")
A: He asked "whos this sofa?" B: She replied: "Chris

If you use Powershell's built-in send-mailmessage (2.0 required), you can eliminate your dependency on blat.exe and properly handle this issue without editing the description from the event log.

The problem is that a simple replace cleans out every quote character, even if escaped (doubled).
Here are the functions I created for my use :
one which removes only orphan quotes.
one which escapes them
I also made them generic to manage other characters, with the optional $charToReplace parameter
#Replaces single occurrences of characters in a string.
#Default is to replace single quotes
Function RemoveNonEscapedChar {
param(
[Parameter(Mandatory = $true)][String] $param,
[Parameter(Mandatory = $false)][String] $charToReplace
)
if ($charToReplace -eq '') {
$charToReplace = "'"
}
$cleanedString = ""
$index = 0
$length = $param.length
for ($index = 0; $index -lt $length; $index++) {
$char = $param[$index]
if ($char -eq $charToReplace) {
if ($index +1 -lt $length -and $param[$index + 1] -eq $charToReplace) {
$cleanedString += "$charToReplace$charToReplace"
++$index ## /!\ Manual increment of our loop counter to skip next char /!\
}
continue
}
$cleanedString += $char
}
return $cleanedString
}
#A few test cases :
RemoveNonEscapedChar("'st''r'''i''ng'") #Echoes st''r''i''ng
RemoveNonEscapedChar("""st""""r""""""i""""ng""") -charToReplace '"' #Echoes st""r""i""ng
RemoveNonEscapedChar("'st''r'''i''ng'") -charToReplace 'r' #Echoes 'st'''''i''ng'
#Escapes single occurences of characters in a string. Double occurences are not escaped. e.g. ''' will become '''', NOT ''''''.
#Default is to replace single quotes
Function EscapeChar {
param(
[Parameter(Mandatory = $true)][String] $param,
[Parameter(Mandatory = $false)][String] $charToEscape
)
if ($charToEscape -eq '') {
$charToEscape = "'"
}
$cleanedString = ""
$index = 0
$length = $param.length
for ($index = 0; $index -lt $length; $index++) {
$char = $param[$index]
if ($char -eq $charToEscape) {
if ($index +1 -lt $length -and $param[$index + 1] -eq $charToEscape) {
++$index ## /!\ Manual increment of our loop counter to skip next char /!\
}
$cleanedString += "$charToEscape$charToEscape"
continue
}
$cleanedString += $char
}
return $cleanedString
}
#A few test cases :
EscapeChar("'st''r'''i''ng'") #Echoes ''st''r''''i''ng''
EscapeChar("""st""""r""""""i""""ng""") -charToEscape '"' #Echoes ""st""r""""i""ng""
EscapeChar("'st''r'''i''ng'") -charToEscape 'r' #Echoes 'st''rr'''i''ng'

None of the above answers worked for me. So I created the following solution...
Search and Replace the character single Quote "'" ascii Character (39) with a space " " ascii Character (32)
$strOldText = [char] 39
$strNewText = [char] 32
$Variable. = $Variable..Replace($strOldText, $strNewText).Trim()

Related

powershell (Get-WmiObject win32_physicalmedia).serialnumber output hex

When I used (Get-WmiObject win32_physicalmedia).serialnumber the output was in hex. Example: 31323334353637383930. Then then I used the code below
$pass=""
$t=(Get-WmiObject win32_physicalmedia).serialnumber
$t -split '(.{2})' |%{ if ($_ -ne "") { $pass+=[CHAR]([CONVERT]::toint16("$_",16)) }}
write host $pass
The output was: 1234567890. The problem is that 1234567890 is not the serial number -- the real serial number is 2143658709. I need a script to swap the number $input "1234567890" to $output "214365768709".
this presumes your SN string is an even number of characters, and that the real number simply reverses the character pairs.
$InString = '1234567890'
$OutString = ''
foreach ($Index in 0..($InString.Length / 2))
{
$CurPos = $Index * 2
$OutString += $InString[$CurPos + 1] + $InString[$CurPos]
}
$OutString
output = 2143658709
I think this is called "middle endian" format, where every two bytes are reversed: middle-endian
Coming from a post here: WMI Win32_PhysicalMedia SMART ID in Vista and 7 Permissions

How to send one character at a time to a function in powershell?

I have a function that takes one parameter of type char, of course that means only one character at a time.
I made a for-loop in order to send one character at a time to the function. But of course this wouldn't work, because $mystring is a string and can't be processed like a char. What would the solution be?
$mystring = "ABCDEFG";
function Characters {
$lettertoascii = [int][char]$args[0];
}
for($i=1; $i -le 10; $i++){
Characters $mystring
}
You could convert your string into a char array and process it with a foreach loop to call your function char by char:
$mystring = "ABCDEFG";
function characters{
$lettertoascii = [int][char]$args[0];
}
$mystring.ToCharArray() | foreach {
Characters $_
}
Or simply loop over the characters in the string like this:
$myString = 'ABCDEFG'
for ($i = 0; $i -lt $myString.Length; $i++) {
# call your function one System.Char at a time
Characters $myString[$i]
}
I suggest an advanced function:
function Get-Character
{
[CmdletBinding()]
[OutputType([System.Int32[]])]
param
(
[Parameter(Position = 0, ValueFromPipeline)]
[System.Char[]]
$Letter
)
process
{
foreach ($item in $Letter)
{
[int]$item
}
}
}
Then you can pass a char array to it through pipeline:
'abcdefg' | Get-Character
or otherwise:
Get-Character 'abcdefg'
Here's documentation on the ToCharArray method, and another link about advanced powershell functions.
Edit:
Turns out powershell is smart and will automatically typecast a string to a char array so you can just pass a string.

Is there a better way to convert all control characters to entities in PowerShell 5?

Context: Azure, Windows Server 2012, PowerShell 5
I've got the following code to convert all control characters (ascii and unicode whitespace other than \x20 itself) to their ampersand-hash equivalents.
function ConvertTo-AmpersandHash {
param ([Parameter(Mandatory)][String]$Value)
# there's got to be a better way of doing this.
$AMPERHASH = '&#'
$SEMICOLON = ';'
for ($i = 0x0; $i -lt 0x20; $i++) {
$value = $value -replace [char]$i,($AMPERHASH + $i + $SEMICOLON)
}
for ($i = 0x7f; $i -le 0xa0; $i++) {
$value = $value -replace [char]$i,($AMPERHASH + $i + $SEMICOLON)
}
return $Value
}
As can be seen by the embedded comment, I'm sure there's a better way to do this. As it stands, one does some 65 iterations for each incoming string. Would regular expressions work better/faster?
LATER
-replace '([\x00-\x1f\x7f-\xa0])',('&#' + [byte][char]$1 + ';')
looks promising but the $1 is evaluating to zero all the time, giving me  all the time.
LATER STILL
Thinking that -replace couldn't internally iterate, I came up with
$t = [char]0 + [char]1 + [char]2 + [char]3 + [char]4 + [char]5 + [char]6
$r = '([\x00-\x1f\x7f-\xa0])'
while ($t -match [regex]$r) {
$t = $t -replace [regex]$r, ('&#' + [byte][char]$1 + ';')
}
echo $t
However out of that I still get

FINALLY
function ConvertTo-AmpersandHash {
param ([Parameter(Mandatory)][String]$Value)
$AMPERHASH = '&#'
$SEMICOLON = ';'
$patt = '([\x00-\x1f\x7f-\xa0]{1})'
while ($Value -match [regex]$patt) {
$Value = $Value -replace $Matches[0], ($AMPERHASH + [byte][char]$Matches[0] + $SEMICOLON)
}
return $Value
}
That works better. Faster too. Any advances on that?
Kory Gill's answer with the library call is surely a better approach, but to address your regex question, you can't evaluate code in the replacement with the -replace operator.
To do that, you need to use the .Net regex replace method, and pass it a scriptblock to evaluate the replacement, which takes a parameter of the match. e.g.
PS C:\> [regex]::Replace([string][char]2,
'([\x00-\x20\x7f-\xa0])',
{param([string]$m) '&#' + [byte][char]$m + ';'})

Your question is a little unclear to me, and could be a duplicate of What is the best way to escape HTML-specific characters in a string (PowerShell)?.
It would be nice if you explicitly stated the exact string you have and what you want it to converted to. One has to read the code to try to guess.
I am guessing one or more of these functions will do what you want:
$a = "http://foo.org/bar?baz & also <value> conversion"
"a"
$a
$b = [uri]::EscapeDataString($a)
"b"
$b
$c = [uri]::UnescapeDataString($b)
"c"
$c
Add-Type -AssemblyName System.Web
$d = [System.Web.HttpUtility]::HtmlEncode($a)
"d"
$d
$e = [System.Web.HttpUtility]::HtmlDecode($d)
"e"
$e
Gives:
a
http://foo.org/bar?baz & also <value> conversion
b
http%3A%2F%2Ffoo.org%2Fbar%3Fbaz%20%26%20also%20%3Cvalue%3E%20conversion
c
http://foo.org/bar?baz & also <value> conversion
d
http://foo.org/bar?baz & also <value> conversion
e
http://foo.org/bar?baz & also <value> conversion
I have one small function which helps me replacing as per my requirement:
$SpecChars are all the characters that are going to be replaced with nothing
Function Convert-ToFriendlyName
{param ($Text)
# Unwanted characters (includes spaces and '-') converted to a regex:
$SpecChars = '\', ' ','\\'
$remspecchars = [string]::join('|', ($SpecChars | % {[regex]::escape($_)}))
# Convert the text given to correct naming format (Uppercase)
$name = (Get-Culture).textinfo.totitlecase(“$Text”.tolower())
# Remove unwanted characters
$name = $name -replace $remspecchars, ""
$name
}
Example: Convert-ToFriendlyName "My\Name\isRana\Dip " will result me "MyNameIsranaDip".
Hope it helps you.

PowerShell - Password Generator - How to always include number in string?

I have the following PowerShell script that creates a random string of 15 digits, for use as an Active Directory password.
The trouble is, this works great most of the time, but on some occasions it doesn't use a number or symbol. I just get 15 letters. This is then not usable as an Active Directory password, as it must have at least one number or symbol in it.
$punc = 46..46
$digits = 48..57
$letters = 65..90 + 97..122
$YouShallNotPass = get-random -count 15 `
-input ($punc + $digits + $letters) |
% -begin { $aa = $null } `
-process {$aa += [char]$_} `
-end {$aa}
Write-Host "Password is $YouShallNotPass"
How would I amend the script to always have at least one random number or symbol in it?
Thank you.
You could invoke the Get-Random cmdlet three times, each time with a different input parameter (punc, digit and letters), concat the result strings and shuffle them using another Get-Random invoke:
(Get-Random -Count 15 -InputObject ([char[]]$yourPassword)) -join ''
However, why do you want to reinvent the wheel? Consider using the following GeneratePassword function:
[Reflection.Assembly]::LoadWithPartialName("System.Web")
[System.Web.Security.Membership]::GeneratePassword(15,2)
And to ensure, it contains at least one random number (you already specify the number of symbols):
do {
$pwd = [System.Web.Security.Membership]::GeneratePassword(15,2)
} until ($pwd -match '\d')
As suggested by jisaak, there is no 100% guaranty that the Membership.GeneratePassword Method generates a password that meets the AD complexity requirements.
That's why I reinvented the wheel:
Function Create-String([Int]$Size = 8, [Char[]]$CharSets = "ULNS", [Char[]]$Exclude) {
$Chars = #(); $TokenSet = #()
If (!$TokenSets) {$Global:TokenSets = #{
U = [Char[]]'ABCDEFGHIJKLMNOPQRSTUVWXYZ' #Upper case
L = [Char[]]'abcdefghijklmnopqrstuvwxyz' #Lower case
N = [Char[]]'0123456789' #Numerals
S = [Char[]]'!"#$%&''()*+,-./:;<=>?#[\]^_`{|}~' #Symbols
}}
$CharSets | ForEach {
$Tokens = $TokenSets."$_" | ForEach {If ($Exclude -cNotContains $_) {$_}}
If ($Tokens) {
$TokensSet += $Tokens
If ($_ -cle [Char]"Z") {$Chars += $Tokens | Get-Random} #Character sets defined in upper case are mandatory
}
}
While ($Chars.Count -lt $Size) {$Chars += $TokensSet | Get-Random}
($Chars | Sort-Object {Get-Random}) -Join "" #Mix the (mandatory) characters and output string
}; Set-Alias Create-Password Create-String -Description "Generate a random string (password)"
Usage:
The Size parameter defines the length of the password.
The CharSets parameter defines the complexity where the character U,
L, N and S stands for Uppercase, Lowercase, Numerals and Symbols.
If supplied in lowercase (u, l, n or s) the returned string
might contain any of character in the concerned character set, If
supplied in uppercase (U, L, N or S) the returned string will
contain at least one of the characters in the concerned character
set.
The Exclude parameter lets you exclude specific characters that might e.g.
lead to confusion like an alphanumeric O and a numeric 0 (zero).
Examples:
To create a password with a length of 8 characters that might contain any uppercase characters, lowercase characters and numbers:
Create-Password 8 uln
To create a password with a length of 12 characters that that contains at least one uppercase character, one lowercase character, one number and one symbol and does not contain the characters OLIoli01:
Create-Password 12 ULNS "OLIoli01"
For the latest New-Password version: use:
Install-Script -Name PowerSnippets.New-Password
Command to Generate Random passwords by using existing funciton:
[system.web.security.membership]::GeneratePassword(x,y)
x = Length of the password
y = Complexity
General Error:
Unable to find type [system.web.security.membership]. Make sure that the assembly that contains this type is loaded.
Solution:
Run the below command:
Add-Type -AssemblyName System.web;
Another solution:
function New-Password() {
param(
[int] $Length = 10,
[bool] $Upper = $true,
[bool] $Lower = $true,
[bool] $Numeric = $true,
[string] $Special
)
$upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$lowerChars = "abcdefghijklmnopqrstuvwxyz"
$numericChars = "0123456789"
$all = ""
if ($Upper) { $all = "$all$upperChars" }
if ($Lower) { $all = "$all$lowerChars" }
if ($Numeric) { $all = "$all$numericChars" }
if ($Special -and ($special.Length -gt 0)) { $all = "$all$Special" }
$password = ""
for ($i = 0; $i -lt $Length; $i++) {
Write-Host "password: [$password]"
$password = $password + $all[$(Get-Random -Minimum 0 -Maximum $all.Length)]
}
$valid = $true
if ($Upper -and ($password.IndexOfAny($upperChars.ToCharArray()) -eq -1)) { $valid = $false }
if ($Lower -and ($password.IndexOfAny($lowerChars.ToCharArray()) -eq -1)) { $valid = $false }
if ($Numeric -and ($password.IndexOfAny($numericChars.ToCharArray()) -eq -1)) { $valid = $false }
if ($Special -and $Special.Length -gt 1 -and ($password.IndexOfAny($Special.ToCharArray()) -eq -1)) { $valid = $false }
if (-not $valid) {
$password = New-Password `
-Length $Length `
-Upper $Upper `
-Lower $Lower `
-Numeric $Numeric `
-Special $Special
}
return $password
}
Flexible enough to set length, turn on/of upper, lower, and numeric, and set the list of specials.
My take on generating passwords in PowerShell, based on what I've found here and in the Internets:
#Requires -Version 4.0
[CmdletBinding(PositionalBinding=$false)]
param (
[Parameter(
Mandatory = $false,
HelpMessage = "Minimum password length"
)]
[ValidateRange(1,[int]::MaxValue)]
[int]$MinimumLength = 24,
[Parameter(
Mandatory = $false,
HelpMessage = "Maximum password length"
)]
[ValidateRange(1,[int]::MaxValue)]
[int]$MaximumLength = 42,
[Parameter(
Mandatory = $false,
HelpMessage = "Characters which can be used in the password"
)]
[ValidateNotNullOrEmpty()]
[string]$Characters = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM##%*-_+:,.'
)
(1..(Get-Random -Minimum $MinimumLength -Maximum $MaximumLength) `
| %{ `
$Characters.GetEnumerator() | Get-Random `
}) -join ''
I preferred this over using System.Web, not to introduce dependencies, which could change with .Net / .Net Core versions.
My variation also allows random password length (in specified range), is fairly concise (apart from the parameters section, which is quite verbose, to enforce some validations and provide defaults) and allows character repetitions (as opposite to the code in the question, which never repeats the same character).
I understand, that this does not guarantee a digit in the password. This however can be addressed in different ways. E.g. as was suggested, to repeat the generation until the password matches the requirements (contains a digit). My take would be:
Generate a random password.
If it does not contain a digit (or always):
Use a random function to get 1 random digit.
Add it to the random password.
Randomize the order of the result (so the digit is not necessarily always at the end).
Assuming, that the above script would be named "Get-RandomPassword.ps1", it could look like this:
$pass = .\Get-RandomPassword.ps1
$pass += (0..9 | Get-Random)
$pass = (($pass.GetEnumerator() | Get-Random -Count $pass.Length) -join '')
Write-Output $pass
This can be generalized, to enforce using any character category:
$sets = #('abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789', '()-_=+[{]};:''",<.>/?`~')
$pass = .\Get-RandomPassword.ps1 -Characters ($sets -join '')
foreach ($set in $sets) {
$pass += ($set.GetEnumerator() | Get-Random)
}
$pass = (($pass.GetEnumerator() | Get-Random -Count $pass.Length) -join '')
Write-Output $pass
I wrote a secure password generator function in PowerShell, maybe this will be useful to someone.
Similar to the accepted answer, this script also uses Get-Random (twice), and also regular expression matching to ensure the output is secure.
The difference in this script is that the password length can also be randomised.
(To hard set a password length, just set the MinimumPasswordLength and MaximumPasswordLength values to the the same length.)
It also allows an easy to edit character set, and also has a regex to ensure a decent password has been generated with all of the following characteristics:
(?=.*\d) must contain at least one numerical character
(?=.*[a-z]) must contain at least one lowercase character
(?=.*[A-Z]) must contain at least one uppercase character
(?=.*\W) must contain at least one non-word character
The answer to your question about always including a number in your generated output can be solved by checking the output with a regex match (just use the parts of the regex that you need, based on the explanations above), the example here checks for uppercase, lowercase, and numerical:
$Regex = "(?=.*\d)(?=.*[a-z])(?=.*[A-Z])"
do {
$Password = ([string]($AllowedPasswordCharacters |
Get-Random -Count $PasswordLength) -replace ' ')
} until ($Password -cmatch $Regex)
$Password
Here is the full script:
Function GeneratePassword
{
cls
$MinimumPasswordLength = 12
$MaximumPasswordLength = 16
$PasswordLength = Get-Random -InputObject ($MinimumPasswordLength..$MaximumPasswordLength)
$AllowedPasswordCharacters = [char[]]'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!?##£$%^&'
$Regex = "(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)"
do {
$Password = ([string]($AllowedPasswordCharacters |
Get-Random -Count $PasswordLength) -replace ' ')
} until ($Password -cmatch $Regex)
$Password
}
GeneratePassword
I had the same issue here is the snippet I used to create my alphanumerical password its simple all I have done is used ASCII regex replace to make it nice.
Function Password-Generator ([int]$Length)
{
# Generate passwords just call password-generator(lenght of password)
$Assembly = Add-Type -AssemblyName System.Web
$RandomComplexPassword = [System.Web.Security.Membership]::GeneratePassword($Length,2)
$AlphaNumericalPassword = $RandomComplexPassword -replace '[^\x30-\x39\x41-\x5A\x61-\x7A]+'
Write-Output $AlphaNumericalPassword
}
I've created this. You can choose how many Pwd to create
$howoften = Read-Host "How many would you like to create: "
$i = 0
do{
(-join(1..42 | ForEach {((65..90)+(97..122)+(".") | % {[char]$_})+(0..9)+(".") | Get-Random}))
$i++
} until ($i -match $howoften)
To change the length of the pwd simply edit the "42" in line 4
(-join(1..**42** | ForEach ...

How to concatenate array of integers into comma separated string

I have two question, acually:
How to join array of integers into comma separated string?
(1,2,3) => "1,2,3"
How to convert array of integers to array of string? (1,2,3) => ("1", "2", "3")
$arraylist = New-Object 'system.collections.arraylist'
$arraylist.Add(1);
$arraylist.Add(2);
$csv = ??
#($arraylist-join -',') returns error: Cannot convert value "," to type "System.Int32". Error: "Input string was not in a correct format."
In your question, you've commented out the following snippet:
($arraylist-join -',')
because it returns the error Cannot convert value "," to type "System.Int32"...
The reason for this is the dash - in front of ','.
In PowerShell, only operators and parameters are prefixed with a dash, and since ',' is neither (it's an argument to an operator), the PowerShell parser gets super confused and tries to treat -',' as a value expression that would result in a negative number.
Just void the dash and you'll be fine:
$arraylist -join ','
Finally, you can easily cast an array of integers to an array of strings with the unchecked cast operator -as (PowerShell 3.0 and newer):
$StringArray = 1,2,3,4,5 -as [string[]]
or with an explicit cast (PowerShell 2.0-compatible):
$StringArray = [string[]]#(1,2,3,4,5)
Next code snippet could help out on understanding:
$arraylist = New-Object 'system.collections.arraylist'
$arraylist.Add(111) | Out-Null
$arraylist.Add([string]222) | Out-Null
$arraylist.Add('"' + 3 + '"') | Out-Null
for($i=0; $i -lt $arraylist.Count; $i++ ){
write-host $i, $arraylist[$i], $arraylist[$i].GetType()
}
write-host ''
$csv = $arraylist -join ','
$csv
Output:
0 111 System.Int32
1 222 System.String
2 "3" System.String
111,222,"3"
Additional view of (un)importance of " double quotes in a string type shows next + operation (sum of integers but concatenation of strings):
write-host $i, $arraylist[$i], $arraylist[$i].GetType().Name, ($arraylist[$i] + 55)
gives next output:
0 111 Int32 166
1 222 String 22255
2 "3" String "3"55
111,222,"3"
However, " double quotes have another important and meaningful role in .csv file when imported e.g. to Excel sheet.
This worked for me:
[String]::Join(",", $arraylist.ToArray())
I got 1,2.
And then the second part:
foreach($number in $arraylist) { $number.ToString() }
I don't if this is correct. Usually, they have powershell2. Just give this a little try.
$a = #()
$strArrayNum=""
for($i=0; $i -lt $arraylist.length; $i++ ){
$strArrayNum += $element
$strArrayNum = $i+1 -eq $arraylist.length ? "" : ","
}
$a = $strArrayNum.Split(",")