Message from STDIN is empty. Why?

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Dmitry P Dmitry P 1 year, 9 months ago.

  • Author
    Posts
  • #30474
    Profile photo of Dmitry P
    Dmitry P
    Participant

    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
    Profile photo of Cyril Muffat
    Cyril Muffat
    Participant

    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

  • #30479
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    Profile photo of Dmitry P
    Dmitry P
    Participant

    Thanks, guys! Your both answers are helped me.

You must be logged in to reply to this topic.