Microsoft Virtualization Discussions

Highlighted

write to file

I have a script which outputs the results to a text file with 'Out-File -Append'

I want to add a few lines to the text file:

$time = (Get-Date -format dd-MM_HH-mm)

Write-Host "*********************************************************************"

Write-Host "New Reading - $time"

Write-Host "*********************************************************************"

the above sends it to the console and not to the file itself

how do I put that in the file?

Thank you

12 REPLIES 12
Highlighted

Re: write to file

You need to look at Add-content - Alias AC

Re: write to file

tried this in the script - gave me gibberish

Add-Content c:\sm_status.txt "`n*********************************************************************"

Add-Content c:\sm_status.txt "`nNew Reading - $time"

Add-Content c:\sm_status.txt "`n*********************************************************************"

+++++++++++++++++++

⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪਍乮睥删慥楤杮ⴠㄠⴰ㠰ㅟⴴ㠱਍⩮⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪਍

Highlighted

Re: write to file

You need to review the help file 

Man ac

Highlighted

Re: write to file

read it - no go

tried this also:

Add-Content -path c:\sm_status.txt -value "`n*********************************************************************"

Add-Content -path c:\sm_status.txt -value "`nNew Reading - $time"

Add-Content -path c:\sm_status.txt -value "`n*********************************************************************"

I'm getting this:

⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪പഊ਀匀漀甀爀挀攀               䐀攀猀琀椀渀愀琀椀漀渀                      匀琀愀琀甀猀  䰀愀最吀椀洀攀吀匀ഀ਀ⴀⴀⴀⴀⴀⴀ               ⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀ                      ⴀⴀⴀⴀⴀⴀ  ⴀⴀⴀⴀⴀⴀⴀⴀⴀഀ਀挀漀渀开昀洀最㨀猀焀氀猀愀最攀猀礀猀搀戀 挀栀昀昀洀最昀氀爀  ㄀㨀猀洀开昀洀最开猀焀氀猀愀最攀猀礀猀搀戀 瀀攀渀搀椀渀最 ㄀㜀㨀 㔀㨀㔀㐀 ഀ਀ഀ਀ഀ਀ഀ਀匀漀甀爀挀攀                   䐀攀猀琀椀渀愀琀椀漀渀                          匀琀愀琀甀猀  䰀愀最吀椀洀攀吀匀ഀ਀ⴀⴀⴀⴀⴀⴀ                   ⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀ                          ⴀⴀⴀⴀⴀⴀ  ⴀⴀⴀⴀⴀⴀⴀⴀⴀഀ਀挀漀渀开昀洀最㨀猀焀氀昀洀最猀椀渀昀甀猀攀爀搀戀 挀栀昀昀洀最昀氀爀  ㄀㨀猀洀开昀洀最开猀焀氀昀洀最猀椀渀昀甀猀攀爀搀戀 瀀攀渀搀椀渀最 ㄀㜀㨀 㐀㨀㈀㔀 ഀ਀ഀ਀ഀ਀ഀ਀匀漀甀爀挀攀                         䐀攀猀琀椀渀愀琀椀漀渀                             匀琀愀琀甀猀 䰀ഀ਀                                                                              愀ഀ਀                                                                              最ഀ਀                                                                              吀ഀ਀                                                                              椀ഀ਀                                                                              洀ഀ਀                                                                              攀ഀ਀                                                                              吀ഀ਀                                                                              匀ഀ਀ⴀⴀⴀⴀⴀⴀ                         ⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀ                             ⴀⴀⴀⴀⴀⴀ ⴀഀ਀䘀䴀䜀㌀ ㈀ 一吀䰀㨀猀焀氀猀栀愀爀攀瀀漀椀渀琀甀猀攀爀搀戀 挀栀昀昀洀最昀氀爀  ㄀㨀猀洀开昀洀最开猀焀氀猀栀愀爀攀瀀漀椀渀琀甀猀攀爀搀戀 椀搀氀攀   ㄀ഀ਀ഀ਀ഀ਀ഀ਀匀漀甀爀挀攀                    䐀攀猀琀椀渀愀琀椀漀渀                           匀琀愀琀甀猀 䰀愀最吀椀洀攀吀ഀ਀                                                                       匀       ഀ਀ⴀⴀⴀⴀⴀⴀ                    ⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀ                           ⴀⴀⴀⴀⴀⴀ ⴀⴀⴀⴀⴀⴀⴀⴀഀ਀挀漀渀开昀洀最㨀猀焀氀渀攀氀猀漀渀猀渀愀瀀椀渀昀漀 挀栀昀昀洀最昀氀爀  ㄀㨀猀洀开昀洀最开猀焀氀渀攀氀猀漀渀猀渀愀瀀椀渀昀漀 椀搀氀攀   ㄀㘀㨀㈀㔀㨀㌀㠀ഀ਀ഀ਀ഀ਀ഀ਀匀漀甀爀挀攀                    䐀攀猀琀椀渀愀琀椀漀渀                           匀琀愀琀甀猀 䰀愀最吀椀洀攀吀ഀ਀                                                                       匀       ഀ਀ⴀⴀⴀⴀⴀⴀ                    ⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀ                           ⴀⴀⴀⴀⴀⴀ ⴀⴀⴀⴀⴀⴀⴀⴀഀ਀挀漀渀开昀洀最㨀猀焀氀渀攀氀猀漀渀猀渀愀瀀椀渀昀漀 挀栀昀昀洀最昀氀爀  ㄀㨀猀洀开昀洀最开猀焀氀渀攀氀猀漀渀猀渀愀瀀椀渀昀漀 椀搀氀攀   ㄀㘀㨀㈀㔀㨀㌀㤀ഀ਀ഀ਀ഀ਀ഀ਀匀漀甀爀挀攀             䐀攀猀琀椀渀愀琀椀漀渀                    匀琀愀琀甀猀 䰀愀最吀椀洀攀吀匀ഀ਀ⴀⴀⴀⴀⴀⴀ             ⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀ                    ⴀⴀⴀⴀⴀⴀ ⴀⴀⴀⴀⴀⴀⴀⴀⴀഀ਀挀漀渀开昀洀最㨀猀焀氀椀洀猀礀猀搀戀 挀栀昀昀洀最昀氀爀  ㄀㨀猀洀开昀洀最开猀焀氀椀洀猀礀猀搀戀 椀搀氀攀   ㄀㈀㨀㌀㘀㨀 㠀 ഀ਀ഀ਀ഀ਀ഀ਀匀漀甀爀挀攀              䐀攀猀琀椀渀愀琀椀漀渀                     匀琀愀琀甀猀  䰀愀最吀椀洀攀吀匀ഀ਀ⴀⴀⴀⴀⴀⴀ              ⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀ                     ⴀⴀⴀⴀⴀⴀ  ⴀⴀⴀⴀⴀⴀⴀⴀⴀഀ਀挀漀渀开昀洀最㨀猀焀氀椀洀甀猀攀爀搀戀 挀栀昀昀洀最昀氀爀  ㄀㨀猀洀开昀洀最开猀焀氀椀洀甀猀攀爀搀戀 瀀攀渀搀椀渀最 ㄀㌀㨀㄀㜀㨀㔀㘀 ഀ਀ഀ਀ഀ਀਀敎⁷敒摡湩⁧‭〱〭弸㐱㌭ഷഊ਀匀漀甀爀挀攀               䐀攀猀琀椀渀愀琀椀漀渀                      匀琀愀琀甀猀  䰀愀最吀椀洀攀吀匀ഀ਀ⴀⴀⴀⴀⴀⴀ               ⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀ                      ⴀⴀⴀⴀⴀⴀ  ⴀⴀⴀⴀⴀⴀⴀⴀⴀഀ਀挀漀渀开昀洀最㨀猀焀氀猀愀最攀猀礀猀搀戀 挀栀昀昀洀最昀氀爀  ㄀㨀猀洀开昀洀最开猀焀氀猀愀最攀猀礀猀搀戀 瀀攀渀搀椀渀最 ㄀㜀㨀 㜀㨀㌀㘀 ഀ਀ഀ਀ഀ਀ഀ਀匀漀甀爀挀攀                   䐀攀猀琀椀渀愀琀椀漀渀                          匀琀愀琀甀猀  䰀愀最吀椀洀攀吀匀ഀ਀ⴀⴀⴀⴀⴀⴀ                   ⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀⴀ                          ⴀⴀⴀⴀⴀⴀ  ⴀⴀⴀⴀⴀⴀⴀⴀⴀഀ਀挀漀渀开昀洀最㨀猀焀氀昀洀最猀椀渀昀甀猀攀爀搀戀 挀栀昀昀洀最昀氀爀  ㄀㨀猀洀开昀洀最开猀焀氀昀洀最猀椀渀昀甀猀攀爀搀戀 瀀攀渀搀椀渀最 ㄀㜀㨀 㘀㨀 㠀 ഀ਀ഀ਀ഀ਀ഀ਀匀漀甀爀挀攀                         䐀攀猀琀椀渀愀琀椀漀渀                             匀琀愀琀甀猀 䰀ഀ਀                                                                              愀ഀ਀                                                                              最ഀ਀

