Not able to put my Switch output to a logfile

Hi Guys,

Here is the piece of code where i am picking an IP and connecting to it and then running a command to show all the port errors and then trying to put that output into a text file. At last sending that output to my mail.

So everything is working out as perfect as i want but i am getting a mail with an empty attachment, where i don’t see my port errors and rather the date and the IP . could anybody please help me in solving the issue. i am sure that i am missing a small one.

$EmailFrom = “
$EmailTo =""
$logFile = “Brocade-Log-” + (Get-date -f dd ).tostring() + (Get-date -f MM ).tostring() + ((Get-Date).year).tostring() + “.txt”

(get-date) > $logFile

Get-Content C:\Users\xyzpa\Desktop\fibre-switches.txt | % { $_ >> $logFile ; & ‘C:\Users\xyzpa\Desktop\putty.exe’ -l admin -pw Sty34viR -m C:\Users\xyzpa\Desktop\brocade1.txt $_ >> $logFile}
#Get-Content .\Desktop\fibre-switches.txt | % { & ‘.\Desktop\putty.exe’ -l admin -pw password -m .\brocade2.txt $_ }

$MyReport = “Cleared Fibre switch errors for all brocade switches”
Send-MailMessage -To $EmailTo -Subject “Brocade switch error clear.” -From $EmailFrom -Body $MyReport -SmtpServer $SMTPSRV -Attachments $LogFile

output of the above script.

Friday, November 17, 2017 6:53:26 PM

Here is the files used in the script.

fibre-switches.txt :
brocade1.txt : porterrshow

I would put full path into the $logfile, that way I know where it is being created and then go check it. Otherwise you might be trying to create it in Windows\System

I Tried with full path but not working. Same output is coming. and what will i create in windows/system

I tried with only the below but still is not coming anything in the output.

$logFile = ‘C:\Users\khvs087pa\Desktop\porterrshow.txt’

(get-date) >> $logFile

Get-Content C:\Users\khvs087pa\Desktop\fibre-switches.txt | % { $_ >> $logFile ; & ‘C:\Users\khvs087pa\Desktop\putty.exe’ -l admin -pw xyz -m C:\Users\khvs087pa\Desktop\brocade1.txt $_ >> $logFile}

and the output is same:

Friday, November 17, 2017 7:29:20 PM

Not what you’re looking for but couldn’t help notice how you name your file, with the date

$logFile = "Brocade-Log-" + (Get-date -f dd ).tostring() + (Get-date -f MM ).tostring() + ((Get-Date).year).tostring() + ".txt"

…and wanted to suggest

$logfile = "Brocade-Log-$((GET-DATE).ToString("ddMMyyyy")).txt"

which will return: “Brocade-Log-17112017.txt”

thanks for the suggestion @iain

I am having a very specific issue with scheduling a PowerShell script to run with Task Scheduler in Windows 2012 server. The script works as expected from the PowerShell ISE console. The script is checking the size of C:\ and a particular log folder size and if the log folder size is more than 20 GB then it clears the logs of the folder with the help of application.

The stop application stops 3 of the particular services and while starting the start application undergoes a interactive session with CMD and if we press y and enter , it will clear the logs and start those 3 services.

Then it will send the mail the output.

The issue is, if i start the script from powershell console it complete successfully as expected but if i do the same from task scheduler then only the start application is not executing and the task is getting completed with stop application and sending the mail. The middle part is not happening.

$cdrive = $NULL
$percentage = $NULL
$freespace = $NULL
$size = $NULL

$cdrive = gwmi win32_logicaldisk -Filter “DeviceId=‘C:’”
$size = [math]::Round($cdrive.Size/1GB,0)
$freespace = [math]::Round($cdrive.FreeSpace/1GB,0)
$percentage = [math]::Round($cdrive.FreeSpace/$cdrive.Size * 100,0)

if($percentage -gt 50)



$logsize = $NULL

$logsize = [math]::Round((Get-ChildItem C:\HPE\HPSS\pa\tomcat\logs -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB,0)

if($logsize -lt 10)

$cdrive1 = $NULL
$percentage1 = $NULL
$size1 = $NULL
$freespace1 = $NULL
$ID1 = $NULL
Add-Type -AssemblyName ‘System.Windows.Forms’
$ID1 = (Start-Process ‘C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Hewlett Packard Enterprise\HPE XP7 Performance Advisor Management Station\Stop Services’ -PassThru).Id
Add-Type -AssemblyName Microsoft.VisualBasic
Sleep 90

sleep 20
this below start function only not executing.
$ID2 = $NULL
#Add-Type -AssemblyName ‘System.Windows.Forms’
$ID2 = (Start-Process ‘C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Hewlett Packard Enterprise\HPE XP7 Performance Advisor Management Station\Start Services’ -PassThru).Id
sleep 10
Add-Type -AssemblyName Microsoft.VisualBasic
sleep 10
sleep 30

$cdrive1 = gwmi win32_logicaldisk -Filter “DeviceId=‘C:’”
$size1 = [math]::Round($cdrive1.Size/1GB,0)
$freespace1 = [math]::Round($cdrive1.FreeSpace/1GB,0)

$percentage1 = [math]::Round($cdrive1.FreeSpace/$cdrive1.Size * 100,0)

$EmailFrom = “”
$EmailTo =“”
$MyReport = @"

Script has cleared the HP performance unused logs from the path C:\HPE\HPSS\pa\tomcat\logs and below is the output of C: drive.

Before log deletion

C: Total size in GB = $size
C: Freespace in GB = $freespace
C: Freespace in % = $percentage

After Log Deletion

C: Total size in GB = $size1
C: Freespace in GB = $freespace1
C: Freespace in % = $percentage1

Send-MailMessage -To $EmailTo -Subject “HP Performance advisor log status” -From $EmailFrom -Body $MyReport -SmtpServer $SMTPSRV


$cdrive2 = $NULL
$percentage2 = $NULL
$cdrive2 = gwmi win32_logicaldisk -Filter “DeviceId=‘C:’”

$percentage2 = [math]::Round($cdrive2.FreeSpace/$cdrive2.Size * 100,0)
$logsize1 = $NULL

$logsize1 = [math]::Round((Get-ChildItem C:\HPE\HPSS\pa\tomcat\logs -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB,0)
$EmailFrom = “
$EmailTo =“
$MyReport = @"

C: drive freespace is < 50% but the HP Performance log folder "C:\HPE\HPSS\pa\tomcat\logs" is not occupied more than 10GB.

Current size of the log folder is $logsize1 MB.


Send-MailMessage -To $EmailTo -Subject "HP Performance advisor log status" -From $EmailFrom -Body $MyReport -SmtpServer $SMTPSRV