суббота, 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###

Delete local users if not current and not Admin

 # Get the username of the currently logged in user

$current_username = [Environment]::UserName


# Get all local users

$local_users = Get-WmiObject -Class Win32_UserAccount -Filter  "LocalAccount='True'" | Where-Object { $_.SID -notlike "S-1-5-21-*" }


# Iterate over the local users

foreach ($user in $local_users) {

    # If the user is not the currently logged in user

    if ($user.Name -ne $current_username) {

        # Delete the user

        net user $user. Name /delete

    }

}


Delete LocalAdmin users for SharedPC

 cls

# Get all local users named "LocalAdmin"

$local_users = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | Where-Object { $_.Name -like "LocalAdm*" }


# Iterate over the local users

foreach ($user in $local_users) {

    $user.Name

    # Delete the user if its name starts with "LocalAdmin"

    net user $user. Name /delete

}


Create LocalAdmin for Shared PC

cls

Add-Type -AssemblyName System.Windows.Forms

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

$form.Text = 'New Admin User'

$form.Size = New-Object System.Drawing.Size(400,250) # Reduced form size

$form.StartPosition = "CenterScreen"


# Status label

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

$statusLabel.Location = New-Object System.Drawing.Point(10,10)

$statusLabel.Size = New-Object System.Drawing.Size(360,150) # Reduced the height here

$statusLabel.Font = New-Object System.Drawing.Font("Arial",12,[System.Drawing.FontStyle]::Bold)

$statusLabel.ForeColor = [System.Drawing.Color]::DarkBlue

$statusLabel.Text = ""

$form.Controls.Add($statusLabel)


# add button

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

$button.Size = New-Object System.Drawing.Size(120,40)

$button.Font = New-Object System.Drawing.Font("Arial",12)

$button.Text = 'Add User'

$button.Location = New-Object System.Drawing.Point([int](($form.ClientSize.Width - $button.Width) / 2), [int]($form.ClientSize.Height - $button.Height - 10)) # Change the margin here


$button.Add_Click({

    $adminName = "LocalAdmin"

    $adminPass = 'Password123'

    #$adminPass = 'Pa$$word123'

    $suffix = ""

    while (Get-WmiObject Win32_UserAccount -filter "Name='$adminName$suffix'" -ErrorAction SilentlyContinue) {

        if ($suffix -eq "") {

            $suffix = 1

        } else {

            $suffix++

        }

    }

    $adminName += $suffix

    invoke-expression "net user /add $adminName $adminPass"

    $user = New-Object System.Security.Principal.NTAccount($adminName) 

    $sid = $user.Translate([System.Security.Principal.SecurityIdentifier]) 

    $sid = $sid.Value;

    New-Item -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\SharedPC\Exemptions\$sid" -Force

    $statusLabel.Text = "User $adminName`nhas been created."

})


$form.Add_Resize({

    $button.Top = $form.ClientSize.Height - $button.Height - 10 # Change the margin here

    $button.Left = ($form.ClientSize.Width - $button.Width) / 2

})


$form.Controls.Add($button)


# Check user on form load

$adminName = "LocalAdmin"

if (Get-WmiObject Win32_UserAccount -filter "Name='$adminName'" -ErrorAction SilentlyContinue) {

    $statusLabel.Text = "User $adminName already exists. `nRecommended not to create."

} else {

    $statusLabel.Text = "User $adminName does not exist."

}


$form.ShowDialog()



GUI to set Proxy in Windows

Adjust the Proxy URL as needed.

### START ### 

cls

# Set Proxy

# Define properties

$fontName = "Arial"

$fontSize = 10

$formWidth = 450

$formHeight = 350

$labelWidth = 570

$labelHeight = 20  

$formText = 'Set Proxy AutoConfigURL'

$currentLabelLocation = New-Object System.Drawing.Point(10,20)

$textBoxLocation = New-Object System.Drawing.Point(10,45)  

$futureLabelLocation = New-Object System.Drawing.Point(10,70)

$setButtonLocation = New-Object System.Drawing.Point(40,100)  

