Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Using Variables In Add-Content CmdLet

Reply
 
Old 01-04-2008   #1 (permalink)
spar1grep


 
 

Using Variables In Add-Content CmdLet

Please could somebody offer me some advice, when I am using the following
cmdlet in a PS script, the output window returns the error

Add-Content : Cannot find drive. A drive with name 'hello world' does not
exist.


The script I am using is as follows

$LogFileName = "C:\Logfile.TXT"
$LogFileLine = "hello world :"

Add-Content -Path $LogFileName -Value $LogFileLine


NB - I have tried numerous permutations of this such as Add-Content
-Path($LogFileName) -Value ($LogFileLine), all of which result in the same
error being displayed. I have looked at numerous help pages and all show
that I am using the cmdlet correctly, I think it has something to do with
using variables and no inverted commas?? This is my first PS script, as I am
now scripting in PS rather then VBS so that I am ready for Windows 2008
onwards!
--
spar1grep

My System SpecsSystem Spec
Old 01-04-2008   #2 (permalink)
Jon


 
 

Re: Using Variables In Add-Content CmdLet

"spar1grep" <spar1grep@xxxxxx> wrote in message
news:46499F78-6AAB-4971-89A8-AAD21AA9966C@xxxxxx
Quote:

> Please could somebody offer me some advice, when I am using the following
> cmdlet in a PS script, the output window returns the error
>
> Add-Content : Cannot find drive. A drive with name 'hello world' does not
> exist.
>
>
> The script I am using is as follows
>
> $LogFileName = "C:\Logfile.TXT"
> $LogFileLine = "hello world :"
>
> Add-Content -Path $LogFileName -Value $LogFileLine
>
>
> NB - I have tried numerous permutations of this such as Add-Content
> -Path($LogFileName) -Value ($LogFileLine), all of which result in the same
> error being displayed. I have looked at numerous help pages and all show
> that I am using the cmdlet correctly, I think it has something to do with
> using variables and no inverted commas?? This is my first PS script, as I
> am
> now scripting in PS rather then VBS so that I am ready for Windows 2008
> onwards!
> --
> spar1grep


Works ok here, but what may be happening is that you've inadvertently
reversed the order of the variables in the statement

ie make sure that it's

Add-Content -Path $LogFileName -Value $LogFileLine

and not

Add-Content -Path $LogFileLine -Value $LogFileName


If the order is mixed up, then it will try to interpret "Hello World:" as a
path, which would result in that error message.

--
Jon



My System SpecsSystem Spec
Old 01-04-2008   #3 (permalink)
spar1grep


 
 

Re: Using Variables In Add-Content CmdLet

Thanks Jon. I too thought I had made a inadvertent swap around of the
variables. This is the actual script code, I cannot see the variables being
mixed up:

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Function TestForLockFile($FileName) {

$FileToTest = $ZyLabDir + "\" + $FileName + ".~X~"
Return Test-Path $FileToTest

}

Function WriteToLogFile($LogFilePath, $LogFileLine) {

Write-Host $Line
Add-Content -Path($LogFilePath) -Value($LogFileLine)

}

#############################################
# Main Routine
#############################################

$ZyLabDir = "X:\ZyIMAGE\ZySCAN JobRoot"
$EmailAddressTo = "bill@xxxxxx"

$LogFilePath = Get-Content Env:SystemRoot
$LogFilePath = $LogFilePath + "System32\LogFiles"

If (Test-Path $LogFilePath) {
$LogFile = (Get-Content Env:SystemRoot) +
"\System32\LogFiles\JobRootHealth.LOG"
}
Else
{
$LogFile = (Get-Content Env:SystemRoot) + "\JobRootHealth.LOG"
}

If (Test-Path $LogFile) {
Clear-Content -Path $LogFile
}
Else
{

}

$Line = "JobRootHealth Executed On : " + (Get-Date) + "."
WriteToLogFile($LogFile,$Line)
--
spar1grep


"Jon" wrote:
Quote:

> "spar1grep" <spar1grep@xxxxxx> wrote in message
> news:46499F78-6AAB-4971-89A8-AAD21AA9966C@xxxxxx
Quote:

