Author Posts

August 17, 2016 at 5:30 pm

Hi,

I need to run a powershell script using commands in the webadministration module on a web server with powershell 2.0 remotely. This is because, I cannot upgrade powershell version on the server.

Can I use an admin server with powershell 3.0 or above to execute the script\commands remotely on the web server?

August 17, 2016 at 7:24 pm

Webadministration module is only available for 3.0 and up powershell.

So if your target server is 2.0 then you'll have to reflect in the administration module from Dotnet.

(get-module -ListAvailable webadministration).PowerShellVersion
Major  Minor  Build  Revision
-----  -----  -----  --------
3      0      -1     -1  

To reflect in the webadmin module here is a snipet of code to do that:

[System.Reflection.Assembly]::LoadFrom('C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll')

Here is the location for the web.administration namespace for dot net.
https://msdn.microsoft.com/en-us/library/microsoft.web.administration(v=vs.90).aspx

August 18, 2016 at 5:21 am

Thom, I hope you don't mind that I chimp in to correct your answer.

Yes, the WebAdministration module becomes available on a Windows Server 2008 R2 once the Web-Server feature has been installed. The PowerShell version on the web server is irrelevant because the module comes with the Web-Server feature of the underlying OS version. An upgrade of PowerShell/WMF (Windows Management Framework) to 3.0 or later does not update the WebAdministration module.

Tip 1: PowerShell v2 does not support module auto-loading so you'll need to use the Import-Module cmdlet.
Tip 2: The WebAdministration module in any version does not support remote administration like other modules. You always need to use a PSSession with the appropriate commands to achieve your tasks or run the commands locally using a configuration management tool like Ansible, Chef, Puppet, SCCM, etc.

A) Simple example using an embedded script block:

$MyScriptBlock = {
    Import-Module -Name WebAdministration

    # List all websites installed on the machine
    Get-Website | Select-Object -Property id, name, state

    # The IIS: drive is also available
    Set-Location -Path IIS:\AppPools
    Get-ChildItem | Select-Object -Property name, state, managedPipelineMode
}

Invoke-Command -Computer Web1 -ScriptBlock $MyScriptBlock

B) Simple example using a script saved in another file:
1) Create task script file "WebAdminTask.ps1" with the following content and save it into the Documents folder of your user profile:

Import-Module -Name WebAdministration

# List all websites installed on the machine
Get-Website | Select-Object -Property id, name, state

# The IIS: drive is also available
Set-Location -Path IIS:\AppPools
Get-ChildItem | Select-Object -Property name, state, managedPipelineMode

2. Create a controller script "WebAdminController.ps1" with the following content:

Invoke-Command -Computer Web1 -FilePath ~\Documents\WebAdminTask.ps1

I hope above is helpful.