locked
Logging Script information into a .txt file RRS feed

  • Question

  • I'm new to Powershell and working on writing a script that will create a config file for our current DB2 servers, move it to a different file location (A file that will be created if it's not there), then perform a brute force (silent) uninstall. For the brute force uninstall, there is a log file created with IBM's db2unins that I can use (Maybe just put it in the same folder? unless somebody knows how to combine the logs), but I want the whole script to be logged.  That is my main issue, I just want everything in a txt file somewhere, especially if it's a silent uninstall. Below is my powershell script:

            # Specify the path

    $OriginalPathfile = "C:\DB2Log.txt"

    $destDir = "C:\DB2_Config_backup"



    # Check if the folder exist if not create it 

    If (!(Test-Path $destDir)) {

       New-Item -Path $destDir -ItemType Directory 

    }

    else {

       Write-Host "Directory already exists!" 

    }



    #Will create a new configuration file and will save it as "DB2_backup_config.txt" Each time it is ran, it will replace the prior version. 

    db2cfexp.exe 'C:\Program Files\IBM\SQLLIB\BIN\DB2_backup_config.txt' 

    #Moving the config backup file to the folder that was created earlier. 
    Copy-Item 'C:\Program Files\IBM\SQLLIB\BIN\DB2_backup_config.txt' -Destination C:\DB2_Config_backup 

    #Will run brute force uninstall    -Y causes no prompts to appear
    db2unins -y

    ]

    Also, if anybody knows how I should turn this into an executable or run with a scheduling software, I would appreciate it. 

    Thanks in advance for any assistance. 

     
    Thursday, March 3, 2016 3:33 PM

Answers

  • Hey Cookm3

    Heres a bit of constructive non judgemental advice for you.

    When I teach PowerShell I recommend all my delegates use the PowerShell in a Month of Lunches book by Don Jones.

    Its a great book and explains everything as you go along.

    What you want can be done and I am sure you know what you are asking. From the description your formatting command construction may be wrong and hence you are not getting what you want in the file.

    Persevere it will be worth it in the end.

    We are all new at everything once!

    As an Example you can fit everything I know about DB2 on a pin head!

    Yours

    Ed

    If this was helpful please mark it as an answer


    Thursday, March 3, 2016 4:23 PM

All replies

  • You mare asking for a complete design.  This is ma forum that answers specific questions about scripting.  You need to ask a question.

    For information on how o use the IBM utilities and installer you will have to post in the IBM forum.

    To output to a file with PowerShell you can use Out-File:


    \_(ツ)_/

    Thursday, March 3, 2016 3:40 PM
  • I have tried to use out-file and I named the variable as a location. I then pipe | out-file $variable, after every line. This results in a blank .TXT document. 
    Thursday, March 3, 2016 3:45 PM
  • I have tried to use out-file and I named the variable as a location. I then pipe | out-file $variable, after every line. This results in a blank .TXT document. 

    No idea what that means:

    'hello world' | Out-File myfile.txt -Append

    Try reading the help before trying to ask a question.  It will save you a lot of time.


    \_(ツ)_/

    Thursday, March 3, 2016 3:49 PM
  • I have read the help, there is nothing telling me how to show my script and it's output in a .txt file. If I use your example it works, but if I put the | Out-File myfile.txt -Append after any of my lines it does not. It will create the txt. file, but it will be empty. EX. Write-Host "Directory already exists!" | Out-File myfile.txt -Append. I have searched all of the help sites and other forums, the closest thing I could find was the start-transcript, but that doesn't work in the ISE. All I need is everything encompassed in a txt file.  
    Thursday, March 3, 2016 4:00 PM
  • It is impossible to understand what you are saying because you do not have enough knowledge of PowerShell.  Your statement is ambiguous.  You cannot just add Out-File after a statement.  It can take a string or object as input or it can take the output of a pipeline as input.  It cannot be place after a command without understanding how PowerShell works.

    Please review the basics of how to use PowerShell. It will clarify why this won't work and why your questions cannot be understood as asked.

    https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx?f=255&MSPPError=-2147217396


    \_(ツ)_/

    Thursday, March 3, 2016 4:07 PM
  • Hey Cookm3

    Heres a bit of constructive non judgemental advice for you.

    When I teach PowerShell I recommend all my delegates use the PowerShell in a Month of Lunches book by Don Jones.

    Its a great book and explains everything as you go along.

    What you want can be done and I am sure you know what you are asking. From the description your formatting command construction may be wrong and hence you are not getting what you want in the file.

    Persevere it will be worth it in the end.

    We are all new at everything once!

    As an Example you can fit everything I know about DB2 on a pin head!

    Yours

    Ed

    If this was helpful please mark it as an answer


    Thursday, March 3, 2016 4:23 PM
  • Yes. " Month of Lunches" is an excellent starter book and it will, within an month, get you started with PowerShell.

    FOr what you are trying to do know the basic examples in the link I posted will also get you started in an afternoon. Once you see how the pipeline works and understand the command environment you will see why your questions are hard to answer.  Yu will also be able to decide how to get the output you want.  The answer can only be decide on a per-command basis.

    Learning PowerShell has now become critical for al who work in Windows technologies. Good luck in your research.


    \_(ツ)_/

    Thursday, March 3, 2016 4:29 PM