$cancelButtonLocation = New-Object System.Drawing.Point(130,100)

$removeButtonLocation = New-Object System.Drawing.Point(220,100)  

$buttonSize = New-Object System.Drawing.Size(75,23)

$autoConfigURL = 'http://pac.domain:8080/pac.pac'

$currentAutoConfigURL = 'Not set' 

$registryPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'


# Load necessary assemblies

Add-Type -AssemblyName System.Windows.Forms


# Create the form

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

$form.Text = $formText

$form.Size = New-Object System.Drawing.Size($formWidth,$formHeight)

$form.StartPosition = 'CenterScreen'


# Define the font

$font = New-Object System.Drawing.Font($fontName, $fontSize)


# Get the current AutoConfigURL value, if it exists

try {

    $currentAutoConfigURL = (Get-ItemProperty -Path $registryPath -Name AutoConfigURL -ErrorAction Stop).AutoConfigURL

} catch {

    # The AutoConfigURL property does not exist

}


# Create label for current AutoConfigURL

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

$currentLabel.Location = $currentLabelLocation

$currentLabel.Size = New-Object System.Drawing.Size($labelWidth,$labelHeight)

$currentLabel.Text = 'Current AutoConfigURL: ' + $currentAutoConfigURL

$currentLabel.Font = $font

$form.Controls.Add($currentLabel)


# Create TextBox for input

$inputTextBox = New-Object System.Windows.Forms.TextBox

$inputTextBox.Location = $textBoxLocation

$inputTextBox.Size = New-Object System.Drawing.Size($labelWidth,$labelHeight)

$inputTextBox.Text = $autoConfigURL

$form.Controls.Add($inputTextBox)


# Create Set button

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

$setButton.Location = $setButtonLocation

$setButton.Size = $buttonSize

$setButton.Text = 'Set'

$setButton.DialogResult = [System.Windows.Forms.DialogResult]::OK

$form.AcceptButton = $setButton

$form.Controls.Add($setButton)


# Create cancel button

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

$cancelButton.Location = $cancelButtonLocation

$cancelButton.Size = $buttonSize

$cancelButton.Text = 'Cancel'

$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

$form.CancelButton = $cancelButton

$form.Controls.Add($cancelButton)


# Create remove button

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

$removeButton.Location = $removeButtonLocation

$removeButton.Size = $buttonSize

$removeButton.Text = 'Remove'

$removeButton.Add_Click({

    # Set the AutoConfigURL to an empty string

    Set-ItemProperty -Path $registryPath -Name AutoConfigURL -Value ''

    # Disable proxy use in Internet Explorer settings

    Set-ItemProperty -Path $registryPath -Name ProxyEnable -Value 0

    $form.Close()

})

$form.Controls.Add($removeButton)


# Create a group box

$groupBox = New-Object System.Windows.Forms.GroupBox

$groupBox.Location = New-Object System.Drawing.Point(10,150)

$groupBox.Size = New-Object System.Drawing.Size(410,105)  # Adjust the size as necessary

$groupBox.Text = 'Instructions'

$groupBox.Font = $font

$form.Controls.Add($groupBox)


# Create instructions

$instructions = @(

    '1. Enter the new AutoConfigURL in the text box.',

    'Click Set to set the new AutoConfigURL and enable the proxy.',

    '2. Click Remove to clear the AutoConfigURL and disable proxy.',

    '3. Clear the textbox and click Set to disable the proxy.'

)


$labelLocation = 20

foreach ($instruction in $instructions) {

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

    $instructionLabel.Location = New-Object System.Drawing.Point(10,$labelLocation)

    $instructionLabel.Size = New-Object System.Drawing.Size(410,20)  # Adjust the size as necessary

    $instructionLabel.Text = $instruction

    $instructionLabel.Font = $font

    $groupBox.Controls.Add($instructionLabel)

    $labelLocation += 20  # Adjust the spacing as necessary

}


# Show the form

$form.Topmost = $true

$result = $form.ShowDialog()


# If user clicks Set, set the AutoConfigURL

