![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) | ||||||||||||
| 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
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) | ||||||||||||||||||||||||
| 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:
| ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
| | #5 (permalink) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 {
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
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
| 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 |