Hello to all,
Hope you can help me
I have created a new PowerShell script with a GUI that work fine.
The purpose of the script is to give Help-Desk personnel the ability to add User names to a specific collection in Configuration manager.
According to the script, the help desk man need to enter user names inside the text box with comma separated value only (,) like this:
Domain\User1,Domain\User2
Is there a way to change that and only enter user names in a list like this:
Domain\user1
Domain\user2
If i try this way, it fails.
from script:
$users = $textBox.Text -split “,”
Thank you so much for your time and help.
New-PSDrive -Name Logfolder -PSProvider "FileSystem" -Root "\\network\RestartComputers"
$logfilepath = "logfolder:RestartComputers.txt"
$datetime = get-date -Format "yyyy-MM-dd HH:mm:ss"
$Logmessage = "[$datetime] Starting process"
$Logmessage | out-file -filepath $logfilepath -append
# Connect to the Configuration Manager site
$scriptPath = $PSScriptRoot
$SiteCode = "aaa"
$SiteServer = "servername.com"
Import-Module -name "$scriptPath\Module\ConfigurationManager.psd1"
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
New-PSDrive -name $SiteCode -psprovider CMSite -Root $SiteServer
}
Set-Location "$($SiteCode):\"
Add-Type -AssemblyName System.Windows.Forms
# Create the form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Restart machines tool"
$form.Size = New-Object System.Drawing.Size(300, 220)
$form.StartPosition = "CenterScreen"
# Create a label
$label = New-Object System.Windows.Forms.Label
$label.Text = "Enter usernames (comma-separated):"
$label.Location = New-Object System.Drawing.Point(10, 10)
$label.AutoSize = $true
# Create a text box
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Multiline = $true
$textBox.ScrollBars = "Vertical"
$textBox.Size = New-Object System.Drawing.Size(250, 100)
$textBox.Location = New-Object System.Drawing.Point(10, 40)
# Create a button
$button = New-Object System.Windows.Forms.Button
$button.Text = "Add Users"
$button.Location = New-Object System.Drawing.Point(10, 150)
$button1 = New-Object System.Windows.Forms.Button
$button1.Text = "Close"
$button1.Location = New-Object System.Drawing.Point(190, 150)
$button2 = New-Object System.Windows.Forms.Button
$button2.Text = "OpenLog"
$button2.Location = New-Object System.Drawing.Point(100, 150)
# Add controls to the form
$form.Controls.Add($label)
$form.Controls.Add($textBox)
$form.Controls.Add($button)
$form.Controls.Add($button1)
$form.Controls.Add($button2)
$form.CancelButton = $button1
# Define button click event handler
$button.Add_Click({
$users = $textBox.Text -split ","
$collectionID = "aaa3535235" # Update with the actual Collection ID
# Add each user to the collection
foreach ($user in $users) {
$userObject = Get-CMUser -Name $user
Add-CMUserCollectionDirectMembershipRule -CollectionID $collectionID -ResourceID $userObject.ResourceID -PassThru
$commandResult = $?
$Logmessage = "[$datetime] $user added to collection with result: $commandResult"
$Logmessage | Out-File -FilePath $logfilepath -Append
$textbox.Text = ""
}
})
# Define button2 click event handler
$button2.Add_Click({
cd logfolder:
.\RestartComputers.txt
})
# Show the form
$form.ShowDialog()