Easy one - passing values to a function


i'm trying write function executes series of commands on machine. want function accept list of server names can execute against each server. i'm having issues..

i have text file c:\serverlist.txt contains entries follows

serverxxx

serveryyy

(and on...)


the script starts off reading text file , storing in variable

$servers = (get-content -path "c:\serverlist.txt)

next function follows:

function blah ([string[]]$arg)

{

out-file "c:\$arg"

}

i call function execute passing server list

blah($servers)

my output 1 text file called:

serverxxx serveryyy.txt

this isn't want, want create unique text file every server defined in serverlist.txt file

i'm sure related how passing data...help!!



your function accepting string array parameter, you'd need iterate through array (or pipe it) or pipe server list function accepts pipeline input.

example:

$servers = @(get-content -path "c:\temp\temp.txt") function blahpipeline { 	param 	([parameter(valuefrompipeline=$true)][string]$arg) 	process 	{ 		out-file "c:\temp\pipeline$arg" 	} } function blah ([string[]]$arg) { 	$arg | %{ out-file "c:\temp\$_"} } $servers | blahpipeline blah $servers



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

WIMMount (HSM) causing cluster storage to go redirected (2012r2 DC)

Failed to delete the test record dcdiag-test-record in zone test.com

Azure MFA with Azure AD and RDS