Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

get-content and passing arguements to a function?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 05-08-2008   #1 (permalink)
akcorr
Guest


 

get-content and passing arguements to a function?

Hi guys,

Is there a way to read .txt file formated like this:

Server-A C:

and pass "server-a" as $args[0] and pass "C:" as $args[1] to a function?

Thanks in advance!

My System SpecsSystem Spec
Old 05-08-2008   #2 (permalink)
Shay Levi
Guest


 

Re: get-content and passing arguements to a function?



If the arguments are on the first line of the file then you can split it
and pass the returned array items to a function:

$a = (get-content d:\scripts\temp\file.txt -totalCount 1).split()

callToYourFunction $a[0] $a[1]




---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Hi guys,
>
> Is there a way to read .txt file formated like this:
>
> Server-A C:
>
> and pass "server-a" as $args[0] and pass "C:" as $args[1] to a
> function?
>
> Thanks in advance!
>

My System SpecsSystem Spec
Old 05-10-2008   #3 (permalink)
Kiron
Guest


 

Re: get-content and passing arguements to a function?

# create a temp file with sample data
sc .\tmp.txt ("`tServer-A N:","`tServer-B P:","`tServer-S R:",
"`tServer-X T:","`tServer-Z Y:")

# a simple function to test against
function viewArgs {
write-host `$args`[0`] = $args[0] `$args`[1`] = $args[1]
}

# get sample data, trim and split each line, assign split values to
# $a1 and $a2 then pass them to the function
gc .\tmp.txt | % {$a1, $a2 = $_.trim().split(); viewArgs $a1 $a2}

# delete temp file
ri .\tmp.txt

--
Kiron
My System SpecsSystem Spec
Old 05-30-2008   #4 (permalink)
akcorr
Guest


 

Re: get-content and passing arguements to a function?

I can see the arguments once pass to my function but I try to use them in a
gmi query I get nothing. What am I doing wrong?

$a = (get-content C:\PowerShellScripts\diskservers.txt -totalCount 1).split()

function GetDisks
{
get-WmiObject -class Win32_LogicalDisk -computername $args[0], -filter
"DriveType=3" | ?{$_.DeviceID -eq $args[1]}
}

ForEach ($server in $a)
{
GetDisks $a[0] $a[1]
}


"Shay Levi" wrote:
Quote:

>
>
> If the arguments are on the first line of the file then you can split it
> and pass the returned array items to a function:
>
> $a = (get-content d:\scripts\temp\file.txt -totalCount 1).split()
>
> callToYourFunction $a[0] $a[1]
>
>
>
>
> ---
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
Quote:

> > Hi guys,
> >
> > Is there a way to read .txt file formated like this:
> >
> > Server-A C:
> >
> > and pass "server-a" as $args[0] and pass "C:" as $args[1] to a
> > function?
> >
> > Thanks in advance!
> >
>
>
>
My System SpecsSystem Spec
Old 05-30-2008   #5 (permalink)
Tao Ma
Guest


 

Re: get-content and passing arguements to a function?

Hi akcorr,

I write a simple function for you to test your code. Call Show-Args instead
of 'GetDrives':
PS C:\> function Show-Args {
Quote:
Quote:

>> foreach ($arg in $args) {
>> $c++;
>> Write-Host "$c : $arg";
>> }
>> }
>>
PS C:\> Show-Args 1 2 3
1 : 1
2 : 2
3 : 3

It will display the parameter(s) passed on the command line.

The split method will not work properly, if the spaces between the hostname
and drive are greater than 1.
$a = (get-content -totalCount 1).split()

You can try the following code which is using regular expression to split
the string.
foreach($h in (Get-Content .\name.txt)) {
$tmp = [regex]::split($h, '\s+');
Show-Args $tmp[0] $tmp[1]
}

the content of my 'name.txt'(2 spaces separate the servername & drivename):
Host1 C:
Host2 D:

Tao Ma

"akcorr" <akcorr@xxxxxx> дÈëÏûÏ¢ÐÂÎÅ:06769D14-2BF0-4F07-980C-4B58336071DA@xxxxxx
Quote:

>I can see the arguments once pass to my function but I try to use them in a
> gmi query I get nothing. What am I doing wrong?
>
> $a = (get-content C:\PowerShellScripts\diskservers.txt -totalCount
> 1).split()
>
> function GetDisks
> {
> get-WmiObject -class Win32_LogicalDisk -computername $args[0], -filter
> "DriveType=3" | ?{$_.DeviceID -eq $args[1]}
> }
>
> ForEach ($server in $a)
> {
> GetDisks $a[0] $a[1]
> }
>
>
> "Shay Levi" wrote:
>
Quote:

>>
>>
>> If the arguments are on the first line of the file then you can split it
>> and pass the returned array items to a function:
>>
>> $a = (get-content d:\scripts\temp\file.txt -totalCount 1).split()
>>
>> callToYourFunction $a[0] $a[1]
>>
>>
>>
>>
>> ---
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>>
Quote:

>> > Hi guys,
>> >
>> > Is there a way to read .txt file formated like this:
>> >
>> > Server-A C:
>> >
>> > and pass "server-a" as $args[0] and pass "C:" as $args[1] to a
>> > function?
>> >
>> > Thanks in advance!
>> >
>>
>>
>>

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing ArrayList as reference to function RD PowerShell 2 01-09-2008 01:33 PM
Passing function params by ref? Duncan Smith PowerShell 1 01-04-2008 06:15 AM
passing a combobox object as function argument Bart PowerShell 2 06-15-2007 12:26 AM
Function Parameter passing wrong or am I ? joergH PowerShell 4 01-23-2007 05:16 PM
Passing arguments to a function Marco Shaw PowerShell 1 12-11-2006 09:03 AM


Vistax64.com 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 2005-2008

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 47 48 49 50 51