Solved

calling an external function on a powershell script

Posted on 2012-04-09
2
719 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 69

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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