Solved

calling an external function on a powershell script

Posted on 2012-04-09
2
692 Views
Last Modified: 2012-04-11
Hello,

I have create this function on a ps script fc.ps1 :
function Write-EventLog
{
param([string]$msg = "Default Message", [string]$type="Information")
$log ="D:\Logs\test.log"
$time_beg=(get-date -uformat "%Y%m%d")+" "+(get-date -uformat "%H:%M:%S")
$user1=whoami
$buf=$time_beg + " par "+$user1 + ": Debut d'action => " + $msg
echo $buf |out-file $log -encoding "default" -append
}

When I try to execute this function on mains.ps1 with the command . d:\fc.ps1, no error returns but the output file isn't generated.

How can I resolve this problem?

Thanks

bibi
0
Comment
Question by:bibi92
2 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 250 total points
ID: 37825578
You are defining a function in fc.ps1, and just calling the ps1 file will hence only define, not execute that function. Either call the ps1 file once at the beginning of your main.ps1, and then use the function Write-EventLog as defined where needed, or remove the first line (containing the function keyword), and call the fc.ps1 each time you need the result.

By the way, a better call of get-date is:
 $time_beg=get-date -format "yyyyMMdd HH:mm:ss"
0
 
LVL 24

Assisted Solution

by:DBAduck - Ben Miller
DBAduck - Ben Miller earned 250 total points
ID: 37835642
Inside any script that you want to use in your script, you can dot source the file and then call the function.

Inside fc2.ps1

. pathto\fc.ps1

code

Write-EventLog something something

Dot sourcing is kind of like including the file inside another which will activate the function for that script.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now