?
Solved

Powershell output to logfile

Posted on 2009-02-18
9
Medium Priority
?
9,913 Views
Last Modified: 2012-05-06
I guess this question is asked already many times.
First off i`m not a script.
With days of works i have created the following script.
I used the start-transcript to write to a logfile.
But i need more...
I would like to have the output that i see if i run the script within a powershell console in a logfile.
#################################################################
# 																#
# Copyright IT Creation, all rights reserved!					#
# 																#
# Name:My first real script 									#
# Version: 	1.0													#
# Purpose: 	Mounting latest snapshot(s) to backup server.		#
# Scripter: Ghans!!!											#
#																#
# Requires: Common Sense										#
#																#
# Version history: 												#
#					V1.0, making it!							#
#################################################################
# Ok, lets set some variables
$Iportal = "192.168.0.0"
$Iport = "3260"
$ip = "\\servername"
$script = "c:\backup_scripts\make_snap.bat"
$servername = "servername"
$log = "F:\File_backup_log\pre_$servername.log"
 
# Logging!!
Start-Transcript -path $log
 
mkdir e:\mount_path\$servername
mkdir $env:temp\$servername
 
sleep 3
 
# Ok, refresh of the iSCSI targetportal!
iscsicli.exe refreshtargetportal $Iportal $Iport
sleep 3
 
# Now we are going to what all existing targets are.
iscsicli.exe listtargets > beforesnapshot.txt
sleep 3
 
# Next step is to let pstools run a script on the target server.
F:\Backup_Scripts\psexec.exe $ip $script
sleep 3
 
# Refresh again!
iscsicli.exe refreshtargetportal $Iportal $Iport
sleep 3
 
# Again all targets into a textie
iscsicli.exe listtargets > aftersnapshot.txt
sleep 3
 
# all disks in a textie
sc $env:temp\$servername\$servername.x "list disk" -encoding ascii
$diskbefore=diskpart /s $env:temp\$servername\$servername.x
sleep 3
 
# OK, allmost there, now lets see what my snapshot is called!
$iqn = Compare-Object -referenceObject $(Get-Content beforesnapshot.txt) -differenceObject $(Get-Content aftersnapshot.txt) | %{$_.Inputobject}
sleep 3
 
# removing some white-space
$iqn = $iqn.Trim()
sleep 3
 
# mounting target
iscsicli.exe logintarget $IQN T * * * * * * * * * * * * * * * 0
sleep 3
 
# all disk agai
sc $env:temp\$servername\$servername.x "list disk" -encoding ascii
$diskafter=diskpart /s $env:temp\$servername\$servername.x
sleep 3
 
# lets see what new disk we have
$disknr = Compare-Object -referenceObject $diskbefore -differenceObject $diskafter | %{$_.Inputobject}
$disknr = [system.string]::join(" ",$disknr.Split()[2..3])
sleep 3
 
# lets see what new volume we have!
sc $env:temp\$servername\$servername.x "select $disknr","detail disk" -encoding ascii
$volumenr = diskpart /s $env:temp\$servername\$servername.x 
switch -regex ($volumenr)
{
   "(?<Volume>Volume\s\d+)"   {$volumenr = $matches.Volume}
}
sleep 3
 
# lets now mount the volume!
sc $env:temp\$servername\$servername.x "select $disknr","select $volumenr","assign mount=e:\mount_path\$servername" -encoding ascii
diskpart /s $env:temp\$servername\$servername.x 
sleep 3
 
#Prepare file for delete snapshot script
sc $env:temp\$servername\$servername.x "select $disknr", "select $volumenr","remove" -encoding ascii
 
# now lets put information in temp file
sc $env:temp\$servername_output.x "IQN = $IQN","DISKNR = $Disknr","VOLUMENR = $volumenr" -encoding ascii
 
Stop-Transcript

Open in new window

0
Comment
Question by:Hans de Jongh
  • 4
  • 3
  • 2
9 Comments
 
LVL 18

Expert Comment

by:BSonPosh
ID: 23671138
Start-Transcript should definitely give you all the data, but you can also use out-file on each line.

line of code | out-file LogName -append -enc ASCII

or you could cheat (although not sure of the results by using the code below
$Iportal = "192.168.0.0"
$Iport = "3260"
$ip = "\\servername"
$script = "c:\backup_scripts\make_snap.bat"
$servername = "servername"
$log = "F:\File_backup_log\pre_$servername.log"
 
# Logging!!
@(mkdir e:\mount_path\$servername
mkdir $env:temp\$servername
 
sleep 3
 
# Ok, refresh of the iSCSI targetportal!
iscsicli.exe refreshtargetportal $Iportal $Iport
sleep 3
 
# Now we are going to what all existing targets are.
iscsicli.exe listtargets > beforesnapshot.txt
sleep 3
 
# Next step is to let pstools run a script on the target server.
F:\Backup_Scripts\psexec.exe $ip $script
sleep 3
 
# Refresh again!
iscsicli.exe refreshtargetportal $Iportal $Iport
sleep 3
 
# Again all targets into a textie
iscsicli.exe listtargets > aftersnapshot.txt
sleep 3
 
# all disks in a textie
sc $env:temp\$servername\$servername.x "list disk" -encoding ascii
$diskbefore=diskpart /s $env:temp\$servername\$servername.x
sleep 3
 
# OK, allmost there, now lets see what my snapshot is called!
$iqn = Compare-Object -referenceObject $(Get-Content beforesnapshot.txt) -differenceObject $(Get-Content aftersnapshot.txt) | %{$_.Inputobject}
sleep 3
 
# removing some white-space
$iqn = $iqn.Trim()
sleep 3
 
# mounting target
iscsicli.exe logintarget $IQN T * * * * * * * * * * * * * * * 0
sleep 3
 
# all disk agai
sc $env:temp\$servername\$servername.x "list disk" -encoding ascii
$diskafter=diskpart /s $env:temp\$servername\$servername.x
sleep 3
 
# lets see what new disk we have
$disknr = Compare-Object -referenceObject $diskbefore -differenceObject $diskafter | %{$_.Inputobject}
$disknr = [system.string]::join(" ",$disknr.Split()[2..3])
sleep 3
 
# lets see what new volume we have!
sc $env:temp\$servername\$servername.x "select $disknr","detail disk" -encoding ascii
$volumenr = diskpart /s $env:temp\$servername\$servername.x 
switch -regex ($volumenr)
{
   "(?<Volume>Volume\s\d+)"   {$volumenr = $matches.Volume}
}
sleep 3
 
# lets now mount the volume!
sc $env:temp\$servername\$servername.x "select $disknr","select $volumenr","assign mount=e:\mount_path\$servername" -encoding ascii
diskpart /s $env:temp\$servername\$servername.x 
sleep 3
 
#Prepare file for delete snapshot script
sc $env:temp\$servername\$servername.x "select $disknr", "select $volumenr","remove" -encoding ascii
 
# now lets put information in temp file
sc $env:temp\$servername_output.x "IQN = $IQN","DISKNR = $Disknr","VOLUMENR = $volumenr" -encoding ascii) | out-file LogName -enc ASCII

Open in new window

0
 

Author Comment

by:Hans de Jongh
ID: 23671235
you again:)
thanks!!

ok well the output of transcript is:

**********************
Windows PowerShell Transcript Start
Start time: 20090218154402
Username  : domain\username
Machine        : backupserver (Microsoft Windows NT 5.2.3790 Service Pack 2)
**********************
Transcript started, output file is F:\File_backup_log\pre_servername.log
New-Item : Item with specified name E:\mount_path\servername already exists.
At line:38 char:24
+         $scriptCmd = {& <<<<  $wrappedCmd -Type Directory @PSBoundParameters
}
    + CategoryInfo          : ResourceExists: (E:\mount_path\servername:String)  
   [New-Item], IOException
    + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.New
   ItemCommand
 


    Directory: C:\WINDOWS\TEMP


Mode                LastWriteTime     Length Name                              
----                -------------     ------ ----                              
d----         18-2-2009     15:44            servername                          
**********************
Windows PowerShell Transcript End
End time: 20090218154501
**********************

but the output in the console is:

PS C:\Documents and Settings\username.domain> . 'C:\Documents and Settings\username.domain\Desktop\Scripts_Powershell\pre.ps1'
Transcript started. Output file is "F:\File_backup_log\pre_servername.log"
New-Item : Item with specified name E:\mount_path\servername already exists.
At line:38 char:24
+         $scriptCmd = {& <<<<  $wrappedCmd -Type Directory @PSBoundParameters }
    + CategoryInfo          : ResourceExists: (E:\mount_path\servername:String) [New-Item], IOException
    + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand



    Directory: C:\DOCUME~1\USERN~1.DOMAIN\LOCALS~1\Temp\2


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         18-2-2009     15:02            servername
Microsoft iSCSI Initiator version 2.0 Build 3497

The operation completed successfully.

PsExec v1.94 - Execute processes remotely
Copyright (C) 2001-2008 Mark Russinovich
Sysinternals - www.sysinternals.com



C:\Windows\system32>"C:\Program Files\EqualLogic\bin\ASMCLI.exe" ASMCLI -smart -email -objectid="Collection{COLLECTION}" -shadowtype=Transportable -backuptype=copy -snapshottype=Snapshot

EqualLogic VSS Administrator successfully created the smart copy.
c:\backup_scripts\make_snap.bat exited on servername with error code 0.
Microsoft iSCSI Initiator version 2.0 Build 3497

The operation completed successfully.
Microsoft iSCSI Initiator version 2.0 Build 3497

LoginTarget to iqn.2001-05.com.ASDFASDFgic:0-8a0906-54971ff02-9fAS32Q234DASF99c1-servername.BSADFASDF-2009-02-18-15:03:53.5449 on <no init instance> to <no portal>/0
Session Id is 0xffffffff89e254fc-0x4000013700000167
Connection Id is 0xffffffff89e254fc-0x2a8
The operation completed successfully.

Microsoft DiskPart version 5.2.3790.3959
Copyright (C) 1999-2001 Microsoft Corporation.
On computer: ITCBCK01

Disk 15 is now the selected disk.

Volume 4 is the selected volume.

DiskPart could not assign the drive letter or mount point.
Make sure the drive letter or mount point is valid.
Transcript stopped. Output file is "F:\File_backup_log\pre_servername.log"

I would prefer that console output...
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 23671622
Did my second options not work for you?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Hans de Jongh
ID: 23691586
nope doesnt work!..
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 23691662
Adding " | out-file LogName -append -enc ASCII" to each of line of code you want outputted didnt work?
0
 

Author Comment

by:Hans de Jongh
ID: 23692006
no that just gave alot of trash in the log
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 23692100
define trash :)
0
 
LVL 4

Expert Comment

by:peter_field
ID: 24247205
I would suggest not using Start-Transcript. Start-Transcript isn't designed for use in scripts, but rather for users to log their PowerShell session. I am assuming you will run this script as a scheduled task. I would redirect output in the scheduled task to the log file, i.e. as the command for the scheduled task do something like the code snippet below. This means your script doesn't have to be cluttered with Out-File statements, and you get all logging. If your running it interactively, then you can configure Start-Transcript in your profile rather than in the script.
powershell -command "& c:\scripts\script.ps1 -param" >> c:\logs\script.log 2>&1

Open in new window

0
 
LVL 4

Accepted Solution

by:
peter_field earned 2000 total points
ID: 24302944
Sorry, I actually found this works on Windows 2008 but not 2003. To make it work on both you need to do this:
c:\windows\system32\cmd.exe /c echo. | c:\windows\system32\windowspowershell\v1.0\powershell.exe -noninteractive -noprofile -command "& c:\scripts\script.ps1 -parameter" 2>&1 >> c:\logs\scriptlog.txt

Open in new window

0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question