Search code examples
powershelliispowershell-3.0powershell-4.0powershell-remoting

Get IIS Bindings from many webservers using powershell remoting, invoke command and module webadministration


Right now I made a small script to see my actual webbindings on a server.

Import-Module WebAdministration;

$today = Get-Date -format M.d.yyyy
$machine = hostname
function IISRport

{
Get-ChildItem -path IIS:\Sites | out-file "\\server01\d$\_utils\PowerShell Scripts\IISexport\IISExport$machine$today.txt"
}

IISReport 

This wil outfile my IIS bindings to txt which works brilliant. I now need to invoke that command to several other servers and save the output file on one server. since the machine name will be diffrent I expect as many outputs as servers.

I tried for example the following :

icm -comp server02 {get-childitem -path IIS:\Sites}

But than the imported Module webadministration is not working, since I only loaded on one server. So I tried to load that remotely using :

icm -comp server02 {import-module webadministration}

without success.

How to achieve that?


Solution

  • This will get the data from all machines defined in $machines and output to \\server01\d$_utils\PowerShell Scripts\IISexport\.

    $today = Get-Date -format M.d.yyyy
    $machines = @("server01","server02")
    foreach($machine in $machines) {
        icm -comp $machine { param($machine,$today)
            import-module webadministration;
            Get-ChildItem -path IIS:\Sites | 
            out-file "\\server01\d$\_utils\PowerShell Scripts\IISexport\IISExport$machine$today.txt"
        } -ArgumentList $machine,$today 
    }
    

    You can of course change the output path( and import list of machines from a file,AD or some other source).