Jump to content

Recommended Posts

Posted

I'm trying to run this powershell command from Autoit and can't figure out how to pull it off:

Get-ProvisionedAppxPackage -Online | Where-Object { $_.PackageName -match "xbox" } | ForEach-Object { Remove-ProvisionedAppxPackage -Online -AllUsers -PackageName $_.PackageName }

I've been trying to run it many different ways including:

$sCMD = 'Get-ProvisionedAppxPackage -Online | Where-Object { $_.PackageName -match "xbox" } | ForEach-Object { Remove-ProvisionedAppxPackage -Online -AllUsers -PackageName $_.PackageName }'
RunWait(@comspec & ' /c powershell.exe -nologo -executionpolicy bypass -noprofile -Command "&' & $sCMD & '"')

The problem is that it seems I'm missing something in how to escape or double the quotes. I've tried doubling the quotes in many different ways, but the end result always produces a syntax error in powershell. I could just run powershell first, then paste and run the command, then close the powershell window, but that's clunky. I'm trying to do it either via parameter (as above) or in one line like this:

RunWait(@comspec & ' /c powershell.exe -nologo -executionpolicy bypass -noprofile -Command "&Get-ProvisionedAppxPackage -Online | Where-Object { $_.PackageName -match "xbox" } | ForEach-Object { Remove-ProvisionedAppxPackage -Online -AllUsers -PackageName $_.PackageName }"')

 

Posted

Well I guess I get to answer my own question. I found a hint in this thread regarding changing the double quotes to single and vice versa.

This seems to work:

$sPSCmd = "Get-ProvisionedAppxPackage -Online | Where-Object { $_.PackageName -match 'xbox' } | ForEach-Object { Remove-ProvisionedAppxPackage -Online -AllUsers -PackageName $_.PackageName }"
RunWait(@comspec & ' /c powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -command "&' & $sPSCmd & '"')

No idea why flopping the quotes on the variable to double and the inner quotes to single fixes this... I swear I've been working for over an hour on this.

Posted (edited)

Many languages including AutoIt tackle the single/double quotes in strings by starting a string with either a ' or " to handle easier the difference

Predict output of below and you will understand better (although me does't understand the full logical explanation either)

$var1="''''''"
$var2='""""""'
$var3=""""""""
$var4=''''''''
; $var5='"'"'"'"'  ; Interesting edge case  ==> Error in expression
; $var6="'"'"'"'"  ; Interesting edge case  ==> Error in expression

consolewrite("test1: <" & $var1 & ">" & @CRLF)
consolewrite("test2: <" & $var2 & ">" & @CRLF)
consolewrite("test3: <" & $var3 & ">" & @CRLF)
consolewrite("test4: <" & $var4 & ">" & @CRLF)
consolewrite("test5: <" & $var5 & ">" & @CRLF)
consolewrite("test6: <" & $var6 & ">" & @CRLF)

and in powershell the character to escape the quote is the backtic ` opposed to many languages using the / as an escape character.

Edited by junkew
Logic is that 2 "" within the string the " is used as an escape character. So first " starts the string and "" then actually means add 1 "

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...