powershellvbscriptiis-8.5

Execute PowerShell command from VBScript


I have a PowerShell command which removes virtual Folder from the IIS (8.5)

Remove-Item IIS:\Sites\WebsiteName\VirtualFolderName -Force -Recurse

This works just fine when executed standalone from PowerShell console, but I want to run this from inside of a VBScript. I have VBScript something like this:

Set objShell = CreateObject("WScript.Shell")
objShell.Run("powershell.exe -noexit -Command='Remove-Item IIS:\Sites\WebsiteName\VirtualFolderName -Force -Recurse'")

When I execute above it wont work, and it just spits out the command on the PowerShell console.

Any suggestions here?


Solution

  • Single quotes only work inside PowerShell. You need double quotes around the PowerShell statement, and you must double them to escape them inside the VBScript string. Also, remove the = between the parameter -Command and its argument. If the module WebAdministration isn't loaded automatically you need to do it yourself, otherwise you won't have a drive IIS:.

    Change this:

    objShell.Run("powershell.exe -noexit -Command='Remove-Item IIS:\Sites\WebsiteName\VirtualFolderName -Force -Recurse'")
    

    into this:

    objShell.Run("powershell.exe -noexit -Command ""Import-Module WebAdministration; Remove-Item IIS:\Sites\WebsiteName\VirtualFolderName -Force -Recurse""")