VMware Cloud Community
icrow1978
Contributor
Contributor

How to send a new output of the script to a new file?

Hi guys,

Im the powercli world and i need some help.

I create an script for collect the active alarm on vcenter, the script work fine .

Now i need to send the result to a txt, i use :

$date = get-date -uFormat "%d%m%y"

$alarms | out-file C:\PS-Scripts\ReporteAlarmasvcenterXX-$date.txt

So, the script then the outputo to the txt file but when i execute again the script the output is send it to the same file,

How can a send the output to a new file?  :-.( .,

I found that with the command get-date -format D i get the the date in the name of the file but when a tried .-format G "date + hour" i receive an error from the scrit , that the path is not found.

I need to change the path?

Thanks

Carlos

0 Kudos
5 Replies
dcouturier
Enthusiast
Enthusiast

Maybe this can help you. I used it every time I need. Change what you need. This part of script make a directory (if not exists) with date of the day

# Define the backup target path (this path will be created if it doesn't exist)

$ParentBackupLocation = "C:\Reports\"

$ResourceFileLocation = $ParentBackupLocation + "Resources\"

$BackupLocation = $ResourceFileLocation  + "{0:yyyy-MM-dd-tt}" -f (Get-Date)

$BackupLocationtest = Test-Path -path $BackupLocation

If ($BackupLocationtest -eq $false) {mkdir $BackupLocation | Out-Null}

Then you can put date into filename :

$Dateformat = get-date -f MMddyyyy

$filelocation=$BackupLocation + "\VCloud_Report_" + $Dateformat + ".txt"

0 Kudos
icrow1978
Contributor
Contributor

Hi ,

Thanks for your quickly reply. One question, "Resources is the new file that is move to the sub-folder Parent backup localtion?

Best regards

Carlos

0 Kudos
icrow1978
Contributor
Contributor

Hi,

Finally i follow your step but when i run the script i dont see the new file in the backuplocation

$alarms | out-file C:\Reports\VCloud_Report-$Dateformat.txt >>>>>>>>>>>><< Original file or first file generate during the day.

$ParentBackupLocation = "C:\Reports\"

$ResourceFileLocation = $ParentBackupLocation + "Resources\"

$BackupLocation = $ResourceFileLocation  + "{0:yyyy-MM-dd-tt}" -f (Get-Date)

$BackupLocationtest = Test-Path -path $BackupLocation

If ($BackupLocationtest -eq $false) {mkdir $BackupLocation | Out-Null}

Then you can put date into filename :

$Dateformat = get-date -f MMddyyyy

$filelocation=$BackupLocation + "\VCloud_Report_" + $Dateformat + ".txt"

This is due to the  >>>>>>>>>< $alarms | out-file C:\Reports\VCloud_Report-$Dateformat.txt

thanks!

0 Kudos
dcouturier
Enthusiast
Enthusiast

With my script, you should have created those directory before ("Report" and "Resources"). Otherwise, it will not found the full path

Regards

0 Kudos
devrawat2
Contributor
Contributor

@icrow1978 Simply use "Add-content". This will add the content to the new line. simple but effective.

0 Kudos