суббота, 22 июля 2023 г.

Enable Shared PC with registry

We need to check if this script really enables Shared PC mode.
It works with registry, but there is also a way to work with Cim/WMI.
In the next few days I will check what is better. 

###START###

cls


Add-Type -AssemblyName System.Windows.Forms


# Set form size and positioning variables

$mainFormWidth = 365

$mainFormHeight = 210

$margin = 20

$verticalSpacing = 30

$marksRightShift = 55

$boxSpacing = 20

$checkBoxSize = 20

$numericUpDownWidth = 70

$numericUpDownHeight = 20

$formFont = 'Microsoft Sans Serif, 10'


# Calculate positions

$label_X = $margin

$checkBox_X = $mainFormWidth / 2 + $boxSpacing

$numericUpDown_X = $mainFormWidth / 2 + $boxSpacing

$okButton_X = ($mainFormWidth - $numericUpDownWidth) / 2 - 80

$cancelButton_X = ($mainFormWidth + $numericUpDownWidth) / 2 -30

$button_Y = $mainFormHeight - $margin - $numericUpDownHeight - $verticalSpacing


# Create the main form

$mainForm = New-Object System.Windows.Forms.Form

$mainForm.Text = 'SharedPC Mode Configuration'

$mainForm.Size = New-Object System.Drawing.Size($mainFormWidth, $mainFormHeight)

$mainForm.StartPosition = 'CenterScreen'

$mainForm.Font = $formFont


# Create a label and checkbox for enabling SharedPC Mode

$enableSharedPCLabel = New-Object System.Windows.Forms.Label

$enableSharedPCLabel.Text = 'Enable SharedPC Mode:'

$enableSharedPCLabel.AutoSize = $True

$enableSharedPCLabel.Location = [System.Drawing.Point]::new($label_X, $margin)

$enableSharedPCCheckBox = New-Object System.Windows.Forms.CheckBox

$enableSharedPCCheckBox.Location = [System.Drawing.Point]::new($checkBox_X + $marksRightShift, $margin)

$enableSharedPCCheckBox.Size = New-Object System.Drawing.Size($checkBoxSize, $checkBoxSize)


$mainForm.Controls.Add($enableSharedPCLabel)

$mainForm.Controls.Add($enableSharedPCCheckBox)


# Create a label and numeric updown box for setting maintenance start time

$maintenanceLabel = New-Object System.Windows.Forms.Label

$maintenanceLabel.Text = 'Maintenance Start Time (0-23 hours):'

$maintenanceLabel.AutoSize = $True

$maintenanceLabel.Location = [System.Drawing.Point]::new($label_X, $margin + $verticalSpacing)

$maintenanceNumericUpDown = New-Object System.Windows.Forms.NumericUpDown

$maintenanceNumericUpDown.Location = [System.Drawing.Point]::new($numericUpDown_X + $marksRightShift, $margin + $verticalSpacing)

$maintenanceNumericUpDown.Size = New-Object System.Drawing.Size($numericUpDownWidth, $numericUpDownHeight)

$maintenanceNumericUpDown.Minimum = 0

$maintenanceNumericUpDown.Maximum = 23


$mainForm.Controls.Add($maintenanceLabel)

$mainForm.Controls.Add($maintenanceNumericUpDown)


# Create a label to display the result (SharedPC Mode enabled/disabled or AutoConfigURL value)

$resultLabel = New-Object System.Windows.Forms.Label

$resultLabel.Text = "Result will be displayed here..."

$resultLabel.AutoSize = $False

$resultLabel.Width = $mainFormWidth - $margin * 2

$resultLabel.Height = 60

$resultLabel.Location = [System.Drawing.Point]::new($margin, $margin + $verticalSpacing * 2)


$mainForm.Controls.Add($resultLabel)


# Create an Execute button

$executeButton = New-Object System.Windows.Forms.Button

$executeButton.Text = 'Execute'

