Powershell and configuration manager

Hi all,
I have created a new PowerShell form with the help of chat GPT.
I requested a form with big textbox and a command button.
My goal is to give this utility to a few support personnel. They will write user name in the textbox, and after pressing the button, the users will be added to Configuration manager (SCCM) specific collection.
The script works great and I can see the result on PowerShell ISE, but the support personnel will not see the result if the action failed or succussed.

I added this command: out-file -Filepath c:\install\log.txt
but the file stays empty.

Any help will be appreciated
Thank you

# Connect to the Configuration Manager site
$scriptPath = $PSScriptRoot
$SiteCode = "aaa"
$SiteServer = "servername.doamin.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 = "Add Users to Collection"
$form.Size = New-Object System.Drawing.Size(300, 220)

# 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(100, 150)

# Add controls to the form

# Define button click event handler
$users = $textBox.Text -split ","
$collectionID = "collectionid"

 # Add each user to the collection
foreach ($user in $users) {
$userObject = Get-CMUser -Name $user
Add-CMUserCollectionDirectMembershipRule -CollectionID $collectionID -ResourceID $userObject.ResourceID | out-file -Filepath c:\install\log.txt

$textbox.Text = ""type or paste code here

Try using the -PassThru parameter:

Add this parameter to return an object that represents the item with which you’re working. By default, this cmdlet may not generate any output.

1 Like