Reputation: 3162
I am trying to make a script in powershell sending a message box to a list of remote PCs. but i got all messages on my local PC.
Can anyone tell me where I went wrong?
$PCLIST = Get-Content 'C:\TEST\PCLIST.TXT'
ForEach ($computer in $PCLIST) {
Enter-PSSession -ComputerName $computer
$GetUserName = [Environment]::UserName
#$CmdMessage has to be one line
$CmdMessage = {C:\windows\system32\msg.exe $GetUserName 'Hello' $GetUserName 'This is a test!'}
Invoke-Command -Scriptblock $CmdMessage
}
Upvotes: 2
Views: 36490
Reputation: 1
For me I had to tweak the script. To make a better box with space between lines. All I need to update is my .ps1 and the pclist.txt
$servers = Get-Content -Path C:\vm\pclist.txt
$msg = ("* Rappel *n
n-1st line.n-2nd line.
nn3rd line,
n4th line.")
ForEach ($server in $servers)
{ Invoke-WmiMethod -Class win32_process -ComputerName $server -Name create -ArgumentList "c:\windows\system32\msg.exe * $msg" }
You need to remove one ` just before every "n" like the picture below I wish it could help some.
Upvotes: 0
Reputation: 1
$name = read-host "Enter computer name "
$msg = read-host "Enter your message "
Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "msg * $msg" -ComputerName $name
EZ GAME EZ LIFE
Upvotes: -1
Reputation: 86
mjolinor is right. Invoke-Command would be better for what you're trying to do. You can work with what you have now, just build the scriptblock for each invoke calls. (I'm using '*' instead of the specific user in the msg param to send it to all users.) Edit: I just realized that the current username variable will probably capture the user invoking this command. An alternative method of acquiring the current user will be required. Maybe through AD or GWMI.
$PCLIST = Get-Content 'C:\TEST\PCLIST.TXT'
ForEach ($computer in $PCLIST) {
Invoke-Command -ComputerName $computer -Scriptblock {
$GetUserName = [Environment]::UserName
$CmdMessage = {C:\windows\system32\msg.exe * 'Hello' $GetUserName 'This is a test!'}
$CmdMessage | Invoke-Expression
}
}
Upvotes: 5
Reputation: 68273
You cannot use Enter-PSSession
for scripting. It can only be used interactively.
If you use it in a script, all the following commands will still execute in your local session. To execute commands in a remote session in a script you have to use Invoke-Command
.
That being said, I don't think this is going to work anyway. It's not going to execute in the context of the logged on user of the remote computer, and there's no UI in that remote session for the message box to display in.
Upvotes: 0