We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Powershell output to logfile

Medium Priority
10,101 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

Comment
Watch Question

Commented:
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

Author

Commented:
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...

Commented:
Did my second options not work for you?

Author

Commented:
nope doesnt work!..

Commented:
Adding " | out-file LogName -append -enc ASCII" to each of line of code you want outputted didnt work?

Author

Commented:
no that just gave alot of trash in the log

Commented:
define trash :)
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

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.