if ($result -eq [System.Windows.Forms.DialogResult]::OK) {

    Set-ItemProperty -Path $registryPath -Name AutoConfigURL -Value $inputTextBox.Text

    Set-ItemProperty -Path $registryPath -Name ProxyEnable -Value 1

}

### END ### 


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

**Docker-Commands**

 

  • Image Creation with Dockerfile: 
    docker image build.

  • Image Tagging and Versioning: 
    docker image tag.

  • Image Distribution: 
    docker image push.

  • Image Sharing: 
    docker login,
    docker tag,
    docker push.

  • Image Pulling:
    docker image pull.

  • Running Containers: 
    docker container run.
  • Containers Management: 
    docker container ls
    docker container start,
    docker container stop,
    docker container rm.

  • Containers Show:
    Show all running containers: docker ps.
  • Show all containers (running or stopped): docker ps -a.
  • Show the logs of a container: docker logs <container_name>.
  • Show the details of a container: docker inspect <container_name>.
  • Show the disk usage of Docker objects: docker system df.
  • Image Updates and Maintenance: 
    docker image pull,
    docker image build,
    docker image push.

  • Rollbacks and Version Control: 
    docker image history,
    docker image tag.
Image Cleanup: 
docker image rm.

Cisco switch - add a port to VLAN

## Add port Gi4/0/50 to VLAN 333

enable
conf t
int gi4/0/50
no switchport access vlan 444
switchport access vlan 333
exit
exit
show run
copy run start

## Remove port Gi4/0/50 from VLAN
conf t
int gi4/0/50
no switchport access vlan 333

##Remove VLAN##
conf t
no vlan 333

## Save configuration 
copy run start

Comparing Terminal Emulators


I have created this table with the assistance of ChatGPT and Bard.

My primary concern is to include all three connection options: SSH, RDP, and Web. However, none of the five completely free options can connect using all three options simultaneously.


If you require SSH and RDP connectivity, mRemote could be considered the best choice. For Web access, Hyper is a viable option as it offers both SSH and Web connectivity.

If you only need SSH access, SuperPutty is a viable choice, especially considering its password memory feature, which is not available in its closest competitor, MTPutty.

For a comprehensive set of access options, MobaXterm or Devolution RDM can be considered. Both provide SSH, RDP, and Web connectivity even in their free versions. However, they do have limitations on the number of sessions or connections allowed. MobaXterm permits up to 12 connections, while Devolution RDM allows up to 10.

RoyalTS also offers all three connection options, but Web access is only available in the paid version. Hence, if you prefer a free option, mRemote is comparable to RoyalTS in terms of features, and mRemote does not have any limitations on the number of connections.

In summary, the two winners among the free options are MobaXterm and Devolution RDM, considering their limitations on the number of connections.


Here are some additional details about each program:

  • mRemote: mRemote is a free and open-source remote desktop connection manager that supports a wide range of protocols, including SSH, RDP, VNC, and Telnet. It has a simple interface and is easy to use.
  • Hyper: Hyper is a free and open-source terminal emulator that supports a wide range of protocols, including SSH, RDP, and Websockets. It has a modern interface and supports a wider range of features than mRemote, such as tabs, split panes, and macros.
  • SuperPutty: SuperPutty is a free and open-source SSH client that supports a wide range of features, such as saved sessions, password management, and tunneling. It is a good option if you only need SSH access.
  • MobaXterm: MobaXterm is a freemium remote desktop connection manager that supports a wide range of protocols, including SSH, RDP, VNC, and Telnet. It has a powerful feature set and is a good option for users who need more than the basic features offered by mRemote or Hyper.
  • Devolution RDM: Devolution RDM is a freemium remote desktop connection manager that supports a wide range of protocols, including SSH, RDP, VNC, and Telnet. It has a similar feature set to MobaXterm, but it is a bit less powerful.
  • RoyalTS: RoyalTS is a paid remote desktop connection manager that supports a wide range of protocols, including SSH, RDP, VNC, and Telnet. It has a powerful feature set and is a good option for users who need more than the basic features offered by the free options.