Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

calling an external function on a powershell script

Posted on 2012-04-09
2
Medium Priority
?
754 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 71

Accepted Solution

by:
Qlemo earned 1000 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 25

Assisted Solution

by:DBAduck - Ben Miller
DBAduck - Ben Miller earned 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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ā€¦
Screencast - Getting to Know the Pipeline

886 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