> > Please could somebody offer me some advice, when I am using the following
> > cmdlet in a PS script, the output window returns the error
> >
> > Add-Content : Cannot find drive. A drive with name 'hello world' does not
> > exist.
> >
> >
> > The script I am using is as follows
> >
> > $LogFileName = "C:\Logfile.TXT"
> > $LogFileLine = "hello world :"
> >
> > Add-Content -Path $LogFileName -Value $LogFileLine
> >
> >
> > NB - I have tried numerous permutations of this such as Add-Content
> > -Path($LogFileName) -Value ($LogFileLine), all of which result in the same
> > error being displayed. I have looked at numerous help pages and all show
> > that I am using the cmdlet correctly, I think it has something to do with
> > using variables and no inverted commas?? This is my first PS script, as I
> > am
> > now scripting in PS rather then VBS so that I am ready for Windows 2008
> > onwards!
> > --
> > spar1grep
>
>
>
> Works ok here, but what may be happening is that you've inadvertently
> reversed the order of the variables in the statement
>
> ie make sure that it's
>
> Add-Content -Path $LogFileName -Value $LogFileLine
>
> and not
>
> Add-Content -Path $LogFileLine -Value $LogFileName
>
>
> If the order is mixed up, then it will try to interpret "Hello World:" as a
> path, which would result in that error message.
>
> --
> Jon
>
>
>
>
My System SpecsSystem Spec
Old 01-04-2008   #4 (permalink)
Shay Levi


 
 

Re: Using Variables In Add-Content CmdLet


Few PowerShell shortcuts:

# get the system32 path
[environment]::SystemDirectory

#Get-Content Env:SystemRoot
$env:SystemRoot

If you want to test for a file exitence using test-path, make sure to specify
"-PathType leaf"
otherwise you can inadvertently test for a directory with the same name.
Check the help for test-path:

help test-path -full


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Thanks Jon. I too thought I had made a inadvertent swap around of the
> variables. This is the actual script code, I cannot see the variables
> being mixed up:
>
> # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> Function TestForLockFile($FileName) {
>
> $FileToTest = $ZyLabDir + "\" + $FileName + ".~X~"
> Return Test-Path $FileToTest
> }
>
> Function WriteToLogFile($LogFilePath, $LogFileLine) {
>
> Write-Host $Line
> Add-Content -Path($LogFilePath) -Value($LogFileLine)
> }
>
> #############################################
> # Main Routine
> #############################################
> $ZyLabDir = "X:\ZyIMAGE\ZySCAN JobRoot"
> $EmailAddressTo = "bill@xxxxxx"
> $LogFilePath = Get-Content Env:SystemRoot
> $LogFilePath = $LogFilePath + "System32\LogFiles"
> If (Test-Path $LogFilePath) {
> $LogFile = (Get-Content Env:SystemRoot) +
> "\System32\LogFiles\JobRootHealth.LOG"
> }
> Else
> {
> $LogFile = (Get-Content Env:SystemRoot) + "\JobRootHealth.LOG"
> }
> If (Test-Path $LogFile) {
> Clear-Content -Path $LogFile
> }
> Else
> {
> }
>
> $Line = "JobRootHealth Executed On : " + (Get-Date) + "."
> WriteToLogFile($LogFile,$Line)
> "Jon" wrote:
Quote:

>> "spar1grep" <spar1grep@xxxxxx> wrote in message
>> news:46499F78-6AAB-4971-89A8-AAD21AA9966C@xxxxxx
>>
Quote:

>>> Please could somebody offer me some advice, when I am using the
>>> following cmdlet in a PS script, the output window returns the error
>>>
>>> Add-Content : Cannot find drive. A drive with name 'hello world'
>>> does not exist.
>>>
>>> The script I am using is as follows
>>>
>>> $LogFileName = "C:\Logfile.TXT"
>>> $LogFileLine = "hello world :"
>>> Add-Content -Path $LogFileName -Value $LogFileLine
>>>
>>> NB - I have tried numerous permutations of this such as Add-Content
>>> -Path($LogFileName) -Value ($LogFileLine), all of which result in
>>> the same
>>> error being displayed. I have looked at numerous help pages and all
>>> show
>>> that I am using the cmdlet correctly, I think it has something to do
>>> with
>>> using variables and no inverted commas?? This is my first PS
>>> script, as I
>>> am
>>> now scripting in PS rather then VBS so that I am ready for Windows
>>> 2008
>>> onwards!
>>> --
>>> spar1grep
>> Works ok here, but what may be happening is that you've inadvertently
>> reversed the order of the variables in the statement
>>
>> ie make sure that it's
>>
>> Add-Content -Path $LogFileName -Value $LogFileLine
>>
>> and not
>>
>> Add-Content -Path $LogFileLine -Value $LogFileName
>>
>> If the order is mixed up, then it will try to interpret "Hello
>> World:" as a path, which would result in that error message.
>>
>> -- Jon
>>

