Need help with powershell script to test path and Output in HTML, which can then be emailed

Hi Everyone,

I need help with a powershell script to check the path. the script is basically to check if a folder is created on all the servers or not. I'm not good with scripting but managed to put together something which can display the result. I can get the output, but can't get it in HTML and the file just gets appended. I need to create a new file every time the script runs or the same file can delete the old information and update with the latest. 

Please help.

Here's the script

$Servers = Get-content C:\temp\Testing\serverslist.txt
$logfile = new-item -ItemType File -name "FailedFolderCheck.html" -Force
Add-Content $logfile "<html><building><table><tr><td>ServerName</td><td>Comment</td></tr>"

Foreach ($Server in $Servers) {
$date = Get-Date -Format "MM-dd-yyyy HH:mm:ss";
$Test = Test-Path -path "\\$Server\E$\SMTP_Holding\"
If ($Test -eq $True) {Write-Host "Path exists on $Server."
}
Else {Write-Host "Path NOT exist on $Server."
}
}

Parents
No Data
Reply
  • I tried to get the script working but then when I checked the path, the path is not the same for couple of servers and I can't change the path, as it requires downtime. Can we get use two foreach

    $Servers = Get-content C:\temp\Testing\serverslist.txt
    $Path = Get-Content C:\temp\Testing\FailedFolderPath

    ##Not sure what the below part is doing

    $logfile = new-item -ItemType File -name "FailedFolderCheck.html" -Force
    Add-Content $logfile "<html><building><table><tr><td>ServerName</td><td>Comment</td></tr>"

    $(Foreach($Server in $Servers) {
    $date = Get-Date -Format "MM-dd-yyyy HH:mm:ss";

    #i would like to get this piece working without specifying the path as I have mentioned the path to check in the above variable $Path
    $Test = (Test-Path $Path)
    If ($Test -eq $True) {Write-Host "Path exists on $Server."
    }
    Else {Write-Host "Path NOT exist on $Server."
    }

    # Display Results
    New-Object -TypeName PSCustomObject -Property @{
    ServerName = $Server
    Date = $date
    Test = $test
    } | Select-Object -Property ServerName,Test,Date | ConvertTo-Html
    }) | Set-Content HtmlReport.html

    # Open HTML
    Invoke-Item HtmlReport.html

    Please can some one also let me know, where to i need to add the code for formatting the html report

Children
No Data