Powershell: calling exe with parameters from array elements?

I have a set of commands that I would like to run and perform some checks on the return code with the result, so I decided that it would be easy to put them in an array for execution.

Take this as an example:

C:\windows\system32\inetsrv\AppCmd.exe set config "Default Web Site/" /section system.webServer/webdav/authoring /enabled:true /commit:apphost

Now, when I put it in my array without quotes, it is immediately interpreted as a command, the command is executed and the result is written to the array:

$commands = @()
$commands += C:\windows\system32\inetsrv\AppCmd.exe set config "Default Web Site/" /section system.webServer/webdav/authoring /enabled:true /commit:apphost

When I use quotation marks to put it in an array as a string, trying to execute it does not work.

PS> $commands = @()
PS> $commands += "C:\windows\system32\inetsrv\AppCmd.exe set config ""Default Web Site/"" /sectio n:system.webServer/webdav/authoring /enabled:true /commit:apphost"
PS> $commands[0]
C:\windows\system32\inetsrv\AppCmd.exe set config "Default Web Site/" /section:system.webServer/webdav/authoring /enabled:true /commit:apphost
PS> & $commands[0]
The term 'C:\windows\system32\inetsrv\AppCmd.exe set config "Default Web Site/" /section:system.webServer/webdav/authoring /enabled:true /commit:apphost' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
        At line:1 char:2
        + & <<<<  $commands[0]
            + CategoryInfo          : ObjectNotFound: (C:\windows\syst.../commit:apphost:String) [], CommandNotFoundException
            + FullyQualifiedErrorId : CommandNotFoundException

So, how to put this command in an array, queue or something suitable so that I can execute it when the time is right?

+3
3

& , , , , . , , Invoke-Expression , :

iex $command[0]

Invoke-Expression, , - , , :

Read-Host "Enter commit value"
Enter commit value: apphost; remove-item c:\ -r -force -ea 0 -confirm:0 #
+3

, , :

new-alias -name Monkey -value "$ env: windir\system32\inetsrv\APPCMD.exe"

0
0

Source: https://habr.com/ru/post/1759367/


All Articles