VMware Cloud Community
DZ1
Hot Shot
Hot Shot
Jump to solution

New line `n is not working

I'm writing a small file out to a log, and it will hold the names of multiple VMs.  I want to make sure that I put the date with the VM names, and then I want a new line for each VM.  I tried something like this:

("$(Get-date -format MM-dd-yy) `r `n" + "$Removed") | Out-File c:\Scripts\test.txt -Append

The date shows, and at first I was only using `n for the new line, and then I tried `r and `n, but everything gets put on one line, and then spaced apart, like this:

09-12-13   VMname VMname

I'm sure I'm missing something simple, but why isn't the new line appearing? 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try changing that line into

("$(Get-date -format MM-dd-yy)") + "`r`n" + ($Removed | out-string) | Out-File C:\deleted.txt -Append


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

You seem to have a blank between the `r and the `n, try like this

("$(Get-date -format MM-dd-yy) `r`n" + "$Removed") | Out-File c:\Scripts\test.txt -Append


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

lol, wow, I can't believe how easy that was (for you).  I would have been working on that all day.  Well, that works, but I have 1 last question, and maybe this is because it's a text file, I'm not sure. When I email the list, each VM is on its own line, but in the text file, now it shows the date, and then it drops to a new line, but then the VM names are all on one line.  I want a VM name on each line. 

-Body "Removed: `n `n $($Removed| out-string)  "

Of course there is more to the line above, but I tested it and it outputs every VM on its own line (at least in the email that gets sent out), and the space does not matter, which is odd, or maybe outlook is doing something.  I include the date in the subject of the email, so I don't worry using Get-Date with the email output, but that does not work in the text file.  How do I put each VM on a new line?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you could show me how you compiled the content of the $Removed variable ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

if ( (Get-PSSnapin -Name VMware.VimAutomation.Core) -eq $null ) {

  Add-PSSnapin -Name VMware.VimAutomation.Core

}

$cred = Get-VICredentialStoreItem -File c:\Scripts\cred.xml

Connect-VIServer "1.1.1.1" -User $cred.user -Password $cred.password

$today = Get-Date

$Removed = @()

$NotRemoved = @()

Get-VM | where {$_.name -cmatch "Delete_\d\d\.\d\d\.\d\d"} | foreach {

[datetime]$dt=($_.name.Split("_",2)[1])

if ($dt.Month -eq $today.Month -and $dt.Day -eq $today.Day -and $dt.Year -eq $today.Year) {

$Removed += $_.name

$Removed | Out-File C:\Scripts\deleted.txt -Append #this is new, and it does not work

Remove-VM -VM -DeletePermanently $_ -Confirm:$false

}

else

{

$NotRemoved += $_.name

}

}

(Send-MailMessage -SmtpServer '1.2.3.4' -From "<m@a.com>" -To "<m@acom>" -Subject VMs -Body "VMs that have been deleted: `n `n $($NotRemoved | out-string)")

(Send-MailMessage -SmtpServer '1.2.3.4' -From "<@com>" -To "<@a.com>" -Subject VMs -Body "VMs that are/were scheduled to be deleted, but the dates don't match: `n `n $($Removed | out-string)")

Disconnect-VIServer * -Confirm:$false

Thanks, this is the script, and you helped me with the split on the date and converting it, so thanks for that Smiley Happy.  I tested this out, and the emails come out just fine, and then I decided to add the txt file log to save the dates long term for VMs that are deleted.  This script does not include the new part where I was saving the text file, this is the script that I was initially going to run, and then I decided to add on to it.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Shouldn't that line be

$_.Name | Out-File C:\Scripts\deleted.txt -Append #this is new, and it does not work

Otherwise you will append the complete array $Removed each time.

An alternative is to move that line outside the Get-VM loop, and then remove the Append switch


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

Thanks for all the help so far, I can't get this one part to work, but here is the script so far:

<#

if ( (Get-PSSnapin -Name VMware.VimAutomation.Core) -eq $null ) {

  Add-PSSnapin -Name VMware.VimAutomation.Core

}

$cred = Get-VICredentialStoreItem -File c:\Scripts\cred.xml

Connect-VIServer 1 -User $cred.user -Password $cred.password

#>

$today = Get-Date

$Removed = @()

$NotRemoved = @()

Get-VM | where {$_.name -cmatch "Delete_\d\d\.\d\d\.\d\d"} | foreach {

[datetime]$dt=($_.name.Split("_",2)[1])

if ($dt.Month -eq $today.Month -and $dt.Day -eq $today.Day -and $dt.Year -eq $today.Year) {

$Removed += $_.name

#("$(Get-date -format MM-dd-yy)") + "`n" + "$Removed" | Out-File C:\Scripts\deleted.txt -Append

#Remove-VM -VM $_ -DeletePermanently $_ -Confirm:$false

}

else

{

$NotRemoved += $_.name

}

}

("$(Get-date -format MM-dd-yy)") + "`r`n" + "$Removed" | Out-File C:\Scripts\deleted.txt -Append

(Send-MailMessage -SmtpServer '1' -From "<>" -To "<>" -Subject VMs -Body "VMs that have been deleted:`n`n $($Removed | out-string)")

(Send-MailMessage -SmtpServer '1' -From "<>" -To "<>" -Subject VMs -Body "VMs that are/were scheduled to be deleted, but the dates don't match:`n`n $($NotRemoved | out-string)")

#Disconnect-VIServer * -Confirm:$false

The section in red is my problem still, I have been playing with it, trying to change it up, but it won't output correctly.  If I leave off the date, the contents of the variable $Removed will show on their own line in the text file, but adding the date will show the date, and then it will drop down to a new line, but then each VM is put on the same line. 

Date

VMname VMname

Of course, I want this:

Date

VMname

VMname

And thanks again, I did have the output inside the foreach, so I moved it accordingly.  Thanks for all your help. 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try changing that line into

("$(Get-date -format MM-dd-yy)") + "`r`n" + ($Removed | out-string) | Out-File C:\deleted.txt -Append


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

Can you be any smarter?  I really appreciate all of your help, that worked perfectly.  I was going to try an alternate method below:

("$(Get-date -format MM-dd-yy)") + "`r`n`n" + $Removed -split " "

Of course, yours works much better since there could have been a rename issue of a VM that included a space.  Thank you so much, all I can offer you is to purchase your next book when it's available.  Many thanks. 

Reply
0 Kudos