My System SpecsSystem Spec
Old 01-04-2008   #5 (permalink)
Jon


 
 

Re: Using Variables In Add-Content CmdLet

Also change this line

WriteToLogFile($LogFile,$Line)

to

WriteToLogFile $LogFile $Line

A space tends to be the delimiter for passing arguments to functions

Otherwise you're passing one argument ie an array containing 2 members
%LogFile and $Line), which would I suspect is throwing out your function.

This small function illustrates the difference

function a($a,$b){$a.GetType()}
a 1 2
a(1,2)


--
Jon


"spar1grep" <spar1grep@xxxxxx> wrote in message
news:9E51519C-08D4-49EA-AEBF-A3575C3DB961@xxxxxx
Quote:

> Thanks Jon. I too thought I had made a inadvertent swap around of the
> variables. This is the actual script code, I cannot see the variables
> being
> mixed up:
>
> # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> Function TestForLockFile($FileName) {
>
> $FileToTest = $ZyLabDir + "\" + $FileName + ".~X~"
> Return Test-Path $FileToTest
>
> }
>
> Function WriteToLogFile($LogFilePath, $LogFileLine) {
>
> Write-Host $Line
> Add-Content -Path($LogFilePath) -Value($LogFileLine)
>
> }
>
> #############################################
> # Main Routine
> #############################################
>
> $ZyLabDir = "X:\ZyIMAGE\ZySCAN JobRoot"
> $EmailAddressTo = "bill@xxxxxx"
>
> $LogFilePath = Get-Content Env:SystemRoot
> $LogFilePath = $LogFilePath + "System32\LogFiles"
>
> If (Test-Path $LogFilePath) {
> $LogFile = (Get-Content Env:SystemRoot) +
> "\System32\LogFiles\JobRootHealth.LOG"
> }
> Else
> {
> $LogFile = (Get-Content Env:SystemRoot) + "\JobRootHealth.LOG"
> }
>
> If (Test-Path $LogFile) {
> Clear-Content -Path $LogFile
> }
> Else
> {
>
> }
>
> $Line = "JobRootHealth Executed On : " + (Get-Date) + "."
> WriteToLogFile($LogFile,$Line)
> --
> spar1grep
>
>
> "Jon" wrote:
>
Quote:

>> "spar1grep" <spar1grep@xxxxxx> wrote in message
>> news:46499F78-6AAB-4971-89A8-AAD21AA9966C@xxxxxx
Quote:

>> > Please could somebody offer me some advice, when I am using the
>> > following
>> > cmdlet in a PS script, the output window returns the error
>> >
>> > Add-Content : Cannot find drive. A drive with name 'hello world' does
>> > not
>> > exist.
>> >
>> >
>> > The script I am using is as follows
>> >
>> > $LogFileName = "C:\Logfile.TXT"
>> > $LogFileLine = "hello world :"
>> >
>> > Add-Content -Path $LogFileName -Value $LogFileLine
>> >
>> >
>> > NB - I have tried numerous permutations of this such as Add-Content
>> > -Path($LogFileName) -Value ($LogFileLine), all of which result in the
>> > same
>> > error being displayed. I have looked at numerous help pages and all
>> > show
>> > that I am using the cmdlet correctly, I think it has something to do
>> > with
>> > using variables and no inverted commas?? This is my first PS script,
>> > as I
>> > am
>> > now scripting in PS rather then VBS so that I am ready for Windows 2008
>> > onwards!
>> > --
>> > spar1grep
>>
>>
>>
>> Works ok here, but what may be happening is that you've inadvertently
>> reversed the order of the variables in the statement
>>
>> ie make sure that it's
>>
>> Add-Content -Path $LogFileName -Value $LogFileLine
>>
>> and not
>>
>> Add-Content -Path $LogFileLine -Value $LogFileName
>>
>>
>> If the order is mixed up, then it will try to interpret "Hello World:" as
>> a
>> path, which would result in that error message.
>>
>> --
>> Jon
>>
>>
>>
>>
My System SpecsSystem Spec
Old 01-04-2008   #6 (permalink)
spar1grep


 
 

