0

I have a powershell script/gui that i want to use to broadcast a message to all ActiveDirectory computers.The message does get broadcasted to all workstations but I want to get the computer name(as output) before the invoke-wmimthod runs for each computer through grid-view if possible.

function BroadcastMulti{
Import-Module active*

try{
    $msg = read-host "Enter your message "
    $List = Get-ADComputer -Filter {Name -like "PC*"}  | Select -ExpandName 
    Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "msg /time:3600 * $msg" -ComputerName $List
}catch{
     [System.Windows.Forms.MessageBox]::Show("Input was not entered correctly")
}
}
} 
4
  • 3
    Just add | Out-GridView -PassThru -OutputMode Multiple after Select-Object Nov 8, 2018 at 16:46
  • Did you mean after the "Select -expandName" ? Should i change "Select -expandName" to "Select-Object -expandName" ?
    – Viking
    Nov 8, 2018 at 17:33
  • 3
    As a best practice in scripts, you shouldn't use aliases (select is an alias for Select-Object and I believe you mean -ExpandProperty as -ExpandName is not a parameter unless that's a typo for -Expand Name which is another form of alias for the -ExpandProperty parameter). Nov 8, 2018 at 17:38
  • Unfortunately the command you had recommended did not work and my catch parameter caught it.
    – Viking
    Nov 13, 2018 at 22:00

1 Answer 1

0
function BroadcastMulti{
    Import-Module active* 
    try{
        $msg = read-host "Enter your message "
        $List = Get-ADComputer -Filter {Name -like "Computer*"}  | Select-Object -ExpandProperty Name 

        foreach($_ in $list){
        Write-Host $_
        $_ | Out-File C:\SupportTools\BroadcastReport.txt -Append
        Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "msg /time:43200 * $msg" -ComputerName $_
        }
        #[System.Windows.Forms.MessageBox]::Show("Message sent to all workstations")
    }catch{
         [System.Windows.Forms.MessageBox]::Show("There was an error, please try again")
          }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.