英文:
Trying NOT to use Out-GridView
问题
我使用 GetADUser
调用,它只返回指定组织单位中所有用户的 Sam 帐户名称。
我尝试在一个系统 Windows Forms 消息框中显示它们,但它不起作用。
我知道我接近成功,但我有点脑袋一片空白。有没有更好的方法来做到这一点?再次强调,我非常希望避免使用 Out-GridView
。我希望有类似这样的东西:
UserTest1
UserTest2
UserTest3
我尝试了一些谷歌搜索,但它们并不真正满足我的需求。任何帮助将不胜感激。谢谢!
英文:
I use a call to GetADUser
, which is just returning the Sam Account Name of all the users in an OU.
I'm trying to display them in a System Windows Forms Message Box but its not working.
I know I'm close, but I'm having a brain fart. Is there a better way of doing this? Again, I'm trying very hard to avoid using Out-GridView
. I'd love to have something like this:
UserTest1
UserTest2
UserTest3
I tried a couple of google searches but they were not really what I'm trying to do. Any help would be appreciated
Thank you
答案1
得分: 0
以下是您要的翻译部分:
<!-- language-all: sh -->
如果您确实希望在此处使用 WinForms [消息框](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.messagebox):
Add-Type -AssemblyName System.Windows.Forms
创建 20 个示例用户名。
在实际代码中,请使用:
$users = (Get-ADUser -filter * -Searchbase $OU).SamAccountName
$users = 1..20 | ForEach-Object { "user" + $_ }
显示每个用户名的消息框,每个用户名占据一行。
$users -join "`n",
'Title', # 窗口标题
'OKCancel', # 按钮
'Information' # 图标
)
请注意,这仅适用于用户名的数量_有限_,即只有屏幕上_一次_可以容纳的数量,不包括显示标题栏和按钮所需的空间。
如果用户名太多,将不会全部可见,按钮也不会显示。
要避免这个问题,您需要创建一个带有(可滚动的)列表框的自定义表单。
英文:
<!-- language-all: sh -->
If you really want to use a WinForms message box for this:
Add-Type -AssemblyName System.Windows.Forms
# Create 20 sample user names.
# In your real code, use:
# $users = (Get-ADUser -filter * -Searchbase $OU).SamAccountName
$users = 1..20 | ForEach-Object { "user" + $_ }
# Display a message box with each username on its own line.
[Windows.Forms.MessageBox]::Show(
$users -join "`n",
'Title', # window title
'OKCancel', # buttons
'Information' # icon
)
Note that this is only suitable for a limited number of usernames, namely as many as will fit on your screen at once, excluding the space needed to display the title bar and the button(s).
If there are too many, not all will be visible, and the button(s) won't show.
To avoid this problem, you'd need to create a custom form with a (scrollable) list box.
答案2
得分: 0
这是我最终完成的代码:
<!-- 语言:PowerShell -->
Get-ADUser -Filter * -Searchbase $Var1 | Where-Object {$__.name -notlike
"$var2*"} | ForEach-Object{[void]$Var3.Items.Add($_.SamAccountName)}
希望这对其他人有所帮助。
英文:
This is what I ended up doing:
<!-- language: powershell -->
Get-ADUser -Filter * -Searchbase $Var1 | Where-Object {$__.name -notlike
"$var2*"} | ForEach-Object{[void]$Var3.Items.Add($_.SamAccountName)}
Hope this will help someone else
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论