Link to home
Start Free TrialLog in
Avatar of Unisys1
Unisys1Flag for United States of America

asked on

Custom Agurments that include Apostrophe (') from input with Powershell

Hi, I am trying to write a powershell or VB script to take an input and copy the entire input to the clipboard in an ordered format.  Within this copy I need to convert UTC time to local time including the DST offset when needed.  

=====Sample input====
script.ps1 -a:'Hello' -b:'$Time$' -c:'This is a string with some apostrophes' in it's messing up powershell'

====Sample Output I would Like copied to clipboard====
Server Name: Hello
Time:  Whatever time in local time
Description from Alert:
This is a string with some apostrophes' in it's messing up powershell





Use that in the code below.  The problem is that the C variable is an Alert from SCOM, and sometimes the alert includes apostroophes and errors the script.  I have loaded PSX (Powershell Extentions) and using the out-clipboard and that is working great.  Again it works if there are no apostrophies in C variable.  How can I fix this.  VBscript is acceptable also, and I thank you in advanced for any help.  Also the input time format is as follows:
5/1/2009 5:20:02 PM
That part of my script does work.
param($a, $b, $c)
[datetime]$AlertraisedUTC = $b
 
#=======Converts UTC to Local time====
$tzfind = Get-WMIObject -Class win32_Timezone
$localTZoffset =  $tzfind.Bias
If ((Get-Date).IsDaylightSavingTime() -eq "True")
{
$localTZoffset = $tzfind.Bias - $tzfind.DaylightBias 
}
$localtime = New-Object System.TimeSpan 0, 0, $localTZoffset, 0, 0
$b = $AlertraisedUTC.add($localtime)
#=====================================
 
$d = "Description from Alert:"
 
[string]$cliptext = "Server Name: $a `
`n Time: $b `
`n $d `
`n $c "
 
Out-Clipboard $cliptext
 
If ($cliptext) {Write-Host "Copied to clipboard"}

Open in new window

Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image


Double quotes instead of single quotes, and escape the apostrophe with `. e.g.

$TroublesomeString = "This string is trouble, isn`'t it?"

Single Quotes behave differently and you won't be able to escape the meaning of ' if using those (because it treats ` as a regular character with no special meaning).

Is that something you can implement prior to calling the script?

Chris
Avatar of Unisys1

ASKER

Ah I see what you mean, except I cannot control the input that is fed to the script

I fee the script:

script.ps1 -a:'$Name$' -b:'$Time$' -c:'$AlertDescription$'

Scom automatically fills in the double dollar sign variables, I cannot modify what is put into those variables so I cannot change:

"This string is trouble, isn't it?"
With a tick before the 't:
"This string is trouble, isn`'t it?"

Btw, those double dollar variables are only examples they are not actual SCOM variable fields.

Cool, I'd assumed as much since you weren't having problems with those :) Besides, if the value was already in a string the ' wouldn't be a problem :)

Anyway, looking again it seems to be quite happy with this kind of thing:

$StringValue = "A string with an apostrophe here '. But it's fine."

Which would suggest that it should also be happy with this:

script.ps1 -a:'$Name$' -b:'$Time$' -c:"$AlertDescription$"

It looks very much like you only need to escape the apostrophe if the string itself is enclosed in them.

Chris
ASKER CERTIFIED SOLUTION
Avatar of Unisys1
Unisys1
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial