I want to add and remove user from the lokal Administrator group. Problem is that even I elevate rights in first place It still Access Denies me when I try to add them.
In a similar code It works this way... only thing I changed it that I created a GUI for it.
Here is the code:
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
if (!$myWindowsPrincipal.IsInRole($adminRole)){
start-process "powershell" -Verb "runas" -ArgumentList "-File",$MyInvocation.MyCommand.Definition
}
[VOID] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[Void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.BackgroundImageLayout = 0
$objForm.BackgroundImage =[System.Drawing.Image]::FromFile('xx')
$objForm.StartPosition = "CenterScreen"
$objForm.Icon="xx"
$objForm.Size = New-Object System.Drawing.Size(400,200)
$objForm.Text = "Lokal Admin Adder v.01"
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(75,30)
$objLabel.Size = New-Object System.Drawing.Size(240,20)
$objLabel.Text = "Nutzernamen eingeben(m_mustermann)"
$objForm.Controls.Add($objLabel)
$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(280,140)
$objLabel2.Size = New-Object System.Drawing.Size(300,20)
$objLabel2.Text = "by Lucas Klarner"
$objForm.Controls.Add($objLabel2)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(85,50)
$objTextBox.Size = New-Object System.Drawing.Size(200,20)
$objForm.Controls.Add($objTextBox)
$FinishButton = New-Object System.Windows.Forms.Button
$FinishButton.Location = New-Object System.Drawing.Size (20,80)
$FinishButton.Size = New-Object System.Drawing.Size (150,20)
$FinishButton.Text = "Nutzer hinzufügen"
$FinishButton.Name = "Nutzer hinzufügen"
$FinishButton.Add_Click({
$Usr = $objTextBox.Text; Add-LocalGroupMember -Group Administratoren -Member $Usr })
$objForm.Controls.Add($FinishButton)
$DeleteButton = New-Object System.Windows.Forms.Button
$DeleteButton.Location = New-Object System.Drawing.Size (210,80)
$DeleteButton.Size = New-Object System.Drawing.Size (150,20)
$DeleteButton.Text = "Nutzer entfernen"
$DeleteButton.Name = "Nutzer entfernen"
$DeleteButton.DialogResult = "OK"
$DeleteButton.Add_Click({
$Usr = $objTextBox.Text;Remove-LocalGroupMember -Group Administratoren -Member $Usr -Verbose })
$objForm.Controls.Add($DeleteButton)
[void] $objForm.ShowDialog()
Thanks a lot!!
FIXED:
Somehow my elevation code was not running correctly and I made a new one out of some internet posts:
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
$CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
Exit
}
}
Here's how it works:
The first line checks to see if the script is already running in an elevated environment. This would occur if PowerShell is running as Administrator or UAC is disabled. If it is, the script will continue to run normally in that process.
The second line checks to see if the Windows operating system build number is 6000 (Windows Vista) or greater. Earlier builds did not support Run As elevation.
The third line retrieves the command line used to run the original script, including any arguments.
Finally, the fourth line starts a new elevated PowerShell process where the script runs again. Once the script terminates, the elevated PowerShell window closes.
https://blog.expta.com/2017/03/how-to-self-elevate-powershell-script.html
Related
I'm trying to make a GUI for an easy tool I need to check if a specific certificate is included in the file and Output the current and next line. This already works.
If I set the variables $INT1 and $Serial1 manually within the code it also works as intended.
e.g. $INT1=31 and $Serial1="5AA61E07726DAC13".
I always get an Error that the variable is empty when inputing the same values into the GUI and clicking OK. What am I doing wrong?
The code:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.StartPosition = "CenterScreen"
$objForm.Size = New-Object System.Drawing.Size(400,250)
$objForm.Topmost = $True
#Label+Textbox first question
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,30)
$objLabel.Size = New-Object System.Drawing.Size(800,20)
$objLabel.Text = "INT of certificate"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,50)
$objTextBox.Size = New-Object System.Drawing.Size(200,20)
$objForm.Controls.Add($objTextBox)
#Label+Textbox second question
$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(10,130)
$objLabel2.Size = New-Object System.Drawing.Size(800,20)
$objLabel2.Text = "Serialnumber of the certificate"
$objForm.Controls.Add($objLabel2)
$objTextBox2 = New-Object System.Windows.Forms.TextBox
$objTextBox2.Location = New-Object System.Drawing.Size(10,150)
$objTextBox2.Size = New-Object System.Drawing.Size(200,20)
$objForm.Controls.Add($objTextBox2)
$INT1 = $objTextBox.Text;
$Serial1 = $objTextBox2.Text;
#OK Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(100,175)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Name = "OK"
#$OKButton.DialogResult = "OK"
$OKButton.Add_Click({Invoke-WebRequest -Uri http://CENSORED.crl -OutFile crl.crl;
$test=certutil.exe -dump crl.crl |Out-String -stream | Select-String -pattern "$Serial1" -Context 0,1
[void] [Windows.Forms.MessageBox]::Show($test)})
$objForm.Controls.Add($OKButton)
[void] $objForm.ShowDialog()
Thank you!
Here's a rewrite of your code.
I'm assuming that you want variables $INT1 and $Serial1 to be available after the windows closes.
In that case, fill them inside the Click() scriptblock and refer to them using $script: scoping, otherwise they are just new variables local to the scriptblock.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# these variables need to visible inside the Click scriptblock of the button
# and be also available after the windows closes
$INT1 = $null
$Serial1 = $null
$objForm = New-Object System.Windows.Forms.Form
$objForm.StartPosition = "CenterScreen"
$objForm.Size = New-Object System.Drawing.Size(400,250)
$objForm.Topmost = $True
#Label+Textbox first question
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,30)
$objLabel.Size = New-Object System.Drawing.Size(800,20)
$objLabel.Text = "INT of certificate"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,50)
$objTextBox.Size = New-Object System.Drawing.Size(200,20)
$objForm.Controls.Add($objTextBox)
#Label+Textbox second question
$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(10,130)
$objLabel2.Size = New-Object System.Drawing.Size(800,20)
$objLabel2.Text = "Serialnumber of the certificate"
$objForm.Controls.Add($objLabel2)
$objTextBox2 = New-Object System.Windows.Forms.TextBox
$objTextBox2.Location = New-Object System.Drawing.Size(10,150)
$objTextBox2.Size = New-Object System.Drawing.Size(200,20)
$objForm.Controls.Add($objTextBox2)
#OK Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(100,175)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Name = "OK"
#$OKButton.DialogResult = "OK"
$OKButton.Add_Click({
# you never seem to use variable $INT1, but if you do, refer to it as $script:INT1
# set the variables defined OUTSIDE the scriptblock using script-scoping
$script:INT1 = $objTextBox.Text
$script:Serial1 = $objTextBox2.Text
# create a full path and filename for the output of the Invoke-WebRequest call
$crlFile = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath 'crl.crl'
Invoke-WebRequest -Uri 'http://CENSORED.crl' -OutFile $crlFile
$test = certutil.exe -dump $crlFile | Out-String -Stream | Select-String -Pattern $script:Serial1 -Context 0,1
[void] [Windows.Forms.MessageBox]::Show($test)
# delete the temporary file
$crlFile | Remove-Item -Force
})
$objForm.Controls.Add($OKButton)
[void] $objForm.ShowDialog()
# clean up the form from memory'
$objForm.Dispose()
# here you can again see what is inside variables $INT1 and $Serial1
Write-Host "Last entered values: $INT1 $Serial1"
All user interaction with your form happens in this, blocking call:
[void] $objForm.ShowDialog()
See the ShowDialog() method.
Therefore, if you want to store results from that user interaction in variables, place the assignments after the call, and reference the controls of interest.
# ... form setup
# Show the form modally (as a dialog).
# The call blocks until the form is closed.
# Note:
# You may want to examine the return value in case there
# are multiple ways to close your form, and one of those ways
# signals having *canceled*, for instance.
$null = $objForm.ShowDialog()
# Now you can get values from the controls and assign them to (script) variables.
$INT1 = $objTextBox.Text
$Serial1 = $objTextBox2.Text
Note: If you need to set script-level variables - i.e., variables visible after a .ShowDialog() call - from inside script blocks serving as event delegates (e.g., the script block passed to $OKButton.Add_Click()), you need to use the $script: scope, given that such script blocks run in a child scope too - see this answer.
This what I have so far it brings up a Syntax screen for wusa. I have confirmed that the Trim is working. If I leave out the remote computer name is works on the local computer. I will be adding this to a much larger script just trying to get this working before trying to add it.
<#
.NAME
Template
#>
$comp = "Remote Pc Name Goes Here"
$str = $Hotfix_TextBox.Text. Trim("K","B")
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(400,400)
$Form.text = "Form"
$Form.TopMost = $false
$gethotfix = New-Object system.Windows.Forms.Button
$gethotfix.text = "Get Hotfixes"
$gethotfix.width = 120
$gethotfix.height = 30
$gethotfix.location = New-Object System.Drawing.Point(100,81)
$gethotfix.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$removehotfix = New-Object system.Windows.Forms.Button
$removehotfix.text = "Remove Hotfix"
$removehotfix.width = 120
$removehotfix.height = 30
$removehotfix.location = New-Object System.Drawing.Point(100,120)
$removehotfix.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Hotfix_TextBox = New-Object system.Windows.Forms.TextBox
$Hotfix_TextBox.Text = ""
$Hotfix_TextBox.multiline = $false
$Hotfix_TextBox.width = 174
$Hotfix_TextBox.height = 20
$Hotfix_TextBox.location = New-Object System.Drawing.Point(12,235)
$Hotfix_TextBox.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
<#
$Trimmed_TextBox = New-Object system.Windows.Forms.TextBox
#$Trimmed_TextBox.Text = "$str"
$Trimmed_TextBox.multiline = $false
$Trimmed_TextBox.width = 174
$Trimmed_TextBox.height = 20
$Trimmed_TextBox.location = New-Object System.Drawing.Point(12,265)
$Trimmed_TextBox.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
#>
$Form.controls.AddRange(#($gethotfix,$removehotfix,$Hotfix_TextBox))
$gethotfix.Add_Click({ GetHotfix })
$removehotfix.Add_Click({ RemoveHotfix })
#region Logic
function GetHotfix {$Hotfix_TextBox.Text = Get-Hotfix -Computername $comp |
Select-Object -ExpandProperty 'HotFixID'|
Out-GridView -Title 'Installed Hotfixes' -PassThru }
#$Hotfix_TextBox.Text. Trim("K","B")
#$Hotfix_TextBox.Text = "$str"
function RemoveHotfix{
#$Trimmed_TextBox.Text = "$str"
$comp = "dus-xtdfed9r386"
#Uninstall-HotFix -ComputerName $comp
wusa -computername /$comp | /uninstall | /kb:$str
}
#endregion
[void]$Form.ShowDialog()
This turned out to be a fiasco. Even when I could get it to work there are many KBs that cannot be removed, as well as we had policies that would not allow us to remove many others. Just not worth the effort. Perhaps someone with less strengent policies in place can do something with this. Have Fun.
I am writing a program using PowerShell WinForms.
For example, the "Login" part of the program.
#CREDENTIAL CREATE - LOGIN AND PASSWORD INPUT
############################################
#APP FOUNDATION
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#Login_Form
$Login_Form = New-Object -TypeName System.Windows.Forms.Form
$Login_Form.ClientSize = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]340,[System.Int32]110))
$Login_Form.Text = [System.String]'Login_Form'
$Login_Form.add_Load($Login_Form_Load)
$Login_Form.ResumeLayout($false)
$Login_Form.PerformLayout()
$Login_Form.SuspendLayout()
$Login_Form.FormBorderStyle = "FixedDialog"
#Login_Label_User
$Login_Label_User = (New-Object -TypeName System.Windows.Forms.Label)
$Login_Label_User.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]12,[System.Int32]9))
$Login_Label_User.Name = [System.String]'Login_Label_User'
$Login_Label_User.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]100,[System.Int32]18))
$Login_Label_User.TabIndex = [System.Int32]3
$Login_Label_User.Text = [System.String]'Login:'
$Login_Label_User.UseCompatibleTextRendering = $true
#Login_TextBox_User
$Login_TextBox_User = (New-Object -TypeName System.Windows.Forms.TextBox)
$Login_TextBox_User.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]12,[System.Int32]30))
$Login_TextBox_User.Name = [System.String]'Login_TextBox_User'
$Login_TextBox_User.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]224,[System.Int32]21))
$Login_TextBox_User.TabIndex = [System.Int32]0
$Login_TextBox_User.Text = ((Get-ADDomain).name + "\")
$Login_TextBox_User.add_TextChanged($Login_TextBox_User_TextChanged)
#Login_TextBox_Password
$Login_TextBox_Password = (New-Object -TypeName System.Windows.Forms.TextBox)
$Login_TextBox_Password.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]12,[System.Int32]74))
$Login_TextBox_Password.Name = [System.String]'Login_TextBox_Password'
$Login_TextBox_Password.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]187,[System.Int32]21))
$Login_TextBox_Password.TabIndex = [System.Int32]1
$Login_TextBox_Password.Text = [System.String]''
$Login_TextBox_Password.PasswordChar = '*'
#Login_Button_Enter
$Login_Button_Enter = (New-Object -TypeName System.Windows.Forms.Button)
$Login_Button_Enter.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]251,[System.Int32]74))
$Login_Button_Enter.Name = [System.String]'Login_Button_Enter'
$Login_Button_Enter.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]75,[System.Int32]21))
$Login_Button_Enter.TabIndex = [System.Int32]2
$Login_Button_Enter.Text = [System.String]'ENTER'
$Login_Button_Enter.UseCompatibleTextRendering = $true
$Login_Button_Enter.UseVisualStyleBackColor = $true
$Login_Button_Enter.add_Click($Login_Button_Enter_Click)
#Login_Button_ShowHide
$Login_Button_ShowHide = (New-Object -TypeName System.Windows.Forms.Button)
$Login_Button_ShowHide.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]205,[System.Int32]74))
$Login_Button_ShowHide.Name = [System.String]'Login_Button_ShowHide'
$Login_Button_ShowHide.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]32,[System.Int32]21))
$Login_Button_ShowHide.TabIndex = [System.Int32]2
$Login_Button_ShowHide.Text = [System.String]'SHOW'
$Login_Button_ShowHide.UseCompatibleTextRendering = $true
$Login_Button_ShowHide.UseVisualStyleBackColor = $true
$Login_Button_ShowHide.add_Click($Login_Button_ShowHide_Click)
#Login_Label_Password
$Login_Label_Password = (New-Object -TypeName System.Windows.Forms.Label)
$Login_Label_Password.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]12,[System.Int32]54))
$Login_Label_Password.Name = [System.String]'Login_Label_Password'
$Login_Label_Password.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]100,[System.Int32]17))
$Login_Label_Password.TabIndex = [System.Int32]4
$Login_Label_Password.Text = [System.String]'Password:'
$Login_Label_Password.UseCompatibleTextRendering = $true
$Login_Label_Password.add_Click($Label2_Click)
#Form.Controls
$Login_Form.controls.AddRange(#($Login_Label_Password,$Login_Label_User,$Login_Button_Enter,$Login_Button_ShowHide,$Login_TextBox_Password,$Login_TextBox_User))
#Login_Button_Enter Click logic
$Login_Button_Enter_Click = {
$Script:username = $Login_TextBox_User.Text
$Script:password = $Login_TextBox_Password.Text
$Script:secstr = New-Object -TypeName System.Security.SecureString
$Script:password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$Script:cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
$Login_Form.Close()
}
#Login_Button_ShowHide Click logic
$Login_Button_ShowHide_Click = {
if($Script:Login_TextBox_Password.PasswordChar -eq '*') {
$Script:Login_TextBox_Password.PasswordChar = 0
$Login_Button_ShowHide.Text = [System.String]'SHOW'
}
elseif($Login_TextBox_Password.PasswordChar -eq 0) {
$Login_Button_ShowHide.Text = [System.String]'HIDE'
$Script:Login_TextBox_Password.PasswordChar = "*"
}
}
#START APPLICATION
[void]$Login_Form.ShowDialog()
When I run the script for the first time, the window and everything else is shown correctly, but the buttons and logic do not work.
For everything to work fine, I need to run it a second time.
How do I make everything work the first time I start it?
I've tried everything that is possible already. I do not understand what is wrong.
PowerShell is a largely interpreted language - statements are executed in line order.
You therefore need to define the event action scriptblocks before you can assign them to the respective event handlers, otherwise you're just assigning $null to the event handlers, which is why none of your buttons work.
# define the scriptblock that's supposed to handle the event action...
$Login_Button_ShowHide_Click = {
if($Script:Login_TextBox_Password.PasswordChar -eq '*') {
$Script:Login_TextBox_Password.PasswordChar = 0
$Login_Button_ShowHide.Text = [System.String]'SHOW'
}
elseif($Login_TextBox_Password.PasswordChar -eq 0) {
$Login_Button_ShowHide.Text = [System.String]'HIDE'
$Script:Login_TextBox_Password.PasswordChar = "*"
}
}
#Login_Button_ShowHide
$Login_Button_ShowHide = (New-Object -TypeName System.Windows.Forms.Button)
$Login_Button_ShowHide.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]205,[System.Int32]74))
$Login_Button_ShowHide.Name = [System.String]'Login_Button_ShowHide'
$Login_Button_ShowHide.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]32,[System.Int32]21))
$Login_Button_ShowHide.TabIndex = [System.Int32]2
$Login_Button_ShowHide.Text = [System.String]'SHOW'
$Login_Button_ShowHide.UseCompatibleTextRendering = $true
$Login_Button_ShowHide.UseVisualStyleBackColor = $true
# ... _before_ assigning it to handlethe event
$Login_Button_ShowHide.add_Click($Login_Button_ShowHide_Click)
Repeat for all the scriptblocks assigned as event handlers - either move them all to the top of the script, or, move all the .add_<EventName>() calls to the bottom, just before launching the GUI:
# Rest of script goes here ...
# register all event handlers
$Login_TextBox_User.add_TextChanged($Login_TextBox_User_TextChanged)
$Login_Button_Enter.add_Click($Login_Button_Enter_Click)
$Login_Button_ShowHide.add_Click($Login_Button_ShowHide_Click)
# start app
[void]$Login_Form.ShowDialog()
Ok, I know this is going to end up being basic but I cant seem to figure it out. I am trying to build a tool that will allow us to get the full AD group name for a user. The current problem we have is that the names of our AD groups are long, so a net user MrSmith /domain ends up truncating the full AD group name. I tried using a gpresult -user MrSmith /r but this doesnt return all of the AD groups for some reason.
So I decided that I would create something new that will also have other applications later on. I want to do this by first: doing a net group /domain to pull all of the AD groups in the DC. Second run a net group $ADGroup /domain for each of the ADGroups in the domain and use that output to search for the specified user ID and create a new output that displays the full name of each AD group that user ID is a part of. I am putting this in a drop down GUI for some of the future features I want to add.
Now I have got a good chunk of the first step done, but obviously I am running into an issue. When I do the net group /domain i get output looking like
The request will be processed at a domain controller for domain MyComapny.com.
Group Accounts for \\Server.MyComapny.com
-------------------------------------------------------------------------------
*AD_Group_1
*AD_Group_2
*AD_Group_3
*AD_Group_4
.....
And I add this to a temp .txt file for use.
The Issue I am having is that its not performing the net group $Choice /domain on the AD group selected from the drop down and I dont know why.
Any advice is welcome, I know I am not the best at this so my format and style may not be standard.
Clear-Variable DropDownList
$RawADGroups = Net Group /domain >> 'C:\script_out\RawADList1.txt'
(get-content -path 'C:\script_out\RawADList1.txt') | ForEach-Object {$_ -replace '\*',''} >> 'C:\script_out\RawADList2.txt'
[string[]]$DropDownList = Get-Content -Path 'C:\script_out\RawADList2.txt'
Remove-Item 'C:\script_out\RawADList1.txt'
Remove-Item 'C:\script_out\RawADList2.txt'
function Return-DropDown {
$Choice = $DropDown.SelectedItem.ToString()
if ($Choice)
{net group $Choice /domain
}
}
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 340
$Form.height = 150
$Form.Text = "ADGroup"
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(10,10)
$DropDown.Size = new-object System.Drawing.Size(300,30)
ForEach ($Line in $DropDownList) {
$DropDown.Items.Add($Line)
}
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(200,20)
$Form.Controls.Add($DropDownLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "Pick One"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
When I click the Pick One button nothing happens, it doesnt reflect anything.
I Figured this out, it was as I thought VERY simple, I was just forgetting to have the command do anything with the output. Hope this might help someone else.
Clear-Variable DropDownList
$RawADGroups = Net Group /domain >> 'C:\script_out\RawADList1.txt'
(get-content -path 'C:\script_out\RawADList1.txt') | ForEach-Object {$_ -replace '\*',''} >> 'C:\script_out\RawADList2.txt'
[string[]]$DropDownList = Get-Content -Path 'C:\script_out\RawADList2.txt'
Remove-Item 'C:\script_out\RawADList1.txt'
Remove-Item 'C:\script_out\RawADList2.txt'
function Return-DropDown {
$Choice = $DropDown.SelectedItem.ToString()
if ($Choice)
{
net group $Choice /domain >> C:\script_out\output.txt
}
}
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 340
$Form.height = 150
$Form.Text = "ADGroup"
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(10,10)
$DropDown.Size = new-object System.Drawing.Size(300,30)
ForEach ($Line in $DropDownList) {
$DropDown.Items.Add($Line)
}
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(200,20)
$Form.Controls.Add($DropDownLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "Pick One"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
I have the below code that used to work fine with Powershell x64. Long story short, there is something corrupted with the config file for x64 which I am waiting to get fixed (I do not have admin access to fix; separate issue).
The top five lines of code are included to force Powershell 32-bit to run, and it indeed works. My issue is that, for some reason, after successfully running with the 32-bit .exe, the code runs a second time with the 64-bit .exe, which of course throws all sorts of errors related to the config file issues.
Errors aside, is there any obvious piece of code that causes everything from line 7 and on to run a second time?? I will do my best to provide any more information that you might need in assisting me with this issue.
NOTE: The entire code set from line 7 and on works perfectly fine, so nothing needs to be changed besides fixing this loop.
Set-ExecutionPolicy -Scope CurrentUser Unrestricted
if ($env:Processor_Architecture -ne "x86")
{ &"$env:windir\syswow64\windowspowershell\v1.0\powershell.exe" -noprofile -file $myinvocation.Mycommand.path }
$env:Processor_Architecture | Out-Null
[IntPtr]::Size | Out-Null
if ($startupvariables) { try {Remove-Variable -Name startupvariables -Scope Global -ErrorAction SilentlyContinue } catch { } }
New-Variable -force -name startupVariables -value ( Get-Variable | ForEach-Object { $_.Name } )
Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::OKCancel
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageTitle = "Strike 1"
$MessageBody = "This script sends the user Strike 1 of the 3 strike process.`n`nTo use it, enter the below information:`n`n`n`tTicket Number`n`n`tUser's Email Address`n`n`tInformation that you want to convey to user`n`n`nIf this is the script you want to use, click OK.`nIf not, click Cancel."
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
if ($Result -eq "Cancel")
{
Exit-PSSession
}
else
{
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$Separator = ".", "#"
$Ticket = [Microsoft.VisualBasic.Interaction]::InputBox("Enter ticket number" , "Ticket Number")
$UserID = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the user's email address" , "User Email Address")
function Read-MultiLineInputBoxDialog([string]$Message, [string]$WindowTitle, [string]$DefaultText)
{
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Size(10,10)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.AutoSize = $true
$label.Text = $Message
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Size(10,40)
$textBox.Size = New-Object System.Drawing.Size(575,200)
$textBox.AcceptsReturn = $true
$textBox.AcceptsTab = $false
$textBox.Multiline = $true
$textBox.ScrollBars = 'Both'
$textBox.Text = $DefaultText
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Size(415,250)
$okButton.Size = New-Object System.Drawing.Size(75,25)
$okButton.Text = "OK"
$okButton.Add_Click({ $form.Tag = $textBox.Text; $form.Close() })
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Size(510,250)
$cancelButton.Size = New-Object System.Drawing.Size(75,25)
$cancelButton.Text = "Cancel"
$cancelButton.Add_Click({ $form.Tag = $null; $form.Close() })
$form = New-Object System.Windows.Forms.Form
$form.Text = $WindowTitle
$form.Size = New-Object System.Drawing.Size(610,320)
$form.FormBorderStyle = 'FixedSingle'
$form.StartPosition = "CenterScreen"
$form.AutoSizeMode = 'GrowAndShrink'
$form.Topmost = $True
$form.AcceptButton = $okButton
$form.CancelButton = $cancelButton
$form.ShowInTaskbar = $true
$form.Controls.Add($label)
$form.Controls.Add($textBox)
$form.Controls.Add($okButton)
$form.Controls.Add($cancelButton)
$form.Add_Shown({$form.Activate()})
$form.ShowDialog() > $null
return $form.Tag
}
$Issue = Read-MultiLineInputBoxDialog -Message "What is the information you want to convey to the user?" -WindowTitle "Information Requested"
$User = $UserID.split($Separator)
$Firstname = $User[0].substring(0,1).toupper()+$User[0].substring(1).tolower()
$Lastname = $User[1].substring(0,1).toupper()+$User[1].substring(1).tolower()
$User = $Firstname, $Lastname
$Username = [System.Environment]::UserName
$subject = "Ticket $Ticket on Hold for User Response - Status Reminder #1"
$body = "
To $User,
Your IT Service Desk ticket is currently in Hold for User Confirmation/Input status while we wait for feedback/action from you. Without your feedback/action we cannot continue our efforts to resolve this ticket.
$Issue
As per policy, if a response is not received within 2 business days of your ticket being changed to Hold for User Confirmation/Input status, we will consider your issue resolved.
Thank You,
IT Service Desk
"
$ButtonType = [System.Windows.MessageBoxButton]::YesNo
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageTitle = "Strike 1"
$MessageBody = "The information you have entered is show below:`n`n`nTicket Number: $Ticket`n`nUser's Email Address: $UserID`n`nInformation Requested: $Issue`n`n`nIf you would like to send the email, click Yes.`nOtherwise, click No."
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
if ($Result -eq "No")
{
Exit-PSSession
}
else
{
Send-MailMessage -To "<$UserID>" -bcc "<$Username#email.com>" -from "<itservicedesk#email.com>" -Subject $subject -SmtpServer "mailrelay.email.com" -body $body
}
}
Function Clean-Memory {
Get-Variable |
Where-Object { $startupVariables -notcontains $_.Name } |
ForEach-Object {
try { Remove-Variable -Name "$($_.Name)" -Force -Scope "global" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue}
catch { }
}
}
the issue is here
if ($env:Processor_Architecture -ne "x86")
{ &"$env:windir\syswow64\windowspowershell\v1.0\powershell.exe" -noprofile -file $myinvocation.Mycommand.path }
When you run this in a powershell x64 all you are doing is starting the x86 powershell but not killing the x64 so the x64 keeps running the script.
Add a exit like
if ($env:Processor_Architecture -ne "x86"){
&"$env:windir\syswow64\windowspowershell\v1.0\powershell.exe" -noprofile -file $myinvocation.Mycommand.path
exit
}