Powershell弹出用户名列表

时间:2019-01-23 14:50:09

标签: powershell

我希望有一个Powershell脚本,它可以向特定的用户列表提供弹出消息。我需要在电子表格中使用相关用户的列表及其网络用户名,而不要使用Active Directory。我具有Powershell脚本来显示带有警告图标的适当消息,并发现必须以某种方式格式化弹出窗口,而我必须使用“ FORM”而不是默认的弹出消息框。尽管以下两行警告图标在表单中不起作用,所以必须使用图片框

$WarningIcon = New-Object ([System.Windows.MessageBoxImage]::Warning)
$Form.Controls.Add($WarningIcon)

该表格的脚本如下。可能有一种更清洁的方式来创建这样的表单,但是我对Powershell还是陌生的!

Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Width = 700
$Form.Height = 400
$Form.BackColor = "#DCDCDC"

$Form.Text = "System Restart Alert"

$Font = New-Object System.Drawing.Font("Ariel",30, 
   [System.Drawing.FontStyle]::Bold::Underline)
$FontB = New-Object System.Drawing.Font("Ariel",14, 
   [System.Drawing.FontStyle]::Bold)

$Picture = (get-item ("C:\FA_Files\Windows_Warning_Exclamation9.jpg"))
$img = [System.Drawing.Image]::Fromfile($Picture)
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Location = New-object System.Drawing.Size(160,30)
$pictureBox.Height = "100"
$pictureBox.Image = $img
$Form.controls.add($pictureBox)

$Label = New-Object System.Windows.Forms.Label
$Label.Location = "260,30"
$Label.Font = $Font
$Label.ForeColor = "Red"
$Label.Text = "WARNING!"
$Label.AutoSize = $True
$Form.Controls.Add($Label)

$LabelB = New-Object System.Windows.Forms.Label
$LabelB.Location = "100,130"
$LabelB.Font = $FontB
$LabelB.Text = "Due to essential maintenance system requires rebooting"
$LabelB.AutoSize = $True
$Form.Controls.Add($LabelB)

$LabelC = New-Object System.Windows.Forms.Label
$LabelC.Location = "100,160"
$LabelC.Font = $FontB
$LabelC.Text = "Please save all work immediately"
$LabelC.AutoSize = $True
$Form.Controls.Add($LabelC)

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = "300,280"
$okButton.Font = "$FontB"
$okButton.Size = "85,28"
$okButton.Text = "Okay"

$Form.Controls.Add($okButton)
$okButton.Add_Click = ({$Form.Close()})
$Form.ShowDialog()

我可以从电子表格中检索网络用户名列表。下面是csv内容的图片

enter image description here

但是我在Google上搜索了很多,无法找到一种将弹出窗口发送到从电子表格中检索到的用户名列表的方法。到目前为止,我已经尝试过以下操作,其中将上面的表单设置为$ msg变量,并且还尝试将表单保存在另一个文件中,并在$ msg变量中引用该文件,但这不起作用

$csv = Import-csv "C:\FA_Files\NetNames.csv"

foreach($line in $csv)
{
  $name = $line.("Name")
  $netName = $line.("NetworkName")
  #Echo "Name is $name and Network Name is $netName"
  msg $netName $msg
}

它也必须在用户名上,而不是计算机名称上。

请问如何纠正?

2 个答案:

答案 0 :(得分:1)

FWIW,msg.exe在Windows中已取代net /send,并且是用于向用户发送消息的,所以我将从looking at the syntax for message.exe here开始。 。

它具有一个/server:开关,可用于将消息发送到远程主机。因此,获取您的用户名和计算机名列表,并将其放入CSV文件中,如下所示:

//MyInputFile.csv
ComputerName,UserName
Laptop01,BillG
Laptop02,StephenO
PC03,WayneH
Desktop04,JimA

您可以使用这样的简短脚本来实现您的目标(或至少可以使您到那里)

$Msg = "Put your message here"
$SpreadSheet = Import-CSV .\PathTo\MyInputFile.csv
ForEach ($row in $SpreadSheet){
    "Sending message to \\$($row.ComputerName)\$($row.UserName) : $msg"
    msg /server:$($row.ComputerName) $($row.UserName) $msg
}

您将在控制台中看到以下输出:

Sending message to \\Laptop01\BillG : Put your message here
Sending message to \\Laptop02\StephenO : Put your message here
Sending message to \\PC03\WayneH : Put your message here
Sending message to \\Desktop04\JimA : Put your message here
Sending message to \\localhost\* : Put your message here

然后,您的幸运用户将在其工作站上看到以下内容。
enter image description here

如果无法连接到计算机,则该过程将在五秒钟左右后超时,然后继续执行列表中的下一台计算机。

答案 1 :(得分:0)

默认情况下,作为Active Directory成员的所有用户都具有读取权限,这是设计使然,因为Active Directory正是您需要的那种功能所依赖的系统...不确定为什么您的ICT经理反对这个想法。

您不能仅向用户名发送消息给用户,还必须知道用户已登录的计算机。

我能想到的唯一方法是遍历每台计算机并查询谁登录,并且如果用户与您的列表匹配,则向该计算机发送消息。这将需要从AD中读取计算机列表,或遍历本地网络上的每个可用IP地址。