I am extremely new to PowerShell tool making so forgive my ignorance. I imported the WPK module and am trying to create a box with multiple fields. One of those fields is a password field but it shows the password as it's being typed. I am unsure of how to make it so that while typing the text is hidden.
New-TextBlock -Text "Database Login Name" `
-Row 2 -Column 0 -VerticalAlignment Center -Margin 5
New-TextBox -Name DbLoginId `
-Row 2 -Column 1 -Margin 4
New-TextBlock -Text "Db Login Password" `
-Row 3 -Column 0 -VerticalAlignment Center -Margin 5
New-TextBox -Name DbLoginPassword `
-Row 3 -Column 1 -Margin 4
Just a heads up, I am not a programmer what so ever. I am a Database Administrator but I have been thrown into this development project so I am literally learning on the fly right about now.
Change your password TextBox to a PasswordBox.
New-PasswordBox -Name DbLoginPassword `
-Row 3 -Column 1 -Margin 4
Related
I am unable to find clear information anywhere on how to use the ShowUI functions. I've created a function that searches for movie information in an effort to easily update my movie list. The function works fine and I can add the content to a New-Window but I would love to be able to add a button that closes it or perhaps close it after a certain period. For the latter, I run into the window taking priority until it is closed, which makes no sense for what I am trying to do. My code is below.
function movie($sss,$ii){
cls
$a = ((wget "https://www.allmovie.com/search/all/$sss" ).allelements|where tagname -match "LI"|where class -match 'movie')|select *
$a = ($a.innertext -join(";") -isplit('movie') -split(';'))
if($ii -lt ''){
$a = $a[4] -replace('\s\s')
}else{
$a -replace("(.?img|DB|#.*|_images*|.com).*|\s\s|(""\W)", "")|out-host
BREAK
}
$moviesearch = new-window $B -TaskbarItemInfo "Movie Search" -Background black -Foreground red -width 500 -Height 300 -top 100 -left 100 -FontSize 16 -show|out-null
}
I'm trying to track progress and I think it's a scope issue. The two functions are located in Functions.ps1 and are called from another script main.ps1. The progress is tracked fine until it gets the the invoke-command in Function 1. Why is the remote session resetting progress tracker? Here is what the code and output look like:
main.ps1
. "C:\Functions.ps1" #dot source so functions available
$steps = 10
Write-ProgressHelper -stepTotal $steps -Status "reset"
Functions.ps1
Function Write-ProgressHelper {
param (
[int]$stepTotal,
[string]$Status
)
Switch ($Status)
{
"run" {
$global:Counter++
Write-Host "$Counter"
Write-Progress -Activity "STIG" -PercentComplete (($Counter / $stepTotal) * 100)
}
"reset" {
$Counter = 0
Write-Host "Counter Reset"
}
}
}
Function 1 {
Write-Host "StepNumber:1"
Write-ProgressHelper -stepTotal $steps -Status "run"
#some code here
Write-Host "StepNumber:2"
$sysinfo = {
. "C:\Functions.ps1" #dot source so write-progresshelper is available within invoke-command
#more code
Write-Host "StepNumber:3"
Write-ProgressHelper -stepTotal $using:steps -Status "run"
Write-Host "StepNumber:4"
#more code
Write-ProgressHelper -stepTotal $using:steps -Status "run"
}#end sysinfo
Invoke-Command -ComputerName $Servers -ScriptBlock $sysinfo
Write-Host "StepNumber:10"
Write-ProgressHelper -stepTotal $using:steps -Status "run"
}#end Function 1
There are 10 Steps to track in Function 1 and then I output the $counter variable. Step 1,2, and 10 are within Function 1. Steps 3-9 are in invoke-command session. I tried declaring the counter as global and that didn't work (I had it declare with script scope in order for it to increment at all). Here is the output:
StepNumber:1
1
StepNumber:2
2
StepNumber:3
1
StepNumber:4
2
StepNumber:5
3
StepNumber:6
4
StepNumber:7
5
StepNumber:8
6
StepNumber:9
7
StepNumber:10
3
Remote variables don't get saved back to local variables (See: about_Remote_Variables). So even though you define $counter as a $global variable, it is only global within that PowerShell session. It is not available on the remote computer. When the Invoke-Command is called, a new PowerShell session is created on that computer, and $counter is reset to 0. It will increment and return. The existing value of the $counter variable on the local computer is retained, and continues on.
When we look at what the value of $global:Counter is, we have to realize that the variable is only available on the computer that the script is executing:
Local Computer Server1
----------------- -------------------
$global:Counter $global:Counter
_________________ ___________________
StepNumber:1 1 (0)
1
StepNumber:2 2 (0)
2
Invoke-Command -Computer Server1
|---------------->
StepNumber:3 (2) 1
1
StepNumber:4 (2) 2
2
StepNumber:5 (2) 3
3
StepNumber:6 (2) 4
4
StepNumber:7 (2) 5
5
StepNumber:8 (2) 6
6
StepNumber:9 (2) 7
7
<-----------------|
StepNumber:10 3
3
See how the value of $global:Counter is incremented for step 1 and 2. When the Invoke-Computer is run, PowerShell creates a new PowerShell session on the remote computer, where the value of $global:Counter is 0. That is why you see the value apparently "reset". After steps 3-9 execute, the process returns to the local computer, where the value of $global:Counter is still 2. That is why you see for Step 10, it returns 3.
To have an effective counter, you will have to pass the counter value to the Invoke-Command as an AgrumentList and return the value back.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
Please could somebody recommend a Powershell, mouse tutorial. I have been searching the internet but cannot find instructions on how to do then following.
I have data in a database which I want to select via a mouse. I have been able to display an output from the database in a Textbox, but cannot select it.
What I want to be able to do is select the line in the output and then use the information in that line.
Any ideas greatly appreciated.
You can use Out-GridView -PassThru on Windows to achieve this:
$selected = $datatable |Out-GridView -PassThru
Select the relevant line(s) by clicking (ctrl-click for more than one), and click Ok in the lower right corner.
$selected will then contain the relevant row(s)
Here are some functions I use myself. I've tried to adapt them to how you will use it.
Add-Type -AssemblyName System.Windows.Forms
$wshell = New-Object -ComObject wscript.shell;
function Mouse-signature-import(){
$global:signature=#'
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
'#
$global:SendMouseClick = Add-Type -memberDefinition $global:signature -name "Win32MouseEventNew" -namespace Win32Functions -passThru
}
function Mouse-Drag($from1,$from2,$to1,$to2){
Mouse-signature-import
[System.Windows.Forms.Cursor]::Position = "$from1,$from2"
$global:SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
[System.Windows.Forms.Cursor]::Position = "$to1,$to2"
start-sleep -m 1500 # If we do not sleep, then the drag does not work right.
$global:SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);
write-host -f yellow -b black "Mouse-Drag" -nonewline; write-host -f Gray " from " -NoNewline; write-host -f Magenta "[" -nonewline; write-host -f Red "$from1 $from2" -nonewline;write-host -f Magenta "]" -nonewline; write-host -f Gray " to " -NoNewline; write-host -f Magenta "[" -nonewline; write-host -f Red "$to1 $to2" -nonewline; write-host -f Magenta "]";
}
function SendInput($a){
[System.Windows.Forms.SendKeys]::SendWait($a)
}
start-sleep -s 1
Mouse-Drag 90 166 126 358
start-sleep -s 1
sendInput "^(c)"
$a = Get-Clipboard
$a
And to figure out the coordinates for where I want to move the mouse cursor to, I use this: https://learn-powershell.net/2015/11/30/create-a-mouse-cursor-tracker-using-powershell-and-wpf/
Basically what I'm doing above is moving the mouse to a cursor position, then I drag the mouse to position 2. Send a CTRL+C to copy what we have highlighted, then use get-clipboard to push the selected text into a variable ($a).
This method is not perfect for what you're trying to do, but it is how you want to do it so this is why I supplied my code. I would recommend trying to get the data through some other means as there is a lot that could go wrong here.
What I am doing is returning a table from a function (RetriveData) that retrieves it from a database.
$listTable = RetriveData | Format-Table | Out-String
Write-host “$listTable”
$selected = $listTable | Out-GridView –PassThru
Write-host “$selected”
The write hosts are showing the table contents as I was expecting. The dialogue opened by Out-GridView shows the table highlighted in light blue, but seems to only be selectable as a complete table.
Am I using the | Format-Table | Out-String correctly here?
The code being used in the RetriveData function to return from the data base is.
$table = Invoke-Sqlcmd –ServerInstance $databaseServer $databaseName –Query $sql
Return $table
So I have the following Tk interface and I want to run my clients and servers from it.
The problem is that when I want to run the servers for example, it won't keep them opened and will execute them only once and then close them, like a normal Tcl script.
Can you tell me a way to open and run a script and keep it running without?
package require Tk
wm title . "Image Organizor"
grid [ttk::frame .c -padding "3 3 12 12"] -column 0 -row 0 -sticky nwes
grid [ttk::label .c.serverLabel -text "Servers"] -column 1 -row 1 -sticky e
grid [ttk::label .c.idServerLabel -text "Test Clients"] -column 2 -row 1 - sticky e
grid [ttk::button .c.slbl -text "Server" -command server] -column 1 -row 2 -sticky e
grid [ttk::button .c.idlbl -text "Id Server" -command idServer] -column 1 -row 3 -sticky e
grid [ttk::button .c.c1lbl -text "Client 1" -command client1] -column 2 -row 2 -sticky e
grid [ttk::button .c.c2lbl -text "Client 2" -command client2] -column 2 -row 3 -sticky e
grid [ttk::button .c.c3lbl -text "Client 3" -command client3] -column 3 -row 2 -sticky e
grid [ttk::button .c.c4lbl -text "Client 4" -command client4] -column 3 -row 3 -sticky e
grid [ttk::button .c.configlbl -text "Config" -command config] -column 1 -row 4 -sticky e
foreach w [winfo children .c] {grid configure $w -padx 5 -pady 5}
proc server {} {
source ImageOrganizor/imageOrganizorServer.tcl
}
proc config {} {
exec notepad.exe ImageOrganizor/config.txt
}
You could run the script in a separate thread using thread::create
Or simply execute the script via shell in a separate process, e.g. exec ImageOrganizor/imageOrganizorServer.tcl
Or source your code at root level and execute a proc to start your server, which might in turn either start a thread, execute or fork a new process.
Basically I'm playing with get-content to create a bunch of users in Office365:
$a=get-content C:\users.txt
foreach($b in $a)
{New-Mailbox -Name $b -Alias $b –Shared -PrimarySmtpAddress $b#email.org
Add-MailboxPermission $b -User me#email.com -AccessRights FullAccess -AutoMapping $true}
This works to bulk import a set of users from a text file, but I need to take it a step further.
I have 2 users in users.txt:
user1
user2
I have 4 different test stacks in stacks.txt:
uat1
qa1
trn1
cln1
I want a total of 8 users maybe in a text file or as results that I can variable into the script above:
user1uat
user1qa1
user1trn1
user1cln1
user2uat
user2qa1
user2trn1
user2clan1
Not sure how to accomplish this in a way that I can set it as a variable to then created 8 shared mailboxes using the script above.
Think i figured it out.
$a=get-content C:\irusers.txt
$c=get-content C:\stacks.txt
$e=foreach ($b in $a) {
foreach ($d in $c) {
-join($b,$d)}}