-1

I`ve a script that triggers when new account is created (OnEvent 4720). I takes the data from the Event and pass it as a parameter to a powershell script. The problem is some names have a apostrophe and are not passed as a parameter.

TaskConfig:

...
<Value name="DisplayName">Event/EventData/Data[@Name='DisplayName']</Value>
...

TaskAction:

  <Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command>
  <Arguments>-Command c:\script\SendEmailNA.ps1 -SubjectUserSid '$(SubjectUserSid)' -SubjectUserName '$(SubjectUserName)' -SubjectDomainName '$(SubjectDomainName)' -SubjectLogonId '$(SubjectLogonId)' -TargetSid '$(TargetSid)' -TargetUserName '$(TargetUserName)' -TargetDomainName '$(TargetDomainName)' -SamAccountName '$(SamAccountName)' -DisplayName '$(DisplayName)' -UserPrincipalName '$(UserPrincipalName)' -HomeDirectory '$(HomeDirectory)' -HomePath '$(HomePath)' -ScriptPath '$(ScriptPath)' -ProfilePath '$(ProfilePath)' -UserWorkstations '$(UserWorkstations)' -PasswordLastSet '$(PasswordLastSet)' -AccountExpires '$(AccountExpires)' -PrimaryGroupId '$(PrimaryGroupId)' -AllowedToDelegateTo '$(AllowedToDelegateTo)' -OldUacValue '$(OldUacValue)' -NewUacValue '$(NewUacValue)' -UserAccountControl '$(UserAccountControl)' -UserParameters '$(UserParameters)' -SidHistory '$(SidHistory)' -LogonHours '$(LogonHours)' -PrivilegeList '$(PrivilegeList)'</Arguments>

SendEmailNA.ps1:

param($SubjectUserSid, $SubjectUserName, $SubjectDomainName, $SubjectLogonId, $TargetSid, $TargetUserName, $TargetDomainName, `
    $SamAccountName, $DisplayName, $UserPrincipalName, $HomeDirectory, $HomePath, $ScriptPath, $ProfilePath, $UserWorkstations, `
    $PasswordLastSet, $AccountExpires, $PrimaryGroupId, $AllowedToDelegateTo, $OldUacValue, $NewUacValue, $UserAccountControl, `
    $UserParameters, $SidHistory, $LogonHours, $PrivilegeList)

And then i take the variable to construct the body of a email. Everything works fine when it's "John Lee" but when it's "John O'Connell" it fails. Anybody have an idea how to pass an parameter with a apostrophe?

Thanks

dkmnt
  • 1
  • 2
    just replace quote with double quotes should do the trick `SubjectUserName '$(SubjectUserName)'` -> `SubjectUserName "$(SubjectUserName)"` – Loïc MICHEL Apr 02 '15 at 14:57
  • `'$(SubjectUserName)'` will just be `$(SubjectUserName)` due to [the single quotes](http://windowsitpro.com/powershell/variable-expansion-powershell-expressions). – E.V.I.L. Apr 02 '15 at 17:44
  • Both solution doesnt work. I get error in task scheduler: `Task Scheduler successfully completed task "\Account created" , instance "{8988d714-1a94-4b86-bb88-b02ad87114a3}" , action "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" with return code 1.` – dkmnt Apr 09 '15 at 11:34

1 Answers1

0

Found a answer.

It won't work with ps1 file but you can use PowerGUI to compile the script to .exe and then it will pass an argument with an apostrophe.

dkmnt
  • 1