Asked by:
Need help with double quotes in schedule task argument

General discussion
-
I'm running this PowerShell command via a schedule task argument but its not working because of the double quotes. Is there a way to make this work?
-ExecutionPolicy Bypass -Command "& { $list = (Get-WmiObject Win32_SystemUsers -ErrorAction SilentlyContinue).partcomponent ; if ($list) { [string] $CurrentComputerUsers = @(); foreach ($l in $list) {$domain = $l.Substring($l.IndexOf("`"")+1) ;$UG = $domain ; $domain = $domain.Substring(0,$domain.IndexOf("`"")) ;$UG = $UG.Substring($UG.IndexOf("`"")+1) ;$UG = $UG.Substring($UG.IndexOf("`"")+1) ;$UG = $UG.Substring(0,($UG.Length-1)) ; if (($UG -notmatch $env:COMPUTERNAME) -and ($UG -notmatch "NT SERVICE")) {$CurrentComputerUsers += $($domain + "@" + $UG + "`r`n")}; };} }"
99upgrade
- Changed type Bill_Stewart Monday, April 30, 2018 9:41 PM
- Moved by Bill_Stewart Monday, April 30, 2018 9:42 PM Unanswerable drive-by question
Thursday, March 1, 2018 7:13 AM
All replies
-
Put your code into a .ps1 File and run its as a script.
Call powershell.exe with the following attribute:
-file "C:\path\to\myScript.ps1" -ExecutionPolicy Bypass
Thursday, March 1, 2018 7:26 AM -
I want to run the command inside the argument instead of using a script.
99upgrade
Thursday, March 1, 2018 10:39 AM -
Then try this:
-ExecutionPolicy Bypass -Command '& { $list = (Get-WmiObject Win32_SystemUsers -ErrorAction SilentlyContinue).partcomponent ; if ($list) { [string] $CurrentComputerUsers = @(); foreach ($l in $list) {$domain = $l.Substring($l.IndexOf("`"")+1) ;$UG = $domain ; $domain = $domain.Substring(0,$domain.IndexOf("`"")) ;$UG = $UG.Substring($UG.IndexOf("`"")+1) ;$UG = $UG.Substring($UG.IndexOf("`"")+1) ;$UG = $UG.Substring(0,($UG.Length-1)) ; if (($UG -notmatch $env:COMPUTERNAME) -and ($UG -notmatch "NT SERVICE")) {$CurrentComputerUsers += $($domain + "@" + $UG + "`r`n")}; };} }'
I've just put the command inbetween single quotes (') instead of doubble quotes (").
This only works if your command does not use single quotes itself.
Thursday, March 1, 2018 11:27 AM -
I want to run the command inside the argument instead of using a script.
Why?
-- Bill Stewart [Bill_Stewart]
Thursday, March 1, 2018 3:11 PM