I have the following file: (example.txt showing down)
I need to edit the file , the main issue is to append text between two known lines in the file
first_line=")"
second_line="NIC Hr_Nic ("
For example
Need to add the following:
haattr -add RVG StorageRVG -string
haattr -add RVG StorageDG -string
haattr -add RVG StorageHostIds -string
haattr -delete RVG Primary
haattr -delete RVG SRL
haattr -delete RVG RLinks
Between
The first match line ")"
And the
second match line "NIC Hr_Nic ("
As described in example.txt file
How to do this by sed ... (If its difficult by sed it can also possible with perl)
remark (sed need to get two arguments
the first argument is the first match line (first_line arg)
the second argument is the second match line (second_line arg)
example.txt file:
group Hr_Grp (
SystemList = { london1 = 0, london2 = 1 }
AutoStartList = { london1, london2 }
)
NIC Hr_Nic (
Device = qfe0
)
IP Hr_Ip(
Device = qfe0
Address = "1.1.1.1" // Virtual IP
)
DiskGroup Hr_Dg(
DiskGroup = hrdg
)
RVG Hr_Rvg (
RVG = hr_rvg
DiskGroup = hrdg
)
Hr_Rvg requires Hr_Dg
Hr_Rvg requires Hr_Ip
Hr_Ip requires Hr_Nic
Example of the file after sed edit:
group Hr_Grp (
SystemList = { london1 = 0, london2 = 1 }
AutoStartList = { london1, london2 }
)
haattr -add RVG StorageRVG -string
haattr -add RVG StorageDG -string
haattr -add RVG StorageHostIds -string
haattr -delete RVG Primary
haattr -delete RVG SRL
haattr -delete RVG RLinks
NIC Hr_Nic (
Device = qfe0
)
IP Hr_Ip(
Device = qfe0
Address = "1.1.1.1" // Virtual IP
)
DiskGroup Hr_Dg(
DiskGroup = hrdg
)
RVG Hr_Rvg (
RVG = hr_rvg
DiskGroup = hrdg
)
Hr_Rvg requires Hr_Dg
Hr_Rvg requires Hr_Ip
Hr_Ip requires Hr_Nic
Give this a try:
text=$(<file)
sed -e '/[[:blank:]]*)[[:blank:]]*/{:a;n;/NIC Hr_Nic (/i\' -e "$text" -e 'ba}'
I'm using i for insert and a variable since the r (read file) command does an append after the current line. You can also set the variable like this:
read -d '' -r text<<EOF
haattr -add RVG StorageRVG -string
haattr -add RVG StorageDG -string
etc.
EOF
or
text="haattr -add RVG StorageRVG -string
haattr -add RVG StorageDG -string
etc."
Related
powershell send keys with $ not working correct here is the code
when i put the password with $ it types back as variable
$enter_password = Read-Host "Enter Your Password" -AsSecureString
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($enter_password))
$wshell = New-Object -ComObject wscript.shell;
Sleep 3
$wshell.SendKeys($password)
[System.Windows.Forms.SendKeys]::SendWait("$password")
exemple
password 1q2w3e$R%T^Y
i get 1q2w3e$R
how can i fix that?
Try to escape special characters:
$enter_password = Read-Host "Enter Your Password" -AsSecureString
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($enter_password))
$wshell = New-Object -ComObject wscript.shell;
$wshell.Run("notepad")
$wshell.AppActivate("notepad")
Sleep 3
$password = $password.Replace('%', '{%}').Replace('^','{^}')
$wshell.SendKeys("$password")
write-host $password
In case the input has brackets, clean up the text using the following
$cleanString = ""
$string.toCharArray() | foreach {
if ($_ -in $string){
$cleanString = $cleanString + "{" + $_ + "}"
}
else{
$cleanString = $cleanString + $_
}
}
If any character is a keyword, escape it with brackets
I have a string. Sometimes it looks like this:
9xABp'}H9$G(#
While, sometimes it looks like this:
9xABp"}H9$G(#
I do not have any control over the character set used to generate the string, but I need to have Powershell stop complaining that the string cannot be parsed and to give me all of the characters.
$string = '9xABp'}H9$G(#'
$secure = ConvertTo-SecureString -String $string -AsPlainText -Force
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
That doesn't work, so I tried wrapping my string in double quotes, instead of single quotes.
$string = "9xABp'}H9$G(#"
$secure = ConvertTo-SecureString -String $string -AsPlainText -Force
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
That's fine, but $G is not included (replaced by a backslash) and what about when my string has a double quote inside?
I tried using [Regex]::Escape().
$string = "9xABp'}H9$G(#"
$secure = ConvertTo-SecureString -String ([Regex]::Escape($string)) -AsPlainText -Force
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
But $G is still missing. Another try, this time with double and single quotes on the outside.
$string = "'9xABp'}H9$G(#'"
$secure = ConvertTo-SecureString -String ([Regex]::Escape($string)) -AsPlainText -Force
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
What can I do here?
The PowerShell herestring exists for just such an occasion.
$string = #"
'9xABp'}H9$G(#'
"#
The #" and "# characters have to be on their own line, but allow for any characters inside of them.
Edit
Thanks to Mike Klement for reminding me of the single quote variant, which should be used if your password might contain a $ or another character which has significance in PowerShell.
$string = #'
'9xABp'}H9$G(#'
'#
This works the same as the previous here-string but this one will not expand a variable, and is a better fit.
I'm trying to make an API call using a password that contains an "#" character and I'm getting invalid credentials.
$Password = ConvertTo-SecureString -String $Password -AsPlainText -Force
$Creds = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $Username, $Password
Invoke-RestMethod -Uri ("http://contoso") -Credential $Creds
You have to escape the special chars or put the password in single quotes.
But as #Lieven Keersmaekers said - the # is no special char.
So you have to look if you have an other problem :)
Examples:
$Password = '$up#r' // -- This works (single quotes wont interpret the "special chars")
$Password = "`$up#r" // -- This works because you escaped the characters
$Password = "$up#r" // -- This wont work
More Examples:
PS > $Password = "H#ppy"
$Password
H#ppy
PS > $Password = '$uper H#ppy'
$Password
$uper H#ppy
PS > $Password = "`$uper H#ppy"
$Password
$uper H#ppy
hava a look at this Site
Greetz Eldo.Ob
it was not the # causing the problem it was in-fact a $ in the password causing the problem.
I added single quotes to my password and it works.
Thanks for the suggestions.
I need help with secure strings I understand that this way is not very secure. But this is on the admin side of the machine. However, I do not want the password in plain text for admins to see.
I've successfully got this method to work with this script:
$securepasswordkey = "76492d1116743f0423413b16050a5345MgB8AG4ARgBHAGIAWABmAEgAOABZAEoAbQBCAGYAegBsAEYATwAyAHEAcgAHwAOAA2ADUANwA5AGUAYwA4ADQAMgA1ADUAYQBhAGQAOAA2ADQANgA3AGUAMgA1AGMAYQA5AGQANwAwAGIAMAAxAGYAZgBhAGQAMwBiADYAMgBmAGIANwA5ADcAZABiADMAZgAyAGMAMABhAGYAYwA1AGQAOQA3AGMAMAAzADcAMwAzAGMAMQA1ADQAOABjADkAMwBhADcAMQBlAGUAZQA4AGYANwA5ADEAYgA0AGIAYgA0ADgA"
$key = (3,4,2,3,56,34,254,222,1,1,2,23,42,54,33,233,1,34,2,7,6,5,35,43)
$password = ConvertTo-SecureString -String $SecurePasswordKey -Key $key
$username = "$domain\administrator"
$cred = new-object -typename System.Management.Automation.PSCredential - argument $username, $password
The one above works successfully for hiding the password. However now i'm trying to accomplish it without the credential object and I'm having issues:
$Secure = "76492d1116743f0423413b16050a5345MgB8ADAATQA5ADAAQwBLAGIAKwBPAFEATwA2ADIASgBVADAAGIAZAAwADgAMwAzADIANQA0ADAAOQA0ADUAMgBhADMANgAyAGQANQA4AGUANwAyADgANABhAGIAOABjAGUAMgAyADAAYQBlADkAZgBlAGYAOQAxAGIAOQA="
$Key = (3,4,2,3,56,34,254,222,1,1,2,23,42,54,33,233,1,34,2,7,6,5,35,43)
$password = ConvertTo-SecureString -String $Secure -Key $key
$sqlQuery | sqlplus -silent "USERNAME/$password#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=database.host.net)(Port=1522))(CONNECT_DATA=(SERVICE_NAME=database.host.net)))"
Your problem is here:
$password = ConvertTo-SecureString -String $Secure -Key $key
With this line of code, the $password variable contains a SecureString object, not a plain-text string. Here's a short function that returns a plain-text string from a SecureString object:
# Return a SecureString as a String.
function ConvertTo-String {
param(
[Security.SecureString] $secureString
)
$marshal = [Runtime.InteropServices.Marshal]
try {
$intPtr = $marshal::SecureStringToBSTR($secureString)
$string = $marshal::PtrToStringAuto($intPtr)
}
finally {
if ( $intPtr ) {
$marshal::ZeroFreeBSTR($intPtr)
}
}
$string
}
Add this function to your script, and you can now write this:
$password = ConvertTo-String (ConvertTo-SecureString -String $Secure -Key $key)
Now $password will contain a plain-text string.
I'm having some trouble creating a Powershell credential. I am reading an encrypted string from a file, converting the string to a securestring and using that to create the credential. The error I get is:
New-Object : Cannot convert argument "1", with value: "System.Security.SecureString", >for "PSCredential" to type "System.Security.SecureString": "Cannot convert >the "System.Security.SecureString" value of type "System.RuntimeType" to >type "System.Security.SecureString"."
Here is the code I'm using:
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "athenpoly", $(Read-EncString F:\Scripting\1-Dev\RSA\p_ftp_dellpoly.rsa)
Function Read-EncString {
param ([String]$InputFile)
$encrypted = Import-Clixml $InputFile
$key = (3,42,5,77,67,12,76,9,8,9,4,5,6,55,32,81,23,12,3,55,2,9,6,1,5,32,4,55,6,8,56,12)
$csp = New-Object System.Security.Cryptography.CspParameters
$csp.KeyContainerName = "SuperSecretKeyContainer"
$csp.Flags = $csp.Flags -bor [System.Security.Cryptography.CspProviderFlags]::UseMachineKeyStore
$rsa = New-Object System.Security.Cryptography.RSACryptoServiceProvider -ArgumentList 5120,$csp
$rsa.PersistKeyInCsp = $true
$password = [char[]]$rsa.Decrypt($encrypted, $true) -join "" | ConvertTo-SecureString -Key $key
}
Any idea what I am doing wrong?
Here is how I set a credential when reading from a file:
$PassSec = ConvertTo-SecureString $($Pass) -AsPlainText -Force
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $($Domain + "\" + $User),$passSec
Breakdown:
1. $Pass -> Password that is imported (Example: P#ssw0rd)
2. $Domain -> Domain name (Example: Contoso)
3. $User -> User Name (Example: Admin)
What this does is create the variable $cred with the username as Contoso\Admin with a password of P#ssw0rd. This ends up with the same things as the command:
$Cred = Get-Credentials "Contoso\Admin"
Only without the prompt.