Powershell Remoting to lower versions

Tagged: 

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Daniel Krebs Daniel Krebs 1 month, 1 week ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #50856
    Profile photo of Sangeetha
    Sangeetha
    Participant

    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?

    #50882
    Profile photo of thom schumacher
    thom schumacher
    Participant

    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

    #50908
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    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.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.