Create Software Forwarder

When computers have to be replaced - remove assigned softwares on old computer and assign them to new
This commit is contained in:
2019-08-29 12:06:18 +10:00
committed by GitHub
parent fd6f3a99e7
commit 748269685d

132
Software Forwarder Normal file
View File

@@ -0,0 +1,132 @@
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName PresentationFramework
# Creation of form object
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Software Forwarder'
$form.Size = New-Object System.Drawing.Size(300,240)
$form.StartPosition = 'CenterScreen'
# OK button object
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,140)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
# Cancel Button object
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,140)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
# Label object
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter Asset Number (Original):'
$form.Controls.Add($label)
# TextBox object
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)
# Label object
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,80)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Please enter Asset Number (New):'
$form.Controls.Add($label2)
# TextBox object
$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(10,100)
$textBox2.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox2)
$form.Topmost = $true
$form.Add_Shown({$textBox.Select()})
#Do/While loop while looking for a correct APRA usrename
Do
{
$SelectedBox = $null
#Show Dialog box
$result = $form.ShowDialog()
Try
{
#if 'Ok' is clicked
if ($result -eq [System.Windows.Forms.DialogResult]::OK){
$inputText = $textBox.Text.Trim()
$inputText2 = $textBox2.Text.Trim()
if ($textBox.Text.Trim().Contains('-') -and $textBox2.Text.Trim().Contains('-') -and $textBox.Text.Trim() -notcontains $textBox2.Text.Trim()){
$getAssetNumber = $inputText.ToUpper()
$ComputerObject = Get-ADComputer $getAssetNumber
$getNewAssetNumber = $inputText2.ToUpper()
$ComputerObjectNew = Get-ADComputer $getNewAssetNumber
}else{
[System.Windows.MessageBox]::Show('Cannot find Computers')
}
}
# if 'Cancel' is clicked
if($result -eq [System.Windows.Forms.DialogResult]::Cancel){
Exit
}
}
Catch
{
# Couldn't be found the user, Display message:
[System.Windows.MessageBox]::Show('Cannot find Computer for ' + $SelectedBox)
# restart loop
$getAssetNumber = $null
$getNewAssetNumber = $null
}
}
While ($getAssetNumber -eq $null)
$List = (Get-adcomputer $ComputerObject.DistinguishedName Properties MemberOf).MemberOf
#Write-Warning -Message "Could not find a user with the username: $List. Please check the spelling and try again."
if ($List -eq ''){
[System.Windows.MessageBox]::Show('No Assigned Software for ' + $textBox.Text)
}else{
foreach($item in $List){
$name = ($item -split ',*..=')[1]
Write-Output $name
Add-ADGroupMember -Identity $name -Members $ComputerObjectNew.DistinguishedName
}
foreach($item in $List){
$name = ($item -split ',*..=')[1]
Write-Output $name
remove-adgroupmember -Identity $name -Members $ComputerObject.DistinguishedName
}
[System.Windows.MessageBox]::Show('Software(s) moved from ' + $textBox.Text + ' to ' + $textBox2.Text)
}