MSG Command as a function? What's wrong?


i'm attempting accomplish particular action using invoke-command as function. here command works when entered directly powershell command line.

invoke-command -computername pc123 {msg /time:10000 * "message here"}



wanted create function in psprofile using:

function netmsg { 	param([parameter(mandatory=$true)][string[]] $computername) 	$readme = read-host -prompt("enter desired message") 	foreach($computer in $computername) 		{ 		$g = $readme 		invoke-command -computername $computer {msg /time:100000 * "$g"} 		} }
since i'm new ps , have yet understand concepts fully, struggling figure out how call $g remote machine accept input.

tried variation , able push remote machine no problem () characters were displayed in message. (because input of direct in quotation marks)

function netmsg { 	param([parameter(mandatory=$true)][string[]] $computername) 	$readme = read-host -prompt("enter desired message") 	foreach($computer in $computername) 		{ 		$g = $readme 		invoke-command -computername $computer {msg /time:100000 * "($g)"} 		} }


after little bit of research, believe because once command sent remote machine unable output $g because it's not same shell or machine. if case, can guide me how take care of function work properly?


hi jbear,

thanks post, when pass local variable invoke-command, may need use -arguementlist.

you check below link reference:

http://blogs.msdn.com/b/powershell/archive/2009/12/29/arguments-for-remote-commands.aspx

and code changed below:

function netmsg { 	param([parameter(mandatory=$true)][string[]] $computername) 	$readme = read-host -prompt("enter desired message") 	foreach($computer in $computername) 		{ 		$g = $readme 		invoke-command -computername $computer { param($g) msg /time:100000 * "($g)"} -argumentlist $g 		} }

i test on lab , works charm.

hope helps.

best regards,

elaine


please remember mark replies answers if , unmark them if provide no help. if have feedback technet subscriber support, contact tnmff@microsoft.com.



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Windows 2016 RDS event 1306 Connection Broker Client failed to redirect the user... Error: NULL