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

Posted on 2009-05-01
Last Modified: 2012-05-06
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

Question by:Unisys1
    LVL 70

    Expert Comment

    by:Chris Dent

    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?


    Author Comment

    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.
    LVL 70

    Expert Comment

    by:Chris Dent

    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.


    Accepted Solution

    It seems as though I am unable to do what I want to do here.  Since I am unable to determine if the input data includes a " or a ' then I cannot send the variable through as an argument.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now