Re: Using Variables In Add-Content CmdLet

Shay, thanks for the useful tip, now added to script.
--
spar1grep


"Shay Levi" wrote:
Quote:

>
> Few PowerShell shortcuts:
>
> # get the system32 path
> [environment]::SystemDirectory
>
> #Get-Content Env:SystemRoot
> $env:SystemRoot
>
> If you want to test for a file exitence using test-path, make sure to specify
> "-PathType leaf"
> otherwise you can inadvertently test for a directory with the same name.
> Check the help for test-path:
>
> help test-path -full
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

> > Thanks Jon. I too thought I had made a inadvertent swap around of the
> > variables. This is the actual script code, I cannot see the variables
> > being mixed up:
> >
> > # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> >
> > Function TestForLockFile($FileName) {
> >
> > $FileToTest = $ZyLabDir + "\" + $FileName + ".~X~"
> > Return Test-Path $FileToTest
> > }
> >
> > Function WriteToLogFile($LogFilePath, $LogFileLine) {
> >
> > Write-Host $Line
> > Add-Content -Path($LogFilePath) -Value($LogFileLine)
> > }
> >
> > #############################################
> > # Main Routine
> > #############################################
> > $ZyLabDir = "X:\ZyIMAGE\ZySCAN JobRoot"
> > $EmailAddressTo = "bill@xxxxxx"
> > $LogFilePath = Get-Content Env:SystemRoot
> > $LogFilePath = $LogFilePath + "System32\LogFiles"
> > If (Test-Path $LogFilePath) {
> > $LogFile = (Get-Content Env:SystemRoot) +
> > "\System32\LogFiles\JobRootHealth.LOG"
> > }
> > Else
> > {
> > $LogFile = (Get-Content Env:SystemRoot) + "\JobRootHealth.LOG"
> > }
> > If (Test-Path $LogFile) {
> > Clear-Content -Path $LogFile
> > }
> > Else
> > {
> > }
> >
> > $Line = "JobRootHealth Executed On : " + (Get-Date) + "."
> > WriteToLogFile($LogFile,$Line)
> > "Jon" wrote:
Quote:

> >> "spar1grep" <spar1grep@xxxxxx> wrote in message
> >> news:46499F78-6AAB-4971-89A8-AAD21AA9966C@xxxxxx
> >>
> >>> Please could somebody offer me some advice, when I am using the
> >>> following cmdlet in a PS script, the output window returns the error
> >>>
> >>> Add-Content : Cannot find drive. A drive with name 'hello world'
> >>> does not exist.
> >>>
> >>> The script I am using is as follows
> >>>
> >>> $LogFileName = "C:\Logfile.TXT"
> >>> $LogFileLine = "hello world :"
> >>> Add-Content -Path $LogFileName -Value $LogFileLine
> >>>
> >>> NB - I have tried numerous permutations of this such as Add-Content
> >>> -Path($LogFileName) -Value ($LogFileLine), all of which result in
> >>> the same
> >>> error being displayed. I have looked at numerous help pages and all
> >>> show
> >>> that I am using the cmdlet correctly, I think it has something to do
> >>> with
> >>> using variables and no inverted commas?? This is my first PS
> >>> script, as I
> >>> am
> >>> now scripting in PS rather then VBS so that I am ready for Windows
> >>> 2008
> >>> onwards!
> >>> --
> >>> spar1grep
> >> Works ok here, but what may be happening is that you've inadvertently
> >> reversed the order of the variables in the statement
> >>
> >> ie make sure that it's
> >>
> >> Add-Content -Path $LogFileName -Value $LogFileLine
> >>
> >> and not
> >>
> >> Add-Content -Path $LogFileLine -Value $LogFileName
> >>
> >> If the order is mixed up, then it will try to interpret "Hello
> >> World:" as a path, which would result in that error message.
> >>
> >> -- Jon
> >>
>
>
>
My System SpecsSystem Spec
Old 01-04-2008   #7 (permalink)
spar1grep


 
 

