简体   繁体   中英

powershell script to to check disk space for each server individually

I have a script which I got from somewhere from MS forum this script works absolutely fine.I get the email and html file as needed But since in a multi domain environment I need to provide different credentials to different servers.So instead of a for loop for each server in the list using same credentials,it should treat each server with its credentials but in the same file and email Any Idea how to get this... Thanks in advance

$freeSpaceFileName = "c:\script\FreeSpace.htm"
$serverlist = "c:\script\computers.txt"
#$SecurePassword = 'Atlantic12' | ConvertTo-SecureString -AsPlainText -Force
#$Credential = New-Object Management.Automation.PSCredential 'tnt\administrator', $SecurePassword

$CredentialList = @{
    Cred1 = New-Object -TypeName pscredential -ArgumentList 'xax\administrator', (ConvertTo-SecureString -String 'Atlantic' -AsPlainText -Force);
    Cred2 = New-Object -TypeName pscredential -ArgumentList 'laptop\admin', (ConvertTo-SecureString -String 'password' -AsPlainText -Force);
    }

    $warning = 50
$critical = 30
New-Item -ItemType file $freeSpaceFileName -Force
# Getting the free space info using WMI
Get-WmiObject win32_logicaldisk  | Where-Object {$_.drivetype -eq 3} | format-table DeviceID, VolumeName,status,Size,FreeSpace | Out-File FreeSpace.txt
# Function to write the HTML Header to the file
Function writeHtmlHeader
{
param($fileName)
$date = ( get-date ).ToString('dd/MM/yyyy HH:mm:ss')
Add-Content $fileName "<html>"
Add-Content $fileName "<head>"
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $fileName '<meta http-equiv="refresh" CONTENT="5">'
Add-Content $fileName '<title>All Servers DiskSpace Report&copy;</title>'
add-content $fileName '<STYLE TYPE="text/css">'
add-content $fileName  "<!--"
add-content $fileName  "td {"
add-content $fileName  "font-family: Tahoma;"
add-content $fileName  "font-size: 20px;"
add-content $fileName  "border-top: 1px solid #999999;"
add-content $fileName  "border-right: 1px solid #999999;"
add-content $fileName  "border-bottom: 1px solid #999999;"
add-content $fileName  "border-left: 1px solid #999999;"
add-content $fileName  "padding-top: 0px;"
add-content $fileName  "padding-right: 0px;"
add-content $fileName  "padding-bottom: 0px;"
add-content $fileName  "padding-left: 0px;"
add-content $fileName  "}"
add-content $fileName  "body {"
add-content $fileName  "margin-left: 5px;"
add-content $fileName  "margin-top: 5px;"
add-content $fileName  "margin-right: 0px;"
add-content $fileName  "margin-bottom: 10px;"
add-content $fileName  ""
add-content $fileName  "table {"
add-content $fileName  "border: thin solid #000000;"
add-content $fileName  "}"
add-content $fileName  "-->"
add-content $fileName  "</style>"
Add-Content $fileName "</head>"
Add-Content $fileName "<body>"
add-content $fileName  "<br></br>"
add-content $fileName  "<table width='100%' align=center>"
add-content $fileName  "<tr bgcolor='#CCCCCC'>"
add-content $fileName  "<td colspan='7' height='25' align='center'>"
add-content $fileName  "<font face='tahoma' color='#003399' size='5'><strong>All Servers DiskSpace Report &copy; - $date</strong></font>"
add-content $fileName  "</td>"
add-content $fileName  "</tr>"
add-content $fileName  "</table>"
add-content $fileName  "<br></br>"
}

# Function to write the HTML Header to the file
Function writeTableHeader
{
param($fileName)

Add-Content $fileName "<tr bgcolor=#CCCCCC>"
Add-Content $fileName "<td width='10%' align='center'>Drive</td>"
Add-Content $fileName "<td width='50%' align='center'>Drive Label</td>"
Add-Content $fileName "<td width='10%' align='center'>Total Capacity(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Used Capacity(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Free Space(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Freespace %</td>"
Add-Content $fileName "</tr>"
}

Function writeHtmlFooter
{
param($fileName)

Add-Content $fileName "</body>"
Add-Content $fileName "</html>"
}

