Author Posts

October 7, 2015 at 12:23 am

Greetings! I cann't understand why next code doens't work:
import-module activedirectory

$message = Read-Host "Type your message"
icm -ComputerName (Get-ADComputer -filter "Name -like 'w*' -or Name -like 'n*'" -searchbase "OU=422,OU=Depy1,OU=HQ,DC=contoso,DC=com").Name -ScriptBlock {msg * "$($message)"}

And ISE says, that's a message from STDIN is empty. I cann't understand why. I have tried "$($message)", $($message) and even just $message and nothing. Please help.

October 7, 2015 at 1:23 am

HI,

 -ScriptBlock {msg * "$($message)"}  

$message his unkown because you are in a script block.

Check : https://technet.microsoft.com/en-us/library/hh847893.aspx

ex :

C:\PS> invoke-command -scriptblock {param ($uu = "Parameter"); 
              "$uu assigned."} -args "Other value"

As always this is "A" way not "The" way of doing it

October 7, 2015 at 1:46 am

If you're using PowerShell v3.0 or later, you can just put $using:message in your script block, which is more convenient. For PSv2 compatibility, you have to use the -ArgumentList parameter of Invoke-Command, as Cyril mentioned.

October 7, 2015 at 2:47 am

Thanks, guys! Your both answers are helped me.