$executeButton.Location = [System.Drawing.Point]::new($okButton_X, $button_Y)

$executeButtonWidth = $numericUpDownWidth + 20

$executeButtonHeight = $numericUpDownHeight + 5

$executeButton.Size = New-Object System.Drawing.Size($executeButtonWidth, $executeButtonHeight)


# Create a bold font and assign it to the Execute button

$boldFont = New-Object System.Drawing.Font("Microsoft Sans Serif", 10, [System.Drawing.FontStyle]::Bold)

$executeButton.Font = $boldFont


$mainForm.Controls.Add($executeButton)


# Create a Cancel button

$cancelButton = New-Object System.Windows.Forms.Button

$cancelButton.Text = 'Cancel'

$cancelButton.Location = [System.Drawing.Point]::new($cancelButton_X, $button_Y)

$cancelButton.Size = New-Object System.Drawing.Size($executeButtonWidth, $executeButtonHeight)

$cancelButton.Font = $boldFont


# Add an event handler to close the form when Cancel is clicked

$cancelButton.Add_Click({

    $mainForm.Close()

})


$mainForm.Controls.Add($cancelButton)


# Incorporate the provided short code to check SharedPC mode status

$wmiObj = Get-WmiObject -Namespace "root\cimv2\mdm\dmmap" -Class "MDM_SharedPC"

$sharedPCStatus = $wmiObj.EnableSharedPCMode


if ($sharedPCStatus) {

    $enableSharedPCCheckBox.Checked = $true

    $resultLabel.Text = "SharedPC Mode is currently enabled."

} else {

    $enableSharedPCCheckBox.Checked = $false

    $resultLabel.Text = "SharedPC Mode is currently disabled."

}


# Add an event handler for the Execute button click

$executeButton.Add_Click({

    # Check if the SharedPC status matches the checkbox state

    if ($enableSharedPCCheckBox.Checked -eq $sharedPCStatus) {

        $resultLabel.Text = "SharedPC Mode is already in the selected state."

        return

    }


    # Disable buttons during execution

    $executeButton.Enabled = $false

    $cancelButton.Enabled = $false

    $okButton.Enabled = $false


    # This is where you can use the user's inputs in your script

    $isSharedPCModeEnabled = $enableSharedPCCheckBox.Checked

    $maintenanceStartTime = $maintenanceNumericUpDown.Value


    # Enable or Disable SharedPC Mode based on the checkbox state

    if ($isSharedPCModeEnabled) {

        Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\CloudStore\Store\Cache\DefaultAccount' -Name "DefaultStoreAccountStatus" -Value 1

        $resultLabel.Text = "SharedPC Mode has been enabled."

    } else {

        Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\CloudStore\Store\Cache\DefaultAccount' -Name "DefaultStoreAccountStatus" -Value 0

        $resultLabel.Text = "SharedPC Mode has been disabled."

    }


    # Get the updated SharedPC status

    $sharedPCStatus = $wmiObj.EnableSharedPCMode


    # Enable buttons after execution is completed

    $executeButton.Enabled = $true

    $executeButton.Text = 'Execute'

    $cancelButton.Enabled = $true

    $okButton.Enabled = $true


})


# Create an OK button

$okButton = New-Object System.Windows.Forms.Button

$okButton.Text = 'OK'

$okButton.Location = [System.Drawing.Point]::new($cancelButton_X, $button_Y)

$okButton.Size = New-Object System.Drawing.Size($executeButtonWidth, $executeButtonHeight)

$okButton.Font = $boldFont

$okButton.Enabled = $false


# Add an event handler to close the form when OK is clicked

$okButton.Add_Click({

    $mainForm.Close()

})


$mainForm.Add_FormClosed({

    # Close the form if the user closes it using the window close button (X)

    $mainForm.Close()

})


$mainForm.Controls.Add($okButton)


# Show the form

$mainForm.ShowDialog()


###END###

Комментариев нет:

Отправить комментарий