Search code examples
powershellactive-directorywmipowershell-2.0msgbox

Powershell 2.0 - Invoke-wmimethod and get the computer name in gridview


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")
}
}
} 

Solution

  • 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")
              }
        }