none
Invoke-command Display the Computer name with the logfile Output RRS feed

  • Question

  • Hi,

    I am running a command to find the IIS log location on multiple servers, but I would like to alter the output so is shows the information with the Server name at the top and then the websites below. I know the invoke-command has the pscomputername, but I can't seem to pass this to my other script

    The first ps script lists the server name and then runs a powershell script to find the iis log location of all websites:

    Invoke-Command -Computername (Get-Content "computers.txt") -FilePath "c:\iislogdir.ps1"

    The iislogdir.ps1 then list the websites (but not the server name they come from)

    Import-Module WebAdministration

    foreach($website in $(get-website))

    {

      $logFile="$($website.logFile.directory)\w3scv$($website.id)".replace("%SystemDrive%",$env:SystemDrive)

       Write-host "$($WebSite.name) [$logfile]"

    }

    Ideally, I would like this to be output to a text file with output similar to below:

    Server1

    Default website: c:\inetpub\logfiles\w3scv1

    Test website: c:\intetput\logfiles\test

    Server2

    Default website: c:\inetpub\logfiles\w3scv1

    etc

    Any help with the output would be appreciated

    Thanks

     


    Jaz

    Wednesday, January 4, 2017 10:00 PM

Answers

  • Use this in your script instead:


    foreach ( $website in $(Get-Website) ) {
      $logDir = "$($website.logFile.directory)\W3SVC$($website.id)".Replace("%SystemDrive%",$env:SystemDrive)
      [PSCustomObject] @{
        "ComputerName" = $env:COMPUTERNAME;
        "Name"         = $website.name;
        "LogFiles"     = $logDir
      }
    }
    

    This will output objects with three properties, and you can easily export your script's output to CSV format.

    -- Bill Stewart [Bill_Stewart]


    Wednesday, January 4, 2017 10:23 PM
    Moderator

All replies

  • Use this in your script instead:


    foreach ( $website in $(Get-Website) ) {
      $logDir = "$($website.logFile.directory)\W3SVC$($website.id)".Replace("%SystemDrive%",$env:SystemDrive)
      [PSCustomObject] @{
        "ComputerName" = $env:COMPUTERNAME;
        "Name"         = $website.name;
        "LogFiles"     = $logDir
      }
    }
    

    This will output objects with three properties, and you can easily export your script's output to CSV format.

    -- Bill Stewart [Bill_Stewart]


    Wednesday, January 4, 2017 10:23 PM
    Moderator
  • Hi That's really useful, thanks.

    Just another question, is there anyway I can put in a test to say if the remote computer does not exist or if it cannot be connected to (due to wsman not configured), then add this information to an output file and continue to the next computer, instead of getting lots of error/warning messages in red when the system cannot be connected to? Would this test be put into the invoke-command script or the iislogdir.ps1 script?

    Thanks


    Jaz

    Thursday, January 5, 2017 2:11 PM
  • Use Test-Connection to verify the server responds to a ping before trying to connect to it.

    There are numerous examples of how to do this (search first).


    -- Bill Stewart [Bill_Stewart]

    Thursday, January 5, 2017 2:23 PM
    Moderator