Function writeDiskInfo
{
param($fileName,$devId,$volName,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace - $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,0)
 if ($freePercent -gt $warning)
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$devid</td>"
 Add-Content $fileName "<td>$volName</td>"

 Add-Content $fileName "<td>$totSpace</td>"
 Add-Content $fileName "<td>$usedSpace</td>"
 Add-Content $fileName "<td>$frSpace</td>"
 Add-Content $fileName "<td>$freePercent</td>"
 Add-Content $fileName "</tr>"
 }
 elseif ($freePercent -le $critical)
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$devid</td>"
 Add-Content $fileName "<td>$volName</td>"
 Add-Content $fileName "<td>$totSpace</td>"
 Add-Content $fileName "<td>$usedSpace</td>"
 Add-Content $fileName "<td>$frSpace</td>"
 Add-Content $fileName "<td bgcolor='#FF0000' align=center>$freePercent</td>"
 #<td bgcolor='#FF0000' align=center>
 Add-Content $fileName "</tr>"
 }
 else
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$devid</td>"
 Add-Content $fileName "<td>$volName</td>"
 Add-Content $fileName "<td>$totSpace</td>"
 Add-Content $fileName "<td>$usedSpace</td>"
 Add-Content $fileName "<td>$frSpace</td>"
 Add-Content $fileName "<td bgcolor='#FBB917' align=center>$freePercent</td>"
 # #FBB917
 Add-Content $fileName "</tr>"
 }
}
Function sendEmail
{ param($from,$to,$subject,$smtphost,$htmlFileName)
$from=New-Object System.Net.Mail.MailAddress "server.space@tnt.com"
$to= New-Object System.Net.Mail.MailAddress "ttamboli2@tnt.com"
$cc= New-Object System.Net.Mail.MailAddress "ttamboli2@tnt.com"
$subject="Servers Disk space report - $Date" 
$smtphost="192.168.1.42"
$body = Get-Content $htmlFileName
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage $from, $to,$subject, $body
$msg.isBodyhtml = $true
$smtp.Send($msg)

}

writeHtmlHeader $freeSpaceFileName
foreach($server in Get-Content $serverlist)
{
 Add-Content $freeSpaceFileName "<table width='100%'><tbody>"
 Add-Content $freeSpaceFileName "<tr bgcolor='#CCCCCC'>"
 Add-Content $freeSpaceFileName "<td width='100%' align='center' colSpan=6><font face='tahoma' color='#003399' size='2'><strong> $server</strong></font></td>"
 Add-Content $freeSpaceFileName "</tr>"
 Add-Content $freeSpaceFileName "<br>"
 writeTableHeader $freeSpaceFileName

 $ComputerList = Import-Csv -Path c:\Computers.csv;
 foreach ($Computer in $ComputerList) 
 {
 Get-WmiObject -ComputerName $Computer.Name -Class Win32_LogicalDisk -Credential $CredentialList[$Computer.Credential] |  Where-Object {$_.drivetype -eq 3}
{
 Write-Host  $item.DeviceID  $item.VolumeName $item.FreeSpace $item.Size
 writeDiskInfo $freeSpaceFileName $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
    }
 }
 writeHtmlFooter $freeSpaceFileName
$date = ( get-date ).ToString('yyyy/MM/dd')
#sendEmail arif@tnt.com ttamboli2@tnt.com "Disk Space Report - $Date" hub1 $freeSpaceFileName

While the code you have provided has a lot of HTML formatting in it, I will share with you a much more simplified example of how to achieve what you're after.

  1. Turn your computers.txt into a CSV file, with two columns, that references a credential object by some friendly name (eg. cred1 or cred2 )
  2. In PowerShell create a HashTable of different credentials, and use the same friendly name that you used in the CSV file
  3. For each computer in the CSV file, use its name and credential to connect to WMI

Computers.csv

Name,Credential
dc01,cred1
dc02,cred1
dc03,cred1
exch01,cred2
exch02,cred2
exch03,cred2
client01,cred3
client02,cred3
client03,cred3

PowerShell Script

# Build a list of key/value pairs, to uniquely reference each of several credentials
$CredentialList = @{
    Cred1 = New-Object -TypeName pscredential -ArgumentList 'domain\user123', (ConvertTo-SecureString -String 'Password123' -AsPlainText -Force);
    Cred2 = New-Object -TypeName pscredential -ArgumentList 'domain\user456', (ConvertTo-SecureString -String 'Password456' -AsPlainText -Force);
    Cred3 = New-Object -TypeName pscredential -ArgumentList 'domain\user789', (ConvertTo-SecureString -String 'Password789' -AsPlainText -Force);
    }

# Get a list of computer/credential pairs
$ComputerList = Import-Csv -Path $PSScriptRoot\Computers.csv;

# For each computer in the CSV file, use the appropriate credential to contact WMI
foreach ($Computer in $ComputerList) {
    Get-WmiObject -ComputerName $Computer.Name -Class Win32_LogicalDisk -Credential $CredentialList[$Computer.Credential];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM