Message from STDIN is empty. Why?

This topic contains 3 replies, has 3 voices, and was last updated by  Dmitry P 2 years, 7 months ago.

  • Author
  • #30474

    Dmitry P

    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.

  • #30478

    Cyril Muffat


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

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

    Check :

    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

  • #30479

    Dave Wyatt

    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.

  • #30485

    Dmitry P

    Thanks, guys! Your both answers are helped me.

You must be logged in to reply to this topic.