Author Posts

January 1, 2012 at 12:00 am

by WilliamD at 2012-10-29 09:00:01

Im new to powershell and need a little help. I have a script put together that does what I need it to do on a single server but I want it to query the folder properties of a collection of servers and include the servername in csv file. This is what I have so far:

$path = "c:\EVOfflineVault"
$CsvFilePath = "C:\Windows\temp\EVOfflineFoldersReport.csv"

Get-ChildItem $path |
Where-Object {$_.PSIsContainer} |
% {
$size = (Get-ChildItem $_.FullName -recurse -force |
Measure-Object -property length -sum -ErrorAction SilentlyContinue).sum
$obj = New-Object PSObject
Add-Member -inp $obj NoteProperty Name $_.Name
Add-Member -inp $obj NoteProperty Path $_.FullName
Add-Member -inp $obj NoteProperty Size $size
$obj
} | Sort-Object Size -Descending |
Select-Object Name, @{N="Size(KB)";E={"{0:N2}" -f ($_.Size / 1KB)} },Path |
Export-CSV -Path $CsvFilePath -NoTypeInformation

$file = $CsvFilePath
$att = new-object Net.Mail.Attachment("$file")
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient("mail.email.com",25)
$msg.From = "email@blah.com"
$msg.To.Add("email@blah.com")
$msg.Subject = "EVOfflineCache Citrix Report"
$msg.Body = "See Attached"
$msg.Attachments.Add($att)
$smtp.Send($msg)

by Jason_Yoder_MCT at 2012-10-29 15:59:29

Hi William,

You can use the Invoke-Command cmdlet to run your code remotely on other servers in your environment. The results will be returned to your client running the script. Here is an example:

$Servers = "Svr1", "Svr2", "Svr3"
ForEach ($Server in $Servers)
{
Invoke-Command -Scriptblock {
#< < Your code here >>
} -ComputerName $Server
}

Let me know if this helps.
Jason

}

by WilliamD at 2012-11-15 08:54:33

That would leave me collecting reports from different servers instead of having them in one place. The simple way for a few static servers is to populate $path with all the server UNCs, e.g. "\\server1\c$\EVOfflineVault", "\\server\c$\EVOfflineVault" ...

Getting the servers from AD is a better choice so I went with:
$erroractionpreference = "SilentlyContinue"

$CsvFilePath = "c:\whatever"

$dataColl = @()

Import-Module ActiveDirectory
$list = Get-ADComputer -SearchBase "OU=Citrix,OU=_Servers,DC=blah,DC=Com" -Filter * | Select -Expand Name
foreach ($computer in $list)
{

Get-ChildItem "\\$computer\c$\EVOfflineVault" |
Where-Object {$_.PSIsContainer} |
% {
$size = (Get-ChildItem $_.FullName -recurse -force |
Measure-Object -property length -sum).sum
$obj = New-Object PSObject
Add-Member -inp $obj NoteProperty Name $_.Name
Add-Member -inp $obj NoteProperty Path $_.FullName
Add-Member -inp $obj NoteProperty Size $size
$dataColl += $obj
$obj
}

$dataColl | Sort-Object Size -Descending |
Select-Object Name, @{N="Size(MB)";E={"{0:N2}" -f ($_.Size / 1MB)} },Path |
Export-Csv -noTypeInformation -path $CsvFilePath
}