Highlighted

Re: write to file

Try using the add-content cmdlet.

Sent from my Windows Phone

Highlighted

Re: write to file

I see no issue

ac -path c:\test.log -value "---------------------"

ac -path c:\test.log -value "new reading $time"

ac -path c:\test.log -value "---------------------"

ac -path c:\test.log -value "`n---------------------"

here's the file

---------------------
new reading 10-08_13-11
---------------------

---------------------

Highlighted

Re: write to file

it doesn't work on my machine - no idea why..

thanks though

Highlighted

Re: write to file

Let me see the whole script

Highlighted

Re: write to file

$time = (Get-Date -format dd-MM_HH-mm)

Write-Host "*********************************************************************"

Write-Host "New Reading - $time"

Write-Host "*********************************************************************"

$FasUser = "root"

$pass = (get-content c:\pass.txt)

$SecureFasPasswd = ConvertTo-SecureString $pass -AsPlainText -Force

$FasCred = New-Object -TypeName System.Management.Automation.PSCredential ($FasUser, $SecureFasPasswd)

Connect-NaController -Name filer01 -Credential $FasCred

Add-Content -path c:\sm_status.txt -value "`n*********************************************************************"

Add-Content -path c:\sm_status.txt -value "`nNew Reading - $time"

Add-Content -path c:\sm_status.txt -value "`n*********************************************************************"

Get-NaSnapmirror -Location vol_01 | select source, destination, status, lagtimets | ft -AutoSize | Out-String -width 300 | Out-File c:\sm_status.txt -Append

Connect-NaController -Name filer02 -Credential $FasCred

Get-NaSnapmirror -Location vol_01 | select source, destination, status, lagtimets | ft -AutoSize | Out-String -width 300 | Out-File c:\sm_status.txt -Append

Highlighted

Re: write to file

Ok, you can do a few things differently

First, I see you are just checking al your snapmirrors.. What's your threshold?   You should have a trigger point, who wants to look at all your snapmirrors.

Also, you can look into using the excel com object or piping it to an HTML page.  If you search some of my scripts that will get you started

Highlighted

Re: write to file

I see an issue. Replace 'n with 'r'n. 'n is a line feed, which by itself is ok for unix. I bet you are on a windows machine. You need a carriage return and line feed or you will muck up your text file, thus the chinese-looking characters.

Sent from my Windows Phone

Highlighted

Re: write to file

਍⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪പഊ上睥删慥楤杮ⴠㄠⴰ㠰㉟ⴰ㤰਍

thank you

Cloud Volumes ONTAP
Review Banner
All Community Forums
Public