Re: Using Variables In Add-Content CmdLet

Jon,

Thanks for the info. Your information about the space delimiter helped
resolve the issue.

I removed the brackets from the WriteToLogFile commands in the Main Routine,
but the error still occurred. Then it got me to thinking about the actual
format of the functions being called. Turns out in PS (as in VBS) functions
can use variables declared in the main subroutine. I believe I tried this
when first coding the script and it did not work, must have been something
else. Thus in the end I removed the variable names in the function arguments
and used the same variable names that I used in the main subroutine,
everything now works as it should. Revised code as follows:

Function TestForLockFile([string]$FileName {

$FileToTest = $ZyLabDir + "\" + $FileName + ".~X~"
Return Test-Path $FileToTest

}

Function WriteToLogFile {

Write-Host $Line
Add-Content -Path $LogFile -Value $Line

}

#########################################
# Main Routine
#########################################

$ZyLabDir = "X:\ZyIMAGE\ZySCAN JobRoot"
$EmailAddressTo = "mrsearch@xxxxxx"

$LogFilePath = Get-Content Env:SystemRoot
$LogFilePath = $LogFilePath + "\System32\LogFiles"

If (Test-Path $LogFilePath -PathType leaf) {
$LogFile = (Get-Content Env:SystemRoot) +
"\System32\LogFiles\JobRootHealth.LOG"
}
Else
{
$LogFile = (Get-Content Env:SystemRoot) + "\JobRootHealth.LOG"
}

If (Test-Path $LogFile) {
Clear-Content -Path $LogFile
}
Else
{

}

$Line = "JobRootHealth Executed On : " + (Get-Date) + "."
WriteToLogFile

--
spar1grep


"Jon" wrote:
Quote:

> Also change this line
>
> WriteToLogFile($LogFile,$Line)
>
> to
>
> WriteToLogFile $LogFile $Line
>
> A space tends to be the delimiter for passing arguments to functions
>
> Otherwise you're passing one argument ie an array containing 2 members
> %LogFile and $Line), which would I suspect is throwing out your function.
>
> This small function illustrates the difference
>
> function a($a,$b){$a.GetType()}
> a 1 2
> a(1,2)
>
>
> --
> Jon
>
>
> "spar1grep" <spar1grep@xxxxxx> wrote in message
> news:9E51519C-08D4-49EA-AEBF-A3575C3DB961@xxxxxx
Quote:

> > Thanks Jon. I too thought I had made a inadvertent swap around of the
> > variables. This is the actual script code, I cannot see the variables
> > being
> > mixed up:
> >
> > # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> >
> > Function TestForLockFile($FileName) {
> >
> > $FileToTest = $ZyLabDir + "\" + $FileName + ".~X~"
> > Return Test-Path $FileToTest
> >
> > }
> >
> > Function WriteToLogFile($LogFilePath, $LogFileLine) {
> >
> > Write-Host $Line
> > Add-Content -Path($LogFilePath) -Value($LogFileLine)
> >
> > }
> >
> > #############################################
> > # Main Routine
> > #############################################
> >
> > $ZyLabDir = "X:\ZyIMAGE\ZySCAN JobRoot"
> > $EmailAddressTo = "bill@xxxxxx"
> >
> > $LogFilePath = Get-Content Env:SystemRoot
> > $LogFilePath = $LogFilePath + "System32\LogFiles"
> >
> > If (Test-Path $LogFilePath) {
> > $LogFile = (Get-Content Env:SystemRoot) +
> > "\System32\LogFiles\JobRootHealth.LOG"
> > }
> > Else
> > {
> > $LogFile = (Get-Content Env:SystemRoot) + "\JobRootHealth.LOG"
> > }
> >
> > If (Test-Path $LogFile) {
> > Clear-Content -Path $LogFile
> > }
> > Else
> > {
> >
> > }
> >
> > $Line = "JobRootHealth Executed On : " + (Get-Date) + "."
> > WriteToLogFile($LogFile,$Line)
> > --
> > spar1grep
> >
> >
> > "Jon" wrote:
> >
Quote:

