Solved

calling an external function on a powershell script

Posted on 2012-04-09
2
686 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

17 Experts available now in Live!

Get 1:1 Help Now