tri_94 Posted December 7, 2018 Posted December 7, 2018 Hi there I'm looking at using powershell to send a message all online computers using code like below. $name = Get-Content "d:\computersonline.txt" #$name = read-host "Enter computer name " #$msg = read-host "Enter your message " $msg = "Test 1 2 3" Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "msg * $msg" -ComputerName $name In order for me to do this I was thinking along the lines of using powershell to query a list of computers and then export the data to a csv or txt file a list computers that are online and one maybe one with computers that are not. I would also just like the computer name and no other data in the exports. and then i'd be able to use this export to send the message using the code about. (I'm sure someone can tell me a far better way of doing this) $file = "D:\Computers.txt" Get-Content $file | ForEach-Object { New-Object -TypeName PSCustomObject -Property @{ VMName = $_ 'Ping Status' = Test-Connection -ComputerName $_ -Quiet -Count 1 } } | Export-Csv -Path "D:\PingStatus.csv" -NoTypeInformation Thanks for any help
steve Posted December 8, 2018 Posted December 8, 2018 When I do something like this I tend to use active directory as my list of computers, you'll need AD users and computers from RSAT on the PC running the code. You can then do this without having to maintain a text file. Even if you want to use a file, you can do this with only a single file of devices to check and not need to export the devices found. With code this you can test if the device is on, and if it is send the message. The code below will list in green the devices found and in red the devices not. #Option using AD OU $rtn = $null $msg = "Test 1 2 3" #Using AD OU Get-ADComputer -SearchBase "OU=Classroom1,OU=Computers,DC=my,DC=school,DC=com" -Filter * | ForEach-Object { $rtn = Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 -Quiet IF($rtn -match 'True') { Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "msg * $msg" -ComputerName $_.dnshostname Write-host -ForegroundColor green $_.dnshostname} ELSE { Write-host -ForegroundColor red $_.dnshostname } } #Option using a file with list of hostnames $rtn = $null $msg = "Test 1 2 3" $file = "D:\Computers.txt" Get-Content $file | ForEach-Object { $rtn = Test-Connection -CN $_ -Count 1 -BufferSize 16 -Quiet IF($rtn -match 'True') { Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "msg * $msg" -ComputerName $_ Write-host -ForegroundColor green $_} ELSE { Write-host -ForegroundColor red $_} }
tri_94 Posted December 9, 2018 Author Posted December 9, 2018 Thanks for that I’ll have a look at that. Only problem I would have would be that I’ve need to pull data from several ou’s and not just one. Would that be possible?
Arthur Posted December 9, 2018 Posted December 9, 2018 Would that be possible? Sure is. $OUs = "OU=Classroom1,OU=Computers,DC=my,DC=school,DC=com", "OU=Classroom2,OU=Computers,DC=my,DC=school,DC=com", "OU=Classroom3,OU=Computers,DC=my,DC=school,DC=com" $OUs | ForEach-Object { Get-ADComputer -SearchBase $_ -Filter * } @steve's script would therefore look like this... $rtn = $null $msg = "Test 1 2 3" $OUs = "OU=Classroom1,OU=Computers,DC=my,DC=school,DC=com", "OU=Classroom2,OU=Computers,DC=my,DC=school,DC=com", "OU=Classroom3,OU=Computers,DC=my,DC=school,DC=com" $OUs | ForEach-Object { Get-ADComputer -SearchBase $_ -Filter * } | ForEach-Object { $rtn = Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 -Quiet IF ($rtn -match 'True') { Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "msg * $msg" -ComputerName $_.dnshostname Write-Host -ForegroundColor green $_.dnshostname } ELSE { Write-Host -ForegroundColor red $_.dnshostname } } 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now