> >> "spar1grep" <spar1grep@xxxxxx> wrote in message
> >> news:46499F78-6AAB-4971-89A8-AAD21AA9966C@xxxxxx
> >> > Please could somebody offer me some advice, when I am using the
> >> > following
> >> > cmdlet in a PS script, the output window returns the error
> >> >
> >> > Add-Content : Cannot find drive. A drive with name 'hello world' does
> >> > not
> >> > exist.
> >> >
> >> >
> >> > The script I am using is as follows
> >> >
> >> > $LogFileName = "C:\Logfile.TXT"
> >> > $LogFileLine = "hello world :"
> >> >
> >> > Add-Content -Path $LogFileName -Value $LogFileLine
> >> >
> >> >
> >> > NB - I have tried numerous permutations of this such as Add-Content
> >> > -Path($LogFileName) -Value ($LogFileLine), all of which result in the
> >> > same
> >> > error being displayed. I have looked at numerous help pages and all
> >> > show
> >> > that I am using the cmdlet correctly, I think it has something to do
> >> > with
> >> > using variables and no inverted commas?? This is my first PS script,
> >> > as I
> >> > am
> >> > now scripting in PS rather then VBS so that I am ready for Windows 2008
> >> > onwards!
> >> > --
> >> > spar1grep
> >>
> >>
> >>
> >> Works ok here, but what may be happening is that you've inadvertently
> >> reversed the order of the variables in the statement
> >>
> >> ie make sure that it's
> >>
> >> Add-Content -Path $LogFileName -Value $LogFileLine
> >>
> >> and not
> >>
> >> Add-Content -Path $LogFileLine -Value $LogFileName
> >>
> >>
> >> If the order is mixed up, then it will try to interpret "Hello World:" as
> >> a
> >> path, which would result in that error message.
> >>
> >> --
> >> Jon
> >>
> >>
> >>
> >>
>
>
My System SpecsSystem Spec
Old 01-04-2008   #8 (permalink)
RichS


 
 

Re: Using Variables In Add-Content CmdLet

Glad we could help resolve the problem
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"spar1grep" wrote:
Quote:

> Jon,
>
> Thanks for the info. Your information about the space delimiter helped
> resolve the issue.
>
> I removed the brackets from the WriteToLogFile commands in the Main Routine,
> but the error still occurred. Then it got me to thinking about the actual
> format of the functions being called. Turns out in PS (as in VBS) functions
> can use variables declared in the main subroutine. I believe I tried this
> when first coding the script and it did not work, must have been something
> else. Thus in the end I removed the variable names in the function arguments
> and used the same variable names that I used in the main subroutine,
> everything now works as it should. Revised code as follows:
>
> Function TestForLockFile([string]$FileName {
>
> $FileToTest = $ZyLabDir + "\" + $FileName + ".~X~"
> Return Test-Path $FileToTest
>
> }
>
> Function WriteToLogFile {
>
> Write-Host $Line
> Add-Content -Path $LogFile -Value $Line
>
> }
>
> #########################################
> # Main Routine
> #########################################
>
> $ZyLabDir = "X:\ZyIMAGE\ZySCAN JobRoot"
> $EmailAddressTo = "mrsearch@xxxxxx"
>
> $LogFilePath = Get-Content Env:SystemRoot
> $LogFilePath = $LogFilePath + "\System32\LogFiles"
>
> If (Test-Path $LogFilePath -PathType leaf) {
> $LogFile = (Get-Content Env:SystemRoot) +
> "\System32\LogFiles\JobRootHealth.LOG"
> }
> Else
> {
> $LogFile = (Get-Content Env:SystemRoot) + "\JobRootHealth.LOG"
> }
>
> If (Test-Path $LogFile) {
> Clear-Content -Path $LogFile
> }
> Else
> {
>
> }
>
> $Line = "JobRootHealth Executed On : " + (Get-Date) + "."
> WriteToLogFile
>
> --
> spar1grep
>
>
> "Jon" wrote:
>
Quote:

> > Also change this line
> >
> > WriteToLogFile($LogFile,$Line)
> >
> > to
> >
> > WriteToLogFile $LogFile $Line
> >
> > A space tends to be the delimiter for passing arguments to functions
> >
> > Otherwise you're passing one argument ie an array containing 2 members
> > %LogFile and $Line), which would I suspect is throwing out your function.
> >
> > This small function illustrates the difference
> >
> > function a($a,$b){$a.GetType()}
> > a 1 2
> > a(1,2)
> >
> >
> > --
> > Jon
> >
> >
> > "spar1grep" <spar1grep@xxxxxx> wrote in message
> > news:9E51519C-08D4-49EA-AEBF-A3575C3DB961@xxxxxx
Quote:

> > > Thanks Jon. I too thought I had made a inadvertent swap around of the
> > > variables. This is the actual script code, I cannot see the variables
> > > being
> > > mixed up:
> > >
> > > # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> > >
> > > Function TestForLockFile($FileName) {
> > >
> > > $FileToTest = $ZyLabDir + "\" + $FileName + ".~X~"
> > > Return Test-Path $FileToTest
> > >
> > > }
> > >
> > > Function WriteToLogFile($LogFilePath, $LogFileLine) {
> > >
> > > Write-Host $Line
> > > Add-Content -Path($LogFilePath) -Value($LogFileLine)
> > >
> > > }
> > >
> > > #############################################
> > > # Main Routine
> > > #############################################
> > >
> > > $ZyLabDir = "X:\ZyIMAGE\ZySCAN JobRoot"
> > > $EmailAddressTo = "bill@xxxxxx"
> > >
> > > $LogFilePath = Get-Content Env:SystemRoot
> > > $LogFilePath = $LogFilePath + "System32\LogFiles"
> > >
> > > If (Test-Path $LogFilePath) {
> > > $LogFile = (Get-Content Env:SystemRoot) +
> > > "\System32\LogFiles\JobRootHealth.LOG"
> > > }
> > > Else
> > > {
> > > $LogFile = (Get-Content Env:SystemRoot) + "\JobRootHealth.LOG"
> > > }
> > >
> > > If (Test-Path $LogFile) {
> > > Clear-Content -Path $LogFile
> > > }
> > > Else
> > > {
> > >
> > > }
> > >
> > > $Line = "JobRootHealth Executed On : " + (Get-Date) + "."
> > > WriteToLogFile($LogFile,$Line)
> > > --
> > > spar1grep
> > >
> > >
> > > "Jon" wrote:
> > >
> > >> "spar1grep" <spar1grep@xxxxxx> wrote in message
> > >> news:46499F78-6AAB-4971-89A8-AAD21AA9966C@xxxxxx
> > >> > Please could somebody offer me some advice, when I am using the
> > >> > following
> > >> > cmdlet in a PS script, the output window returns the error
> > >> >
> > >> > Add-Content : Cannot find drive. A drive with name 'hello world' does
> > >> > not
> > >> > exist.
> > >> >
> > >> >
> > >> > The script I am using is as follows
> > >> >
> > >> > $LogFileName = "C:\Logfile.TXT"
> > >> > $LogFileLine = "hello world :"
> > >> >
> > >> > Add-Content -Path $LogFileName -Value $LogFileLine
> > >> >
> > >> >
> > >> > NB - I have tried numerous permutations of this such as Add-Content
> > >> > -Path($LogFileName) -Value ($LogFileLine), all of which result in the
> > >> > same
> > >> > error being displayed. I have looked at numerous help pages and all
> > >> > show
> > >> > that I am using the cmdlet correctly, I think it has something to do
> > >> > with
> > >> > using variables and no inverted commas?? This is my first PS script,
> > >> > as I
> > >> > am
> > >> > now scripting in PS rather then VBS so that I am ready for Windows 2008
> > >> > onwards!
> > >> > --
> > >> > spar1grep
> > >>
> > >>
> > >>
> > >> Works ok here, but what may be happening is that you've inadvertently
> > >> reversed the order of the variables in the statement
> > >>
> > >> ie make sure that it's
> > >>
> > >> Add-Content -Path $LogFileName -Value $LogFileLine
> > >>
> > >> and not
> > >>
> > >> Add-Content -Path $LogFileLine -Value $LogFileName
> > >>
> > >>
> > >> If the order is mixed up, then it will try to interpret "Hello World:" as
> > >> a
> > >> path, which would result in that error message.
> > >>
> > >> --
> > >> Jon
> > >>
> > >>
> > >>
> > >>
> >
> >
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Clear-Content Cmdlet and Processing Lines in Text File PowerShell
get-content csv with two variables PowerShell
Handy edit-content cmdlet PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46